James
James

Reputation: 25

How can I declare a string within this method?

I have a public string, called 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");
}

Upvotes: 0

Views: 112

Answers (5)

Craig Wilson
Craig Wilson

Reputation: 12624

Remove the quotes:

st.DeleteTask(tester);

Upvotes: 5

Iain Ward
Iain Ward

Reputation: 9936

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?

Upvotes: 0

Nathan Romano
Nathan Romano

Reputation: 7096

Couldn't you just pass the variable tester to the method st.DeleteTask?

Upvotes: 2

Ord
Ord

Reputation: 5843

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.

Upvotes: 3

Kyle Trauberman
Kyle Trauberman

Reputation: 25684

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);
}

Upvotes: 0

Related Questions