473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

static / new / singleton

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.
Nov 15 '05 #1
8 1465
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 Paldino [.NET/C# MVP]
- nick(dot)paldino=at=exisconsulting<dot>com

"Cybertof" <cy****************@gmx.net> wrote in message
news:MP************************@msnews.microsoft.c om...
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.

Nov 15 '05 #2
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.


In article <#F**************@TK2MSFTNGP12.phx.gbl>,
ni**************@exisconsulting.com says...
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.

Nov 15 '05 #3
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" <cy****************@gmx.net> wrote in message
news:MP************************@msnews.microsoft.c om...
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.


In article <#F**************@TK2MSFTNGP12.phx.gbl>,
ni**************@exisconsulting.com says...
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.

Nov 15 '05 #4
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.
In article <#t**************@tk2msftngp13.phx.gbl>,
ni**************@exisconsulting.com says...
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).

Nov 15 '05 #5
Nicholas, do you have an opinion on my last message ?

Thanks.

Christophe.

Nov 15 '05 #6
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.
--
- Nicholas Paldino [.NET/C# MVP]
- nick(dot)paldino=at=exisconsulting<dot>com

"Cybertof" <cy****************@gmx.net> wrote in message
news:MP************************@msnews.microsoft.c om...
Nicholas, do you have an opinion on my last message ?

Thanks.

Christophe.

Nov 15 '05 #7
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.
In article <eq**************@TK2MSFTNGP12.phx.gbl>,
ni**************@exisconsulting.com says...
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.

Nov 15 '05 #8
twt
try a class (call it Global) and make all its field static (for changable
fields) or const (for constant ones).

=== twt

"Cybertof" <cy****************@gmx.net> wrote in message
news:MP************************@msnews.microsoft.c om...
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.
In article <eq**************@TK2MSFTNGP12.phx.gbl>,
ni**************@exisconsulting.com says...
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.

Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: cppaddict | last post by:
I would like to know the best way to initialize complex static member variables. In addition, I want to avoid creating an Init() method that is called by the ctor, since there's no need to wait...
4
by: baumann | last post by:
hi all, according the private / protected access control, - private; that is, its name can be used only by members and friends of the class in which it is declared. - protected; that is,...
9
by: Joanna Carter \(TeamB\) | last post by:
Following on from the other discussion, I have to just check something out with reference to disposal of resources held in static fields. I have a Persistence Framework that is 'globally...
3
by: Diebels | last post by:
Hi, I have some problems using static variables which results in a core dump. I have attached code and coredump to the end of my message. I am trying to implement a kind of factory design. I...
6
by: dewdman42 | last post by:
I have a question for you C++ gurus. Let's say I have a class singleton class such as: class singleton {
7
by: John A Grandy | last post by:
For a singleton class utilizes by ASP.NET 2.0 page processing: When initial instantiation is performed during the initial call to the retrieve instance method (let's call the method...
9
by: wizwx | last post by:
There are two typical implementations of a singleton. The first one is to use a static pointer class Singleton { static Singleton * pSingleton; public: static Singleton * instance() {...
7
by: daniel | last post by:
Hello , I have the following code , which implements the singleton pattern: class Singleton{ private: static Singleton* uniqueInstance; //other useful instance variables here Singleton(){
4
by: John Doe | last post by:
Hi, I have a singleton class defined like this : class UIManager : public CSingleton<UIManager>, public CObject { protected: DECLARE_DYNAMIC(UIManager) friend class CSingleton<UIManager>;
5
by: Andy B | last post by:
I have a class that I want to make static but it uses some objects that are instance objects. I keep getting a compiler error saying something about using instance objects in a static class or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.