Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
2.50/5 (4 votes)
See more:
Hi,

I have a C# class in .net 2.0 in which I want to use the variable(static int) whose values is set in one method which is static and that variable is used in another static method.

The value of that variable changes according to some condition.
That variable must have only two values 0,1 from if else for true and false.

I have declared a static int type variable in that class which is global and both methods can use that, but I got an exception 'object is not reference to object'.

Also value once assign will not change in future assignment.

thanks.
Posted
Updated 16-Mar-11 12:13pm
v2
Comments
Albin Abel 15-Mar-11 7:58am    
Static variables belongs to a class, it can't have multiple memory locations like instance variables. So It is not belongs to each instance and can't access through instance of an object. Look my answer for an example
fjdiewornncalwe 15-Mar-11 13:59pm    
OP Comment moved from answer:
thanks
but i m asking?
can static variable be incremented in any method and new value is used by another method.
Niklas L 15-Mar-11 14:44pm    
yes (in your case)
Albin Abel 15-Mar-11 14:46pm    
The static variable can be incremented. It sustain till the process live (Windows application). If you have a static variable (in a class apart from page) in a web application it sustains till the web server shut off. So care need to be taken in web app that it should not hold high load unless otherwise needed. Now answer for the question, Is new value is used by another method. If you are asking the new value= incremented value then Yes!. If you are asking about assign a new value then Yes!. Static variable also like other variables, but just its scope not belong to a particular instance. is it clear now?
Dalek Dave 16-Mar-11 18:13pm    
Edited for readability.

Static variables not referenced through an object instance.

For example public class Car{ public static int noOfWheels{get; set;} } will be get or set by Car.noOfWheels=4; and not by Car car=new Car(); car.noOfWheels=4;. The later throw exception
 
Share this answer
 
Comments
fjdiewornncalwe 15-Mar-11 13:59pm    
+5, correct.
Albin Abel 15-Mar-11 14:37pm    
Thanks Marcus Kramer!
Sergey Alexandrovich Kryukov 15-Mar-11 20:26pm    
All you say is correct, but OP's case is something else because exception is mentioned. It only happens in run-time. We need source code.
--SA
Sergey Alexandrovich Kryukov 15-Mar-11 20:27pm    
Balwant, show you source code and exception, point out in each line. So far, you got some correct statements, but probably not the Answer to your problem. This is only because you did not share enough information.
Please reply to my comment.
--SA
Albin Abel 16-Mar-11 8:48am    
Hi SAKryukov OP posted a code snippet. But I see this code fragment not complete. May be you have look and predict the problem?
Why didn't somebody just say: yes

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Dalek Dave 16-Mar-11 18:14pm    
YES!!!!! :)
Espen Harlinn 16-Mar-11 18:22pm    
Thanks Dalek :)
fjdiewornncalwe 16-Mar-11 23:01pm    
I'd say yes if she was cute enough...
Espen Harlinn 17-Mar-11 9:39am    
Thanks Marcus!
Albin Abel 16-Mar-11 23:02pm    
Yes! Yes
A side dish: if you need some type with values 0 or 1 only, this is what you should do:

C#
enum Discrete : byte { Low = 0, Hight = 1, } //for example


It will force writing correct code at compile time but won't prevent other values during run-time (the fact which can be used to the benefit as well).

[EDIT]
Why not boolean? There are important applications for that. Here is the example. Imagine a numeric calculation using or not using certain special factor. What to do? Using "if (useSpecialFactor) //..."? Nah! Look:

C#
enum SpecialFactorUse { Ignore = 0, Use = 1, } 
//...
double Calculate(SpecialFactorUse specialFactorUse /* */) {
   //...
   return mainFactor + specialFactor * (int)specialFactorUse;
}

//...

enum Direction { Left = -1, StayHere = 0, Right = +1, } 
//...
Direction direction = getNewDirection(currentDirection, arrowKeys);
//...
double newPosition = currentPosition + step * (int)direction;


—SA
 
Share this answer
 
v5
Comments
Espen Harlinn 16-Mar-11 18:07pm    
Nice idea - 5ed, but I wonder why he isn't using a bool -> " 0,1 from if else for true and false"
Sergey Alexandrovich Kryukov 16-Mar-11 18:29pm    
Because OP did not want bool :-)
No, quite serious, using enumeration is different. Please see my updated Answer above -- pretty interesting...
Did you use such techniques? I find it handy, use from time to time, since Object Pascal and Delphi (maybe even Turbo Pascal). Huh?
--SA
Espen Harlinn 16-Mar-11 18:51pm    
Well, I believe I do :) Those where the days - mapping sets of enums to integer falgs
TEnum = (One,Two,Three) - TEnums = Set Of TEnum, Flags array[TEnum] of Integer ... etc :)
Sergey Alexandrovich Kryukov 16-Mar-11 19:19pm    
Yes, set of enum was great. I address the sets a bit on my article on C#/.NET enum.
--SA
Albin Abel 16-Mar-11 23:04pm    
The Enum idea is a good advice. My 5
Additional instructions: how to report the problem properly. You need to create full exception dump and look at exception stack to point out code lines where exception is being propagated. Please see my directions:

How do i make a loop that will stop when a scrollbar reaches the bottom[^]
When i run an application an exception is caught how to handle this?[^]

When you do it, you will find out where the exception is. If you still cannot solve the problem yourself, post relevant extra information, but only in this way: use "Improve question" and proper formatting.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900