473,394 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. private void btnDifference_Click(object sender, EventArgs e)
  2.         {
  3.             lblDifference.Visible = true;
  4.             salesDifference = comp.GetDifference(difference);       
  5.             outputMessage = "The difference between the highest and lowest sales is $" + salesDifference;          
  6.             lblDifference.Text = outputMessage;
  7.         }

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?
Jan 9 '10 #1
5 2011
tlhintoq
3,525 Expert 2GB
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.
Jan 9 '10 #2
oh ok, this is my first time posting.

Thanks.
Jan 9 '10 #3
tlhintoq
3,525 Expert 2GB
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
Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. // class comp
  2. public decimal GetDifference(decimal ValueOne, decimal ValueTwo)
  3. {
  4.    return ValueOne - ValueTwo;
  5. }
  6.  
  7. // class Form1
  8. private void btnDifference_Click(object sender, EventArgs e)
  9.         {
  10.            lblDifference.Visible = true;
  11.            lblDifference.Text = string.format("The difference between the highest and lowest sales is $ {0}", comp.GetDifference(FirstValue, SecondValue));// Whereever your values are coming from
  12.         }
  13.  
Jan 9 '10 #4
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:

Expand|Select|Wrap|Line Numbers
  1. 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.
Jan 9 '10 #5
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.
Jan 9 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Chris Allen | last post by:
Hi I'm new to PHP and I'm trying to create a Login Form. Once the user has logged in then he shouldn't have to log in again. The trouble is I'm getting a new session ID between every page and so...
1
by: Erick Bodine | last post by:
I am trying to set a new environment variable on a W2k machine with only partial success. The name("SSID") and value("ASIM") show up correctly in the registry and when I go to "System...
3
by: KathyB | last post by:
Hi, I've tried everything I can think of. Using the script below, I thought on submit my ConfirmSave function would run, then if true returned, would run my form action. At this point it appears...
0
by: Cordell Lawrence | last post by:
Okay guys, We are wondering if this is a bug in Framework 2.0.40607 and looking for some clarification on the issue. Take a look at the folowing code. public delegate bool BoundryTest(int...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
36
by: Cap'n Ahab | last post by:
I have used VB3 - VB6, so learning all this OO stuff is reasonably new to me (although I looked at Java a few years ago). Anyway, I thought I would write a small class to begin with, with a...
9
by: Coleen | last post by:
Hi All :-) I am desperately looking for some help/information on how to direct page flow. Forget what I have done - here's what I need to do: I have a large ASPX.Net - VB.Net web application...
4
by: Alan T | last post by:
I got a method in my ancestor form declared as Protected, this method has empty body. In my descendant form I declared as Protected also, then compile has no problem but the name of the method has...
8
by: Lucvdv | last post by:
VS2005: can someone explain why this code doesn't cause endless recursion? It just returns False. However, if you replace "Return TestProp" with "Return Me.TestProp", the compiler warns for...
38
by: tshad | last post by:
In VS 2008, why doesn't ToString require "()". If I have Option Strict On on why can I do both: selectedIndex.ToString() selectedIndex.ToString or sQuery =...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.