static / new / singleton

  • Thread starter Thread starter Cybertof
  • Start date Start date
C

Cybertof

Hello,

I have a simple question :

In the below code, why do i get an (ERROR) saying
"An object reference is required for the nonstatic field, method, or
property 'mLockeEnv3'"

I don't understand why mLockeEnv3 should be static.....

Also, what is the difference betwenn mLockeEnv & mLockeEnv2 as both
declarations are working....


public class CLockeMain
{
public static CLockeEnv mlockeEnv;
public static CLockeEnv mlockeEnv2 = new CLockeEnv();
public CLockeEnv mLockeEnv3;

static void Main()
{
mLockeEnv = CLockeEnv.GetInstance();
mLockeEnv2 = CLockeEnv.GetInstance();
mLockeEnv3 = CLockeEnv.GetInstance(); (ERROR)
}
}



Note :
CLockeEnv is a singleton class with an internal private member pointing
to itsel. GetInstance creates the object or returns the existing
instance.
 
Cybertof,

When you declare a field without the static modifier, that field is
accessible only to the instance. Because you are making the assignment in a
static method, the static method doesn't know which instance to set the
field to, so it must be static.

Hope this helps.
 
I understand what you mean.

Why are generated winforms have a 'static' Main ?

Most of the time declared like this :
//[STAThread]
static void Main()
{...}

What are the consequences to remove the [STAThread] ?
What are the consequences to remove the static keyword in Winforms
generated code ?


Regards,
Christophe.
 
Cybertof,

If you remove the STAThread attribute, then some of your windows forms
controls might not work. The attribute is a way of indicating to the
runtime that this thread is a single-threaded apartment thread, meaning that
all access is synchronized (through the windows message loop). Some windows
forms controls which are COM controls require this.

If you remove the static keyword from the Main method, then your code
will not run. When the runtime looks through your code to execute, it
doesn't have any instances of any of your objects. The only thing it can
call to begin a program is a static method, which is on a type, which it can
locate (and not have to create like an instance).


--
- Nicholas Paldino [.NET/C# MVP]
- nick(dot)paldino=at=exisconsulting<dot>com

Cybertof said:
I understand what you mean.

Why are generated winforms have a 'static' Main ?

Most of the time declared like this :
//[STAThread]
static void Main()
{...}

What are the consequences to remove the [STAThread] ?
What are the consequences to remove the static keyword in Winforms
generated code ?


Regards,
Christophe.




Cybertof,

When you declare a field without the static modifier, that field is
accessible only to the instance. Because you are making the assignment in a
static method, the static method doesn't know which instance to set the
field to, so it must be static.

Hope this helps.
 
Nicholas,

Thanks for your explanation.
So, if i resume, I cannot declare in my main class a public instance of
a class and access it directly from the static main proc (for example to
instanciate it).
If i want to do this, i have to declare my public class to be static.
Or maybe I instanciate a new class beeing declared within the main proc
(and not in the class body) but then i cannot access to it from an
external class (as it is a member of a proc and not a class).

Are there any good solutions ?

Also, I declare a public class Class1 in a source1.cs file, this Class1
is beeing instancied somewhere.
How can i have access to it from another class (in another source2.cs
file) for example ? Do i need to call a method that returns me a
reference to an existing instance (so this is the singleton method) or
is there another solution (not to use static auto-referencing
'singleton' classes) ?


Regards,
Christophe.
 
Cybertof,

I am not quite sure what you want to do. You can have ANY class's Main
method be the entry point. From there, you can create any instances of any
public classes that you wish, and that is where everything begins. If you
want separate instances of classes to be aware of each other, then you have
to give them access to the reference, whether by passing it through a
method, or setting a property/field, or even setting it in a static field
somewhere where the other instance can get at it. No matter what, the
responsibility is on you to make it accessible to the other instances.
 
In fact, i'm just wondering where to put global variables in my
application code that would be accessible from various classes that may
be created in the application.
The only way i have in mind now is to make a static singleton class that
would return a same reference to itself each time a class asks for it.
Or maybe a global static struct...
Are there other ways to do this (global variable storage) ?


Thanks.
Cybertof.
 
try a class (call it Global) and make all its field static (for changable
fields) or const (for constant ones).

=== twt
 
Back
Top