Sharing variables between dlls

Posts   
 
    
Fishy avatar
Fishy
User
Posts: 392
Joined: 15-Apr-2004
# Posted on: 19-May-2005 18:04:02   

Hi all,

I have a solution that contains a windows application project with forms. Also, the solution has a dll which contains forms and is instatiated by the main windows project. That part works fine and the forms in the dll come up without any problems.

I'm getting myself a little confused on how I should setup global variables to be used between both project files.

My thoughts now is to create a GlobalVars dll that would be referenced in both project files.

Does this sound like a good idea or is there a better way to do it?

Thanks,

Fishy.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39750
Joined: 17-Aug-2003
# Posted on: 20-May-2005 11:48:15   

A global vars dll sounds ok: it makes sure you don't get cyclic references in your projects. Make sure you export readonly statics, not const statics.

Frans Bouma | Lead developer LLBLGen Pro
Fishy avatar
Fishy
User
Posts: 392
Joined: 15-Apr-2004
# Posted on: 20-May-2005 17:14:34   

Otis wrote:

Make sure you export readonly statics, not const statics.

Excuse my ignorance flushed , but what do you mean by that?

Thanks,

Fishy

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39750
Joined: 17-Aug-2003
# Posted on: 20-May-2005 17:50:57   

Fishy wrote:

Otis wrote:

Make sure you export readonly statics, not const statics.

Excuse my ignorance flushed , but what do you mean by that?

Thanks,

Fishy

If you do:


public const string Foo = "lala";

in assembly A and you use Foo in assembly B, the actual value 'lala' is compiled in assembly B. This means that if you change A, B will still use 'lala'.

if you do:


public static readonly string Foo = "lala";

for B it won't make a difference, but now, a reference to A.Foo is compiled, not 'lala'. This means that if A changes, B will pick up the change.

Frans Bouma | Lead developer LLBLGen Pro
Fishy avatar
Fishy
User
Posts: 392
Joined: 15-Apr-2004
# Posted on: 20-May-2005 18:03:20   

ahh, ok, that makes sense.

Thanks again for your quick response smile

swallace
User
Posts: 648
Joined: 18-Aug-2003
# Posted on: 20-May-2005 19:04:19   

in assembly A and you use Foo in assembly B, the actual value 'lala' is compiled in assembly B. This means that if you change A, B will still use 'lala'.

Facinating. I didn't know that. I'm guessing that knowledge was learned in a very difficult and painful debugging session.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39750
Joined: 17-Aug-2003
# Posted on: 20-May-2005 19:22:38   

Heh, luckily I read it somewhere and every time I type in 'const' I remember it and replace it with 'readonly...'. simple_smile It's the answer to the common question: "Why is there 'readonly' in C# while there's also const?

Frans Bouma | Lead developer LLBLGen Pro