Click to See Complete Forum and Search --> : using function parameter in variables?


metalfred
January 30th, 2009, 10:14 AM
Hello,

I am still a newbie in C# and I have a basic questions for you guys. This is my function:


public void stop_timers()
{
if (video1_started == true)
{
if (statsTimer_1 != null)
{
if (statsTimer_1.Enabled == true)
{
statsTimer_1.Stop();
}
}
if (VectorScopeTimer_1 != null)
{
if (VectorScopeTimer_1.Enabled == true)
{
VectorScopeTimer_1.Stop();
}
}
if (WaveformTimer_1 != null)
{
if (WaveformTimer_1.Enabled == true)
{
WaveformTimer_1.Stop();
}
}
}
}


How can I make it usable for all my video objects? Something like this:


public void stop_timers(int videoid)
{
if (video+videoid+_started == true)
{
if (statsTimer_+videoid+ != null)
{
if (statsTimer_+videoid+.Enabled == true)
{
statsTimer_+videoid+.Stop();
}
}
if (VectorScopeTimer_+videoid+ != null)
{
if (VectorScopeTimer_+videoid+.Enabled == true)
{
VectorScopeTimer_+videoid+.Stop();
}
}
if (WaveformTimer_+videoid+ != null)
{
if (WaveformTimer_+videoid+.Enabled == true)
{
WaveformTimer_+videoid+.Stop();
}
}
}
}


Do you see what I mean? In PHP I would know but I have no idea how to format my functions when using paramaters... Thanks for the help and sorry for the newb question.

-fred

metalfred
January 30th, 2009, 12:11 PM
Anyone?

code?
January 31st, 2009, 12:26 AM
You simply can't have it work that way.

You can do this.

public void StopTimer(Timer T)
{
T.Stop();
}

BigEd781
January 31st, 2009, 02:22 AM
You could also keep them in a Dictionary with the id used as the key.

metalfred
January 31st, 2009, 09:09 AM
Why why whyyyy

It should have been possible to do, PHP is so easy I guess. Here is what I will do:


public void stop_timers(int videoid)
{
if (videoid == 1)
{
if (video1_started == true)
{
if (statsTimer_1 != null)
{
if (statsTimer_1.Enabled == true)
{
statsTimer_1.Stop();
}
}
if (VectorScopeTimer_1 != null)
{
if (VectorScopeTimer_1.Enabled == true)
{
VectorScopeTimer_1.Stop();
}
}
if (WaveformTimer_1 != null)
{
if (WaveformTimer_1.Enabled == true)
{
WaveformTimer_1.Stop();
}
}
}
}
else if (videoid == 2)
{
if (video2_started == true)
{
if (statsTimer_2 != null)
{
if (statsTimer_2.Enabled == true)
{
statsTimer_2.Stop();
}
}
if (VectorScopeTimer_2 != null)
{
if (VectorScopeTimer_2.Enabled == true)
{
VectorScopeTimer_2.Stop();
}
}
if (WaveformTimer_2 != null)
{
if (WaveformTimer_2.Enabled == true)
{
WaveformTimer_2.Stop();
}
}
}

... and so on ...

}


This is ugly tho...

Thanks for the help

(HOW COME YOU CANNOT DO THIS IN C# OMG)

haha
;)

JonnyPoet
January 31st, 2009, 02:45 PM
Hi !
Here is something very very strange in your code.
It basically shouldn't be any roblem to send messages to different locations by the use of ID's
My question is
A) Why do you need another delegate for each of your timers.
B) Can't you fire the event generally bcause it will only be catched by simple that control that is coupled to your event.
For getting this done simple use for example the observer pattern.

If you need different delegates because you maybe will need different EventArgs then why not using a generic delegate

public delegate void MyEventHandler<T>(object sender, MyEventArgs<T> e);

Maybe you will explain a bit more about what you are doing, so we can help you easier. Are this all Timers ? Are they all on different forms ? So my general question is: what do you want to perform. ?
Does this mayb help you

// we wrap our VideoTimer in a small class like
public class Video {
private Timer _statsTimer;
private Timer _vectorScopeTimer;
private Timer _waveFormTimer;

private int _videoID;
private bool _videoStarted;

Public Vide(int videoID){
_statsTimer = new Timer();
_vectorScopeTimer = new Timer();
_waveFormTimer = new Timer();
_videoID =videoID;

}
public bool VideoStarted{
get{return _videoStarted;}
set { _videoStarted = value;}
}
public int VideoID{
get{ return _videoID;}

}
// add all other properties here to enable your different Timers
// you may have for one specific Video like enabling / diasabling them;

}


Dictionary<int, Video> _allVideos = new Dictionary<int, Video();

//....you add a Timer to this like
public void AddVideo( int videoID, Video video){
_allVideos.Add(videoID,video);
}

and you may access a Timer like
public void StopTimers(int videoID){
if( _allVideos.ContainsKey(videoID)){
Video usedVideo = _allVideos[videoID];
if ( usedVideo.VideoStarted == true){
if ( _statsTimer.Enabed ){
_statsTimer.Stop();
}
if ( _vectorScopeTimer.Enabled){
vectorScopeTimer.Stop();
}
....
}
}
This may give you the basic idea hopefully
PS.: Sorry This posted during I was still typing unintentionally. Here is the corrected post