Question: Best place to put global variables on a VS.NET C# project 
Author Message
 Question: Best place to put global variables on a VS.NET C# project

Greetings
On my C# application I need to have several global variables (including
constants (const)), in way to any form and any class can see them and
change them (when possible). What is the best way to do this?
Is there any samples?
Thanks

Srgio Almeida



Fri, 15 Apr 2005 23:53:22 GMT  
 Question: Best place to put global variables on a VS.NET C# project
Create a class where these variables are
static.

Olav

Quote:
>-----Original Message-----
>Greetings
>On my C# application I need to have several global

variables (including
Quote:
>constants (const)), in way to any form and any class can
see them and
>change them (when possible). What is the best way to do

this?


Sat, 16 Apr 2005 00:17:46 GMT  
 Question: Best place to put global variables on a VS.NET C# project
Srgio,

    By default, const fields are static, and they are not variables, they
are constant.

    Basically, declare the variables that are constant as const, and the
ones that can change as static.  If you need something that you can only get
externally but set internally, then set a static field that is internal and
then have a public property that gets that field.  Then, anything in your
assembly can access that field, but external assemblies can only get the
value through the property accessor.

    Hope this helps.

--
               - Nicholas Paldino [.NET/C# MVP]


Quote:
> Thank you Olav but as far as I know (correct me if I'm wrong) const
> "variables" cannot be static. This is my problem ;))
> thank you

> Srgio Almeida


> > Create a class where these variables are
> > static.

> > Olav

> >>-----Original Message-----
> >>Greetings
> >>On my C# application I need to have several global

> > variables (including

> >>constants (const)), in way to any form and any class can

> > see them and

> >>change them (when possible). What is the best way to do

> > this?



Sat, 16 Apr 2005 00:47:26 GMT  
 Question: Best place to put global variables on a VS.NET C# project
Different: const are automatically static. As such, you dont need the static
keyword.

Now the funny part: thats documented in the language specifications.

--
Regards

Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)
--
Still writing SQL and dealing with DataSets?
  Why dont you use our EntityBroker - the most advanced o/r and business
object toolkit in town.


Quote:
> Thank you Olav but as far as I know (correct me if I'm wrong) const
> "variables" cannot be static. This is my problem ;))
> thank you

> Srgio Almeida


> > Create a class where these variables are
> > static.

> > Olav

> >>-----Original Message-----
> >>Greetings
> >>On my C# application I need to have several global

> > variables (including

> >>constants (const)), in way to any form and any class can

> > see them and

> >>change them (when possible). What is the best way to do

> > this?



Sat, 16 Apr 2005 00:54:33 GMT  
 Question: Best place to put global variables on a VS.NET C# project

Quote:

> Thank you Olav but as far as I know (correct me if I'm wrong) const
> "variables" cannot be static. This is my problem ;))

They can't be *declared* static because they're *implicitly* static.
From the C# spec:

"Even though constants are considered static members, a constant-
declaration neither requires nor allows a static modifier."

--

http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too



Sat, 16 Apr 2005 01:28:37 GMT  
 Question: Best place to put global variables on a VS.NET C# project

Quote:
>-----Original Message-----

>> >"variables" cannot be static.=20
>> A C# "const" is always static.

>> you can use "static readonly" for expressions that=20
>> cant be declared const (this might be what you mean
>> with "const variable"?).

>No, there's a different between static readonly and const.

I am refering to that it is some restrictions to what
you can put in a const.
//if you have
const string a = "hello";
//then
const len = a.Length;
//doesnt compile.

//though you can have
static readonly len = a.Length;
//this is also a constant though not
//in C# syntax, in Turbo Pascal I think
//this would correspond to a constant variable

Olav



Sat, 16 Apr 2005 03:37:17 GMT  
 Question: Best place to put global variables on a VS.NET C# project
Thank you Olav but as far as I know (correct me if I'm wrong) const
"variables" cannot be static. This is my problem ;))
thank you

Srgio Almeida

Quote:

> Create a class where these variables are
> static.

> Olav

>>-----Original Message-----
>>Greetings
>>On my C# application I need to have several global

> variables (including

>>constants (const)), in way to any form and any class can

> see them and

>>change them (when possible). What is the best way to do

> this?



Sat, 16 Apr 2005 00:25:02 GMT  
 Question: Best place to put global variables on a VS.NET C# project

Quote:

> >"variables" cannot be static.=20
> A C# "const" is always static.

> you can use "static readonly" for expressions that=20
> cant be declared const (this might be what you mean
> with "const variable"?).

No, there's a different between static readonly and const.

Consider the following program:

using System;

public class Foo
{
    public const I=5;
    public static readonly J=6;

Quote:
}

public class Bar
{
    public static void Main()
    {
        Console.WriteLine (Foo.I);
        Console.WriteLine (Foo.J);
    }

Quote:
}

The result will be 5, 6 as expected. Now change the 5 and 6 from Foo to
10 and 11, and recompile *just Foo* - the result will be 5, 11. Constants
are placed directly in the calling class, whereas static readonly values
are read from the declaring class itself at runtime.

--
Jon Skeet

If replying to the group, please do not mail me at the same time



Sat, 16 Apr 2005 03:03:56 GMT  
 Question: Best place to put global variables on a VS.NET C# project

Quote:
>-----Original Message-----
>Thank you Olav but as far as I know (correct me if I'm
wrong) const
>"variables" cannot be static.

A C# "const" is always static.

you can use "static readonly" for expressions that
cant be declared const (this might be what you mean
with "const variable"?).

You can also have static properties, perhaps
in combination with a Singleton-setup.

Olav

This is my problem ;))

Quote:
>thank you

>Srgio Almeida


>> Create a class where these variables are
>> static.

>> Olav

>>>-----Original Message-----
>>>Greetings
>>>On my C# application I need to have several global

>> variables (including

>>>constants (const)), in way to any form and any class
can

>> see them and

>>>change them (when possible). What is the best way to do

>> this?

>.



Sat, 16 Apr 2005 02:31:56 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Changes in C# between VS.NET Beta and VS.NET Final versions

2. VS for C# is less user Friendly then for VB.net projects

3. VS.NET: Adding one .cs file to more then one project

4. Unsupported options in C# projects under VS.NET

5. static local variables vs global variables

6. Best place to put a startup dialog box

7. where's a good place to put this code

8. How can I put a global environment variable?

9. Where do I put global variables?

10. where to put global variables

11. Global variable or not global variable

12. C# vs C++ .NET performance question

 

 
Powered by phpBB® Forum Software