开发者

Problems with settings a string C#

开发者 https://www.devze.com 2023-03-14 06:44 出处:网络
As far as I am aware the code below appears to do what I intend it to do - although for some reason the deletest string does not appear to be set properly, as w开发者_Go百科hen I click on the button t

As far as I am aware the code below appears to do what I intend it to do - although for some reason the deletest string does not appear to be set properly, as w开发者_Go百科hen I click on the button the action does not happen. Although if I define the actual task name e.g. "task3" it deletes it.

Code:

public string deletest {get;set;}


private void deletetask_Click(object sender, EventArgs e)
{
    string deletetask = DeleteTaskBox.Text;
    ScheduledTasks st = new ScheduledTasks(@"\\" + System.Environment.MachineName);
    st.DeleteTask(deletest);
}


You're passing null to the st.DeleteTask method. deletest and deletetask are two different variables.


string ***deletetask*** = DeleteTaskBox.Text;
ScheduledTasks st = new ScheduledTasks(@"\\" + System.Environment.MachineName);
st.DeleteTask(***deletest***);

Different variable names.


Even if you didn't have the typo that the other answers have mentioned, you'd still have a bug - you wouldn't be setting the property, because you'd still be declaring a new local variable in the method. The call to st.DeleteTask would work, but nothing else would then be able to access it later. You don't want to declare a new variable. You want something like:

public string DeleteTaskName { get; set; }

private void deletetask_Click(object sender, EventArgs e)
{
    // Note that this doesn't declare a variable
    DeleteTaskName = DeleteTaskBox.Text;
    ScheduledTasks st = new ScheduledTasks(@"\\" + Environment.MachineName);
    st.DeleteTask(DeleteTaskName);
}


is this what you were trying to do?

public string deletest {get;set;}

private void deletetask_Click(object sender, EventArgs e)
{
    deletest = DeleteTaskBox.Text;
    ScheduledTasks st = new ScheduledTasks(@"\\" + System.Environment.MachineName);
    st.DeleteTask(deletest);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消