How can I declare a string within this method?
I have a public string, calle开发者_Go百科d tester, that I would like to use within my deletetask_Click event, how could this be used within the st.DeleteTask line?
public string tester {get;set;}
private void deletetask_Click(object sender, EventArgs e)
{
ScheduledTasks st = new ScheduledTasks(@"\\" + System.Environment.MachineName);
st.DeleteTask("tester");
}
Remove the quotes:
st.DeleteTask(tester);
Unless I am misunderstanding your question, you can just try:
st.DeleteTask(tester); // no quotes around variable name
When you put quotes around it, you are essentially creating a new string which contains the text "tester"
. However, when you remove the quotes, C# interprets it as a reference to the tester
variable, which contains the string you already created.
Couldn't you just pass the variable tester to the method st.DeleteTask?
You can just use it as is. In your code snippet, tester
is class-level, and can be used in any of the classes non-static methods.
private void deletetask_Click(object sender, EventArgs e)
{
ScheduledTasks st = new ScheduledTasks(@"\\" + System.Environment.MachineName);
st.DeleteTask(tester);
}
If tester is declared within the same class as that method, then simply like this:
private void deletetask_Click(object sender, EventArgs e)
{
ScheduledTasks st = new ScheduledTasks(@"\\" + System.Environment.MachineName);
st.DeleteTask(tester);
}
If it's not, where is it declared?
精彩评论