Why doesn't my method return the variable at all?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Angry C sharp user
    New Member
    • Jan 2010
    • 4

    Why doesn't my method return the variable at all?

    I am currently working on a Windows Forms Application and I need to create a few methods in another class and then call the methods back into my form1.cs but every time I set my global variables equal to the ones I returned from the method, it says that my variables do not exist in the current context.

    I'm still learning how to work with c sharp so it might not be any close to perfect but this is an example of what is happenning:

    Code:
    private void btnDifference_Click(object sender, EventArgs e)
            {
                lblDifference.Visible = true;
                salesDifference = comp.GetDifference(difference);       
                outputMessage = "The difference between the highest and lowest sales is $" + salesDifference;          
                lblDifference.Text = outputMessage;
            }

    salesDifference is my global variable that i created in form1.cs
    comp is the name of my class that i created at the top of form1.cs aswell
    difference is the name of the variable that i returned in the method GetDifference but it says that difference does not exist in the current context and i don't know why >.<

    Can someone help me fix this problem?
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • Angry C sharp user
      New Member
      • Jan 2010
      • 4

      #3
      oh ok, this is my first time posting.

      Thanks.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        salesDifference is not "global" if you defined it inside of Form1. It exists throughout the scope of the Form1 class. But your calculation is in its own class "comp". Thus salesdifference doesn't exist in its world (its 'scope' to use proper terminology)

        I would suggest you stop using scope-wide or global variables for such things. Its a bad habit to get into. Instead, pass values to the calculation and get a value in return.

        ifference is the name of the variable that i returned in the method GetDifference
        Code:
        salesDifference = comp.GetDifference(difference);
        Uh... Not really. 'difference' is the value you are passing *TO* the GetDifference() method, not the values returned. salesDifference in this case has no reason to be scope wide. I am also a little confused as to how you could be calculating a difference when you are only passing one value. I would really expect something more like this:

        Code:
        // class comp
        public decimal GetDifference(decimal ValueOne, decimal ValueTwo)
        {
           return ValueOne - ValueTwo;
        }
        
        // class Form1
        private void btnDifference_Click(object sender, EventArgs e)
                {
                   lblDifference.Visible = true;
                   lblDifference.Text = string.format("The difference between the highest and lowest sales is $ {0}", comp.GetDifference(FirstValue, SecondValue));// Whereever your values are coming from
                }

        Comment

        • Angry C sharp user
          New Member
          • Jan 2010
          • 4

          #5
          Uh... Not really. 'difference' is the value you are passing *TO* the GetDifference() method, not the values returned. salesDifference in this case has no reason to be scope wide.
          That made a lot more sence, because I thought that when calling the method you needed to put the variable that you returned, in the brackets of the method, when you just need to reinput the input parameters because the value you returned was set to the method. This fixed my problem.

          I am also a little confused as to how you could be calculating a difference when you are only passing one value.
          My method was supposed to look like:

          Code:
          averageSales = comp.GetAverageSales(sales);
          Sales is an array and so I had a loop with some if statements to find the greatest and lowest value, so I could find the difference between the greatest and lowest value, sorry about my lack of detail earlier.

          Thanks a lot for the help and the tips and also I apologize from my improper terminology.

          Comment

          • Angry C sharp user
            New Member
            • Jan 2010
            • 4

            #6
            Also, I needed to have the salesDifference scope wide because that is how we are being taught right now and so I think it would be best to write the code based on how we were taught for now.

            Comment

            Working...