如何将字符串类型转换为任务类型
本文关键字:任务 类型 类型转换 字符串 | 更新日期: 2023-09-27 18:09:12
我有一个由某些任务组成的xml
节点字符串,以及一个项目列表字符串,我正在迭代任务和项目列表,并调用另一个类的函数并传递任务和项目列表,但它给了我best overloaded method and it has some invalid argument
的错误。我假设这是一个类型转换错误。
private void addtask(object sender,RoutedEventArgs e)
{
foreach (object item in tasklistBox.Items)
{
var listBoxItem = tasklistBox.ItemContainerGenerator.ContainerFromItem(item);
var myContentPresenter = FindVisualChild<ContentPresenter>(listBoxItem);
var myDataTemplate = myContentPresenter.ContentTemplate;
var mydata = (System.Windows.Controls.Label)myDataTemplate.FindName("tasklabel", myContentPresenter);
var xmlElement = (XmlElement)mydata.Content;
//System.Windows.Forms.MessageBox.Show(xmlElement.InnerText);
foreach (Harvest_Project item1 in Globals._globalController.harvestManager._PROJECTLIST)
{
Globals._globalController.harvestManager.assignTaskToProject(xmlElement.InnerText,item1);
}
}
}
和我正在调用的另一个类的函数:
public void assignTaskToProject(Harvest_Task task, Harvest_Project project)
{
Console.WriteLine("Assigning Task to Project");
string assignXML = "<task>" +
"<id type='"integer'">" + task._id + "</id> " +
"</task>";
sendPOSTRequest(uri + _GETALLPROJECTS + "/" + project._id + "/task_assignments", assignXML);
}
Harvest_Task
类在这里:
private bool _billable_by_default;
private bool _deactivated;
private float _default_hourly_rate=0;
public int _id { get; set; }
private bool _is_default;
public string _name { get; set; }
private DateTime _created_at;
private DateTime _updated_at;
private XmlNode _node;
public Harvest_Task()
{ }
public Harvest_Task(XmlNode node)
{
this._node = node;
this._name = node.SelectSingleNode("name").InnerText;
this._created_at = Harvest_Base.storeTime(node.SelectSingleNode("created-at").InnerText);
this._updated_at = Harvest_Base.storeTime(node.SelectSingleNode("updated-at").InnerText);
this._is_default = bool.Parse(node.SelectSingleNode("is-default").InnerText);
this._deactivated = bool.Parse(node.SelectSingleNode("deactivated").InnerText);
float.TryParse(node.SelectSingleNode("default-hourly-rate").InnerText,out this._default_hourly_rate);
this._id = int.Parse(node.SelectSingleNode("id").InnerText);
}
public Harvest_Task(string name, float defaultHourlyRate)
{
this._name = name;
this._default_hourly_rate = defaultHourlyRate;
}
public string createXMLAddEntry()
{
string returnXML = "<task>" +
"<billable-by-default type='"boolean'">true</billable-by-default>" +
"<default-hourly-rate type='"decimal'">"+this._default_hourly_rate+"</default-hourly-rate>" +
"<is-default type='"boolean'">true</is-default>" +
"<name>" + this._name + "</name> " +
"</task>";
return returnXML;
}
Harvest_Project
类在这里
public string _name { get; set; }
private DateTime _over_budget_notified_at;
private bool _billable;
private DateTime _created_at;
private bool _active;
private enum _bill_by { Tasks, People, none };
public int _client_id = 0;
private string _code;
private string _notes;
private enum _budget_by { project, project_cost, task, person, none };
private float _budget = 0; //Budget in hrs
private DateTime _latest_record_at;
private DateTime _earliest_record_at;
private int _fees = 0;
public int _id { get; set; }
private DateTime _updated_at;
private XmlNode _node;
public int getId() { return this._id; }
public Harvest_Project(XmlNode node)
{
this._node = node;
this._name = node.SelectSingleNode("name").InnerText;
this._created_at = storeTime(node.SelectSingleNode("created-at").InnerText);
this._updated_at = storeTime(node.SelectSingleNode("updated-at").InnerText);
this._over_budget_notified_at = storeTime(node.SelectSingleNode("over-budget-notified-at").InnerText);
this._latest_record_at = storeTime(node.SelectSingleNode("hint-latest-record-at").InnerText);
this._earliest_record_at = storeTime(node.SelectSingleNode("hint-earliest-record-at").InnerText);
this._billable = bool.Parse(node.SelectSingleNode("billable").InnerText);
try
{
this._id = Convert.ToInt32(getXmlNode("id", node));
this._client_id = Convert.ToInt32(getXmlNode("client-id", node));
this._budget = float.Parse(getXmlNode("budget", node));
this._fees = Convert.ToInt32(getXmlNode("fees", node));
}
catch (OverflowException e)
{
Console.WriteLine("The number cannot fit in an Int32.");
}
catch (FormatException e)
{
Console.WriteLine("The string is not a valid integer");
}
this._code = node.SelectSingleNode("code").InnerText;
this._notes = node.SelectSingleNode("notes").InnerText;
}
这个函数有Harvest_task
和Harvest_Project
类,我正在调用这个函数,但无法调用。这行给出错误
Globals._globalController.harvestManager.assignTaskToProject(xmlElement.InnerText,item1);
使用什么转换?我卡住了
帮忙吗?由于
目前来看你可以用这个
foreach (Harvest_Project item1 in Globals._globalController.harvestManager._PROJECTLIST)
{
Globals._globalController.harvestManager.assignTaskToProject(new Harvest_Task(xmlElement), item1);
}
我正在使用来自Harvest_Task的构造器public Harvest_Task(XmlNode node)
来创建一个实例,而不是将xmlElement.InnerText
作为参数传递给assignTaskToProject
方法
我怀疑它失败了,因为你的xml没有任何id和构造函数期望node.SelectSingleNode("id").InnerText
你将以某种方式需要解析InnerText属性来创建Harvest_Task的类型- InnerText将是字符串类型,所以它可能是一个计划,添加一个'Converter'类,它接受字符串类型并输出一个Harvest_Task对象。
class HarvestTaskConverter {
public Harvest_Task Convert(string text) {
var task = new Harvest_Task();
// parse the text here
return task;
}
}
只是一些额外的注释-方法/变量的命名不是很c# - camelCase是首选,而'全局'变量通常是不受欢迎的,但这一切都取决于你是否可以控制其他代码