CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2009
    Posts
    10

    using function parameter in variables?

    Hello,

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

    Code:
            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:

    Code:
            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

  2. #2
    Join Date
    Jan 2009
    Posts
    10

    Re: using function parameter in variables?

    Anyone?

  3. #3
    Join Date
    May 2006
    Posts
    306

    Re: using function parameter in variables?

    You simply can't have it work that way.

    You can do this.
    Code:
    public void StopTimer(Timer T)
    {
        T.Stop();
    }

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: using function parameter in variables?

    You could also keep them in a Dictionary with the id used as the key.

  5. #5
    Join Date
    Jan 2009
    Posts
    10

    Re: using function parameter in variables?

    Why why whyyyy

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

    Code:
            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

  6. #6
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: using function parameter in variables?

    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
    Code:
    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
    Code:
    // 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
    Last edited by JonnyPoet; January 31st, 2009 at 04:14 PM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured