CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2012
    Posts
    46

    using variables within 2 different methods

    Code:
    private void timerAlarm_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                if (comboBoxItemsforAlarm.SelectedItem == null)
                    stopWatch.Stop();
                else
                {
                    int hours = stopWatch.Elapsed.Hours, minutes = stopWatch.Elapsed.Minutes;
                    labelStopWatch.Text = hours + ".";
                    if (minutes < 10)
                        labelStopWatch.Text += "0" + minutes;
                    else
                        labelStopWatch.Text += minutes;
                    double doubleStopWatch = Convert.ToDouble(labelStopWatch.Text);
                    
                }
            }
    
    ====================================================================
    
     private void btnAddTimer_Click(object sender, EventArgs e)
            {
                if (comboBoxItemAlarm.Items.Count == 0 && maskedTime.Text == null)
                {
                    MessageBox.Show("select an item");
                }
                else 
                {
                    if (comboBoxItemAlarm.SelectedItem != null)
                    {
                        comboBoxItemsforAlarm.Items.Add(comboBoxItemAlarm.SelectedItem + "-" + maskedTime.Text);
                    }
    
                        string str = comboBoxItemAlarm.SelectedItem.ToString();
                        string[] into = str.Split('-');
                        double timeAllocated = Convert.ToDouble(into[1]);
                        double timeAlarm = Convert.ToDouble(maskedTime.Text);
                        if(timeAlarm > timeAllocated)
                        {
                            MessageBox.Show("eheheh");
                        }  
                }
    
            }
    I want to use if statement for variables in these 2 methods, to compare the timeAlarm with the doubleStopWatch, because of their declaration scope, it seems imposible and if I declare it outside their method it will not pick the right argument, like this:

    if(timeAlarm==doubleStopWatch)
    //do something

  2. #2
    Join Date
    May 2012
    Location
    Bonn, Germany
    Posts
    43

    Re: using variables within 2 different methods

    What do you mean with "it will not pick the right argument"?

  3. #3
    Join Date
    Feb 2012
    Posts
    46

    Re: using variables within 2 different methods

    Thanks. It's may be a mis-use of language. What I mean is if I declare them outside the the methods they are now, I'll not be able to assign value to them. Thanks.

  4. #4
    Join Date
    May 2012
    Location
    Bonn, Germany
    Posts
    43

    Re: using variables within 2 different methods

    But you should be able to do so.
    There's no reason why you couldn't do that.

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