Click to See Complete Forum and Search --> : static variables


nightscorpion
May 11th, 2009, 05:29 AM
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:

public class Program
{
static void Main(string[] args)
{
...
...
Mediator.Class1 mediat = new Mediator.Class1();
mediat.test(4);
waitHandle.WaitOne();
}
}



B:

...
...

Mediator.Class1 mediat = new Mediator.Class1();
mediat.recieveEle(request);


C:

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;

}
}

MNovy
May 11th, 2009, 06:00 AM
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?

memeloo
May 11th, 2009, 06:07 AM
sounds like a homework...

nightscorpion
May 11th, 2009, 07:00 AM
:) 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.

MNovy
May 11th, 2009, 07:11 AM
well, there is no problem with the code.
Just look at the following again


in A:
Mediator.Class1 mediat = new Mediator.Class1();



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.

nelo
May 11th, 2009, 08:32 AM
:) 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.

nightscorpion
May 11th, 2009, 08:37 AM
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

MNovy
May 12th, 2009, 01:17 AM
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.


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

boudino
May 12th, 2009, 03:01 AM
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?

nightscorpion
May 12th, 2009, 03:16 AM
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.

MNovy
May 12th, 2009, 04:00 AM
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.




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();
}
}

}

memeloo
May 12th, 2009, 04:08 AM
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

nightscorpion
May 12th, 2009, 04:17 AM
yes ur right memeloo..

My Project A is a console application.
Project B is a class library.
Project C is also a class library.

memeloo
May 12th, 2009, 04:30 AM
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 ; ]

MNovy
May 12th, 2009, 04:39 AM
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!

nightscorpion
May 12th, 2009, 04:40 AM
ok ill try this out..

boudino
May 12th, 2009, 07:02 AM
It could be, it is the easies case.

nightscorpion, are mediat.test(4) and mediat.recieveEle(request) called in the same process, or in two separate processes (e.g. windows service and a client application)? If it would be two separate processes, you need more comlex solution, like a one based on WCF.

nightscorpion
May 12th, 2009, 08:51 AM
They are two separate processes....one(A) is a console app and project B is a class library .

so u mean to say i need to have WCF services like an LCS with a config file?

boudino
May 12th, 2009, 09:57 AM
WCF or remoting or any other kind of RPC, because as mentioned before, if there are two different proceses, which means two different app domains they, there are also two different "instances" of the Mediator type (the type is loaded in two different instances of CLR) which doesn't share anything, so the static doesn't work the way you know from case where there is only one process.

nabeelisnabeel
May 13th, 2009, 01:31 AM
Is the guy'z problem sharing and accessing data between two different processes?

I don't think so. He has declared and initialized two separate instances of his class and every instance will have its separate data.

Scorpion! if you want to access A's instance in B, make some public method in B which accepts A's instance and pass it your declared instance.

Correct me someone if I am wrong.

MNovy
May 13th, 2009, 02:35 AM
They are two separate processes....one(A) is a console app and project B is a class library .

If the class B is used in your console it is the SAME process
as long you do not start a 2nd exe or create 2nd process.

nightscorpion
May 13th, 2009, 06:47 AM
Hey ,
@MNovy : I tried out the code which u sent me..in ur code u could use the instance as a parameter in ur main coz u have created the instance in the same class itself. Whereas in my case one instance is created in main (project A) and the other instance in Project B...AND both are having their references to C. So between A and B there are no references at all.

to be more precise on wht i am working with project A is my host which is a console app AND which also initiates a Workflow instance . Project B is suppose to start the Enterprise Architect and open a Project(when the project is opened it is in the the form of a tree view.). I select an element from the treeview and it should get this workflow instance which was created in my Project A.
To make this more simpler i used a int instead of a workflow instance to make it easier to understand in the forum.


@nabeelisnabeel : as i said both are having their references to Project C it hardly matters if i declare the methods as public ..as they are 3 different projects

i had created the project C coz A and B was creating a circular dependency and also because A is a console app and B a class library.

i have not much info abt WCF..probably it might work out with it.

MNovy
May 13th, 2009, 11:48 PM
Hey ,
@MNovy : I tried out the code which u sent me..in ur code u could use the instance as a parameter in ur main coz u have created the instance in the same class itself. Whereas in my case one instance is created in main (project A) and the other instance in Project B...AND both are having their references to C. So between A and B there are no references at all.


Where is the problem? You can use the mechanics I mentioned here, too.

Where is B created? In A?
B has to be instantiated somewhere, isn't it?
At this point, just pass the class you want to use there, too.

So you create in A your instance of class C and set all values.
Create instance of B in A (because you want to use it there) and pass the class C directly.

BTW:
Your code design seems to be very, very bad anyway.
If you have to access an instance from other classes, you have to pass either the values by
copying (call by value) them or by reference (call by reference).
Both are explained in my previous code.
Or make that class completely static, then it holds the values directly with no instance.

There is NO other way man.
Redesign your code!!!