|
-
May 11th, 2009, 05:29 AM
#1
static variables
hi everyone ,
Im having problems in saving values in the static variables. Here it is how it goes
i have 3 projects in a solution. Lets say A , B and C.
I have added the references of C in A and B
A calls C and saves a value .
Later B calls C but B get the value as 0(even though this variable wasnt used anywhere in between)
this is how my code looks like
A:
Code:
public class Program
{
static void Main(string[] args)
{
...
...
Mediator.Class1 mediat = new Mediator.Class1();
mediat.test(4);
waitHandle.WaitOne();
}
}
B:
Code:
...
...
Mediator.Class1 mediat = new Mediator.Class1();
mediat.recieveEle(request);
C:
Code:
public class Class1
{
public static int m_globalVar = 0;
int j;
public static int GlobalVar
{
get { return m_globalVar; }
set { m_globalVar = value; }
}
public void test( int i)
{
GlobalVar = i;
}
public void recieveEle(Element ele)
{
j = GlobalVar;
}
}
Last edited by nightscorpion; May 12th, 2009 at 03:10 AM.
-
May 11th, 2009, 06:00 AM
#2
Re: static variables
You say you have a problem with saving values,
but you did not share the error message or the core problem with us.
What is exactly your problem?
-
May 11th, 2009, 06:07 AM
#3
Re: static variables
sounds like a homework...
Last edited by memeloo; May 11th, 2009 at 06:30 AM.
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback  you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
May 11th, 2009, 07:00 AM
#4
Re: static variables
its not my homework.its a project on which im working on.n i guess there is a small mistake wht i have in this code which is actually getting to my nerves. The problem is the variable j gets the value 0 when the method recieveEle is called.
-
May 11th, 2009, 07:11 AM
#5
Re: static variables
well, there is no problem with the code.
Just look at the following again
Code:
in A:
Mediator.Class1 mediat = new Mediator.Class1();
Code:
in B:
Mediator.Class1 mediat = new Mediator.Class1();
You just create two independent instances of your class. Surely the value you set in one instance is not set in the other one. They are independent.
-
May 11th, 2009, 08:32 AM
#6
Re: static variables
 Originally Posted by nightscorpion
 its not my homework.its a project on which im working on.n i guess there is a small mistake wht i have in this code which is actually getting to my nerves. The problem is the variable j gets the value 0 when the method recieveEle is called.
Well...I'm not sure what the problem is. It seems to me that '0' is a valid value for GlobalVar. There is nothing in the sample to suggest that GlobalVar should have a different value when 'recieveEle' is called. If you think otherwise perhaps you could set up a break point in the setter (i.e. set { m_globalVar = value; }). You may need the [DebuggerStepThrough] attribute to do this. With the break point you will be able to see at what point GlobalVar is being set to '0' before 'recieveEle' is called.
Last edited by nelo; May 11th, 2009 at 08:33 AM.
Reason: Completeness
-
May 11th, 2009, 08:37 AM
#7
Re: static variables
hmm.. ok
so wht other option do i have so that i can get the value of j=4?
or perhaps that i could capture the same instance in two classes?
when i set the breakpoint in B where the new instance is created , thats where j is set to 0
thank you
Last edited by nightscorpion; May 11th, 2009 at 08:58 AM.
-
May 12th, 2009, 01:17 AM
#8
Re: static variables
 Originally Posted by nightscorpion
when i set the breakpoint in B where the new instance is created , thats where j is set to 0
No wonder. You just access a different instance than the one in the main is set.
 Originally Posted by nightscorpion
hmm.. ok
so wht other option do i have so that i can get the value of j=4?
or perhaps that i could capture the same instance in two classes?
1) you could create a static class, which needs no instances and is directly accessible from anywhere.
2) you could pass the instance from one class into other class (by using parameters) to use values there
-
May 12th, 2009, 03:01 AM
#9
Re: static variables
But there is common static variable, which holds the value. It should work... hm...
Other idea: "j" is never declared in the example. What it is? Maybe you are inspecting other "j" than you think you set the value?
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post. 
-
May 12th, 2009, 03:16 AM
#10
Re: static variables
hi ,
1) you could create a static class, which needs no instances and is directly accessible from anywhere.
yeah actually the static variable should be holding the value even though i have 2 instances.....well i cannot declare my class as static coz i would later also want to implement an interface .
2) you could pass the instance from one class into other class (by using parameters) to use values there
i do not have much expierence ..but can i know how can i pass the instance of one class to another by using parameters?
regarding j ..i had initialised it in my program. however i forgot to initialise it in my post..but now i have edited the code.
Last edited by nightscorpion; May 12th, 2009 at 03:22 AM.
-
May 12th, 2009, 04:00 AM
#11
Re: static variables
Just study this code and you're fine.
A is some basic class.
B just get all values from A passed and can be used independently. Changes in B do not affect A.
C gets a references on A. Everything changed in C, is changed also in A.
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
A myClass_A_in_Main = new A(); // create instance of A
myClass_A_in_Main.variable = 5; // set value in that instance
myClass_A_in_Main.Display_Value(); // display value from that instance
B myClass_B_in_Main = new B(myClass_A_in_Main); // create new instance of B, pass A, copy values
myClass_B_in_Main.Display_Value(); // Display value in other instance
C myClass_C_in_Main = new C();
myClass_C_in_Main.DoSomething(ref myClass_A_in_Main); // change referenced value
myClass_A_in_Main.Display_Value(); // display value from that instance
Console.Read();
}
}
/// <summary>
/// Simple Class with display method.
/// </summary>
class A
{
public int variable;
public void Display_Value()
{
Console.WriteLine(variable);
}
}
/// <summary>
/// This Class holds the other class A inside. The values are passed through constructor
/// </summary>
class B
{
A myClass_A_in_B = new A();
public B(A getInstance) // Constructor with input parameter
{
myClass_A_in_B = getInstance; // copy values if new instance is created
}
public void Display_Value()
{
Console.WriteLine(myClass_A_in_B.variable);
}
}
/// <summary>
/// This Class get only a reference on another Class. Changes here affects the other class, too.
/// </summary>
class C
{
public void DoSomething(ref A referencedA)
{
referencedA.variable = 999;
referencedA.Display_Value();
}
}
}
-
May 12th, 2009, 04:08 AM
#12
Re: static variables
I think this code is wrong because A, B and C are not classes but projects, so they are assemblies: A is an exe and B&C are dlls I guess
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback  you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
May 12th, 2009, 04:17 AM
#13
Re: static variables
yes ur right memeloo..
My Project A is a console application.
Project B is a class library.
Project C is also a class library.
-
May 12th, 2009, 04:30 AM
#14
Re: static variables
I'm not sure becasue I cannot prove it but I think there are two "static instances" of C, one in the scope of A and one in the scope of B so it won't work... we need someone with greater knowledge ; ]
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback  you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
May 12th, 2009, 04:39 AM
#15
Re: static variables
 Originally Posted by nightscorpion
yes ur right memeloo..
My Project A is a console application.
Project B is a class library.
Project C is also a class library.
It does not matter, where the classes are stored. If in the same file or in an extra .cs
The method is always the same!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|