Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing variables problem!

Status
Not open for further replies.

Kenny100

Technical User
Feb 6, 2001
72
NZ
Hi Folks

I'm experimenting with the Timer method and have written the following code (don't laugh until you've read my questions - I know I'm not very good at this!!!). However, I've got two problems:

...
// Passes NewName to the Namer method
MyName.Namer(NewName);
...

public void Namer(string name) // I can pass it here but I don't need it here!!
{
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnNamerTimer);
aTimer.Interval = 5000;
aTimer.Enabled = true;

Console.WriteLine("Press \'q\' to quit the exit timer");
while(Console.Read()!='q');
}

public static void OnNamerTimer(Object source, ElapsedEventArgs e)
{
System.Timers.Timer aTimer = source as System.Timers.Timer;
string name = **name in Namer**;
...
...
}

1. How can I pass the NewName variable to OnNamerTimer? I can pass it to Namer OK but I don't need to use it here! I can't seem to pass it to OnNamerTimer without getting an error.
2. I copied the timer code from an example program. How can I change the code so that I can get rid of the Console.WriteLine("Press \'q\' to quit the exit timer") stuff?

Cheers for any help!

Kenny.
 
Well, for one, you're not looking for a string in OnNamerTimer.

Second, it seems like you're doing this all in one class, so why pass the variable at all? Make a class-level variable, and have it modified by your Namer() method.

I'll give you an example (it may not work to a t, because I'm more familiar with Java, but the syntax should be very similar).

public class timerClass
{
String strName;

//Insert stuff here to change the value of strName

public static void OnNamerTimer(Object source, ElapsedEventArgs e)
{
System.Timers.Timer aTimer = source as System.Timers.Timer;
string name = strName;
...
...
}

}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top