-3

I want to use shared variable between two different projects and i want to update that variable each time in c#? I used dll file to share a varible but it didn't worked while updating the variable. please help.

public static class Signals
{
public static bool Admin_Logged_In;
public static bool get_Admin_Flag()
{
    return Admin_Logged_In;
}
public static bool get_Busy_Flag()
{
    return Back_End_Busy;
}
}

i attached above class in DLL file and refered in two different projects & then try to update value in one project & expecting to get updated value in second project. but it doesn't provide updated value.

Rameshwar Pawale
  • 632
  • 3
  • 17
  • 35
  • 3
    Can you _share_ some code? – Tim Schmelter Mar 02 '13 at 12:13
  • why use variable? how about file? or database. – John Woo Mar 02 '13 at 12:13
  • i m updating this variable frequently so i thought for a single variable why to open & close a file each time. – Rameshwar Pawale Mar 02 '13 at 12:19
  • Are these two different projects part of the same executable? I.e., is there one executable that happens to reference both projects (dll's), or is one the executable that happens to reference the other project? Can there be multiple copies of the program running simultaneously? What if there are multiple copies on different machines? You need to more precisely specify what you mean by "shared variable" and how the projects are related, and how they are executed. – Eamon Nerbonne Mar 02 '13 at 13:40
  • Actually there are 2 different exe's which are mutually exclusive which is decided by that shared variable – Rameshwar Pawale Mar 02 '13 at 13:57

4 Answers4

3

The MS documentation states about DLLs : "By default, each process using a DLL has its own instance of all the DLLs global and static variables.".

Take a look on this that give some workaround : http://msdn.microsoft.com/en-us/library/h90dkhs0(v=vs.80).aspx

BTw, I would share this values using network communications.

hamilton.lima
  • 1,902
  • 18
  • 29
3

If you have two processes (like two .exe files) that need to communicate, it's called inter process communication. There really is no easy way to explain because there are a million different ways to handle this.

  • You can write in files and read them.
  • You can connect the processes by network.
  • You can write and read to a common database.
  • You can use Message Queues.
  • You can use named pipes.
  • And probably another dozen ways I've missed...

You will need to reasearch them and find a solution that best fits your problem.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
0

You are trying to access the static member by two projects in the same program right? Not two separate programs.

If so, I think the misunderstanding you're having is between value types and reference types. You can update the shared variable from both projects, but those values won't propagate. I'll use a single class to demonstrate, rather than the two you're using.

static class Test
{
    public static bool logged_in;
}


Test.logged_in = true;
var t = Test.logged_in;
Console.WriteLine(l); // prints true
Test.logged_in = false;
var f = Test.logged_in;
Console.WriteLine(f); // prints false
Console.WriteLine(t); // prints true

Notice how the value of t wasn't updated when you changed the static member? That's because bool is a value type, not a reference type. So, when you ask for the value, you receive a copy of the value, not a reference to the variable.

If the static member was a reference type though, you can observe different behaviour:

static class Test
{
    public static string logged_in;
}


Test.logged_in = "true";
var t = Test.logged_in;
Console.WriteLine(l); // prints "true"
Test.logged_in = "false";
var f = Test.logged_in;
Console.WriteLine(f); // prints "false"
Console.WriteLine(t); // prints "false"
Josh Smeaton
  • 47,939
  • 24
  • 129
  • 164
-1

what about linked files (click on project -> add -> existing item -> (select you.cs) -> add as link)?

Horev Ivan
  • 270
  • 3
  • 9