473,385 Members | 1,472 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,385 software developers and data experts.

Correct OOP Behavior or Compiler Glitch?

This isn't a problem from the standpoint that its easy to work around.
However, I am very currious as to the correctness of this behavior.

I am developing an MDI application. In the application I have defined a
Toolbox class deriving from System.Windows.Forms.Form. I want only one
instance of this class to ever exist and for it to be displayable by calling
a static method of the class called Show(). To my surprise, my static
method is masking the instance (non-static) Show() method which the class
inherits from System.Windows.Forms.Form and the compiler warns me to declare
it with the 'new' modifier. Is this correct behavior? Can and should
static and non-static members mask each other? It would seem to me that
becuase reference to each are qualified differently (i.e. ClassName.Method()
vrs InstanceVar.Method()) the compiler can clearly distiguish which is being
sought and that there should never being any masking issues.

Below is an abreviated class definition for the Toolbox class that
exemplifies the issue.

public class Toolbox : System.Windows.Forms.Form
{
private static Toolbox StaticInstance = new Toolbox();

private Toolbox()
{
// Init code that builds the contents of the tool box
// from a config file.
}

//Why do I need the 'new' keyword here. Are not static and instance
members sufficiently
//distinct to be able to avoid name conflicts?
public static new void Show()
{
//Compiler Glitch? Need to cast this to a form, otherwise the
compiler thinks
//we are trying to reference a Toolbox's static Show method, i.e.
the
//method we are currently defining!
((Form)StaticInstance).Show();
}
}

--Ken Baltrinic
Nov 15 '05 #1
13 1334
Kenneth Baltrinic <ne**********@baltrinic.com> wrote:
This isn't a problem from the standpoint that its easy to work around.
However, I am very currious as to the correctness of this behavior.

I am developing an MDI application. In the application I have defined a
Toolbox class deriving from System.Windows.Forms.Form. I want only one
instance of this class to ever exist and for it to be displayable by calling
a static method of the class called Show(). To my surprise, my static
method is masking the instance (non-static) Show() method which the class
inherits from System.Windows.Forms.Form and the compiler warns me to declare
it with the 'new' modifier. Is this correct behavior? Can and should
static and non-static members mask each other? It would seem to me that
becuase reference to each are qualified differently (i.e. ClassName.Method()
vrs InstanceVar.Method()) the compiler can clearly distiguish which is being
sought and that there should never being any masking issues.


The C# compiler can, but others may not be able to. For instance, in
Java you can do:

Thread.currentThread().sleep(5000);

which makes it look like sleep is an instance method, when in fact it's
a static method. I'd imagine that users of J# could get confused by
your class.

I don't think it's a compiler error though - from section 10.7.1.2 of
the ECMA spec:

<quote>
A method introduced in a class or struct hides all non-method base
class members with the same name, and all base class methods with the
same signature (method name and parameter count, modifiers, and types).
</quote>

There's no mention of a static method only hiding other static methods
or an instance method only hiding other instance methods.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Kenneth,

On the surface, I would think that it is a bug as well, or rather, it
shouldn't give you that error. However, there is a gnawing feeling deep
within me that is telling me that the compiler is correct.

Based on what you are doing though, instead of exposing a Show method,
you really should be using a singleton pattern to expose the single
instance. Then, Show would be called on that instance.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Kenneth Baltrinic" <ne**********@baltrinic.com> wrote in message
news:Od**************@tk2msftngp13.phx.gbl...
This isn't a problem from the standpoint that its easy to work around.
However, I am very currious as to the correctness of this behavior.

I am developing an MDI application. In the application I have defined a
Toolbox class deriving from System.Windows.Forms.Form. I want only one
instance of this class to ever exist and for it to be displayable by calling a static method of the class called Show(). To my surprise, my static
method is masking the instance (non-static) Show() method which the class
inherits from System.Windows.Forms.Form and the compiler warns me to declare it with the 'new' modifier. Is this correct behavior? Can and should
static and non-static members mask each other? It would seem to me that
becuase reference to each are qualified differently (i.e. ClassName.Method() vrs InstanceVar.Method()) the compiler can clearly distiguish which is being sought and that there should never being any masking issues.

Below is an abreviated class definition for the Toolbox class that
exemplifies the issue.

public class Toolbox : System.Windows.Forms.Form
{
private static Toolbox StaticInstance = new Toolbox();

private Toolbox()
{
// Init code that builds the contents of the tool box
// from a config file.
}

//Why do I need the 'new' keyword here. Are not static and instance
members sufficiently
//distinct to be able to avoid name conflicts?
public static new void Show()
{
//Compiler Glitch? Need to cast this to a form, otherwise the
compiler thinks
//we are trying to reference a Toolbox's static Show method, i.e. the
//method we are currently defining!
((Form)StaticInstance).Show();
}
}

--Ken Baltrinic

Nov 15 '05 #3
maybe a singleton pattern would be a better choice?

class MyClass
{
//holds the only instance of a class
private static MyClass singleInstance=null;

//private constructor. You cant explicitly create an instance of this
class
private MyClass()
{
//constructor logic
}

//static public methot creating one and only one instance of an object
public static MyClass GiveMyClass()
{
//if not created, create
if(singleInstance==null)
singleInstance=new MyClass();
//return the only instance
return singleInstance;
}
}
--
"Zeglarstwo jest koniecznoscia
zycie nia nie jest"

www.saper.infra.pl/

Saper(ek)
Użytkownik "Kenneth Baltrinic" <ne**********@baltrinic.com> napisał w
wiadomo¶ci news:Od**************@tk2msftngp13.phx.gbl...
This isn't a problem from the standpoint that its easy to work around.
However, I am very currious as to the correctness of this behavior.

I am developing an MDI application. In the application I have defined a
Toolbox class deriving from System.Windows.Forms.Form. I want only one
instance of this class to ever exist and for it to be displayable by calling a static method of the class called Show(). To my surprise, my static
method is masking the instance (non-static) Show() method which the class
inherits from System.Windows.Forms.Form and the compiler warns me to declare it with the 'new' modifier. Is this correct behavior? Can and should
static and non-static members mask each other? It would seem to me that
becuase reference to each are qualified differently (i.e. ClassName.Method() vrs InstanceVar.Method()) the compiler can clearly distiguish which is being sought and that there should never being any masking issues.

Below is an abreviated class definition for the Toolbox class that
exemplifies the issue.

public class Toolbox : System.Windows.Forms.Form
{
private static Toolbox StaticInstance = new Toolbox();

private Toolbox()
{
// Init code that builds the contents of the tool box
// from a config file.
}

//Why do I need the 'new' keyword here. Are not static and instance
members sufficiently
//distinct to be able to avoid name conflicts?
public static new void Show()
{
//Compiler Glitch? Need to cast this to a form, otherwise the
compiler thinks
//we are trying to reference a Toolbox's static Show method, i.e. the
//method we are currently defining!
((Form)StaticInstance).Show();
}
}

--Ken Baltrinic

Nov 15 '05 #4
Saper(ek) <sa*****@tlen.pl> wrote:
maybe a singleton pattern would be a better choice?


Possibly, but preferrably with a thread-safe (and simpler)
implementation - see http://www.pobox.com/~skeet/csharp/singleton.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
with that implementation, what holds me from creating more than one
instance? nothing. constructor is public, so I can create as many instances
as I want...

thread sagety is another thing, but I was trying to keep it as simple as I
can.

--
"Zeglarstwo jest koniecznoscia
zycie nia nie jest"

www.saper.infra.pl/

Saper(ek)
Uzytkownik "Jon Skeet [C# MVP]" <sk***@pobox.com> napisal w wiadomosci
news:MP************************@msnews.microsoft.c om...
Saper(ek) <sa*****@tlen.pl> wrote:
maybe a singleton pattern would be a better choice?


Possibly, but preferrably with a thread-safe (and simpler)
implementation - see http://www.pobox.com/~skeet/csharp/singleton.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #6
Saper(ek) <sa*****@tlen.pl> wrote:
with that implementation, what holds me from creating more than one
instance? nothing. constructor is public, so I can create as many instances
as I want...
Which implementation is that? Each implementation on that page have a
private constructor, and no other constructors. If I've made a mistake
on the page, please point it out precisely.
thread sagety is another thing, but I was trying to keep it as simple as I
can.


In what way is your version simpler than:

public class Singleton
{
static Singleton instance=new Singleton();

Singleton()
{
}

public static Singleton GetInstance()
{
return instance;
}
}

?

(You can optionally have a static constructor in there which just
prevents the compiler from adding the beforefieldinit flag, too.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
Why not implement the Singleton as
// .NET Singleton
sealed class Singleton
{
private Singleton() {}
public static readonly Singleton Instance = new Singleton();
}
mucho simpler :D

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Saper(ek) <sa*****@tlen.pl> wrote:
with that implementation, what holds me from creating more than one
instance? nothing. constructor is public, so I can create as many instances as I want...


Which implementation is that? Each implementation on that page have a
private constructor, and no other constructors. If I've made a mistake
on the page, please point it out precisely.
thread sagety is another thing, but I was trying to keep it as simple as I can.


In what way is your version simpler than:

public class Singleton
{
static Singleton instance=new Singleton();

Singleton()
{
}

public static Singleton GetInstance()
{
return instance;
}
}

?

(You can optionally have a static constructor in there which just
prevents the compiler from adding the beforefieldinit flag, too.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #8
Cant get simpler than that :D Static initialisztion is guranteed thread
safe by the runtime. It fixes old workworunds (that you do with locking in
non managed code) on other runtimes which dont have such gurantees.

readonly means its set once and only once.

Here you have both features of the singleton design pattern.
1. Well known access point to the instance (via static Instance method)
2. one guranteed instance and no more. (via readonly)

So, you would call the above with the static interface (the wellknown access
point) Singleton.Instance.SomeFeatureHereBlahSmerdyHerdyB orkBork

How simpler do you want it?

"news.microsoft.com" <di********@discussion.microsoft.com> wrote in message
news:u9**************@TK2MSFTNGP09.phx.gbl...
Why not implement the Singleton as
// .NET Singleton
sealed class Singleton
{
private Singleton() {}
public static readonly Singleton Instance = new Singleton();
}
mucho simpler :D

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Saper(ek) <sa*****@tlen.pl> wrote:
with that implementation, what holds me from creating more than one
instance? nothing. constructor is public, so I can create as many instances as I want...
Which implementation is that? Each implementation on that page have a
private constructor, and no other constructors. If I've made a mistake
on the page, please point it out precisely.
thread sagety is another thing, but I was trying to keep it as simple
as I can.


In what way is your version simpler than:

public class Singleton
{
static Singleton instance=new Singleton();

Singleton()
{
}

public static Singleton GetInstance()
{
return instance;
}
}

?

(You can optionally have a static constructor in there which just
prevents the compiler from adding the beforefieldinit flag, too.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 15 '05 #9
news.microsoft.com <di********@discussion.microsoft.com> wrote:
Why not implement the Singleton as
// .NET Singleton
sealed class Singleton
{
private Singleton() {}
public static readonly Singleton Instance = new Singleton();
} mucho simpler :D


Yes, that is another option too. (You *can* get simpler in terms of the
code, by getting rid of the private modifier, but that's a different
discussion.)

As I write on my page:

<quote>
Note that all of these implementations also use a public static method
GetInstance as the means of accessing the instance. In all cases, the
method could easily be converted to a property with only an accessor,
with no impact on thread-safety or performance.
</quote>

Not all of them can be converted to public readonly variables (the
fully lazy instantiation one, which allows other static methods to be
called without the singleton itself being created) but I think I'd
generally prefer to leave it as a property or a method anyway, just in
case the implementation needs to become smarter later on.

I shall fix the page to use readonly variables where possible, and to
make the class explicitly sealed, both of which are good things to do.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #10
As Jon mentioned, you don't get lazy creation this way. That's fine if you
are sure you want your instance to be made as soon as the JIT gets to your
class. But in interests of performance, using a method or a property will
delay instantiation until the object is actually needed.

Niall

"news.microsoft.com" <di********@discussion.microsoft.com> wrote in message
news:u9**************@TK2MSFTNGP09.phx.gbl...
Why not implement the Singleton as
// .NET Singleton
sealed class Singleton
{
private Singleton() {}
public static readonly Singleton Instance = new Singleton();
}
mucho simpler :D

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Saper(ek) <sa*****@tlen.pl> wrote:
with that implementation, what holds me from creating more than one
instance? nothing. constructor is public, so I can create as many instances as I want...
Which implementation is that? Each implementation on that page have a
private constructor, and no other constructors. If I've made a mistake
on the page, please point it out precisely.
thread sagety is another thing, but I was trying to keep it as simple
as I can.


In what way is your version simpler than:

public class Singleton
{
static Singleton instance=new Singleton();

Singleton()
{
}

public static Singleton GetInstance()
{
return instance;
}
}

?

(You can optionally have a static constructor in there which just
prevents the compiler from adding the beforefieldinit flag, too.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 15 '05 #11
Niall <as**@me.com> wrote:
As Jon mentioned, you don't get lazy creation this way. That's fine if you
are sure you want your instance to be made as soon as the JIT gets to your
class. But in interests of performance, using a method or a property will
delay instantiation until the object is actually needed.


In fact, it *doesn't* make sure that your instance is made as soon as
the JIT gets to your class - the JIT can choose to run the type
initializer of a class with beforefieldinit *later* than it would under
other circumstances. For instance, it could not bother running it at
all, ever, if the class's static fields are never touched - even if the
class is instantiated! It's all very odd...

(In practice, I'm sure it *does* always mean it's initialized earlier -
I'm only talking in theory here.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #12
Hmm, sounds like the same kind of mysterious workings as what makes the GC
decide to run... Maybe I should do some research and write a book:
"Mysteries Revealed: The Secrets of the Pyramids, Inner Happiness and .Net
Internals".

Niall

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Niall <as**@me.com> wrote:
As Jon mentioned, you don't get lazy creation this way. That's fine if you are sure you want your instance to be made as soon as the JIT gets to your class. But in interests of performance, using a method or a property will delay instantiation until the object is actually needed.


In fact, it *doesn't* make sure that your instance is made as soon as
the JIT gets to your class - the JIT can choose to run the type
initializer of a class with beforefieldinit *later* than it would under
other circumstances. For instance, it could not bother running it at
all, ever, if the class's static fields are never touched - even if the
class is instantiated! It's all very odd...

(In practice, I'm sure it *does* always mean it's initialized earlier -
I'm only talking in theory here.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #13
Second try. This of course is not the Singleton pattern described by the
Gang of Four since it is missing a getter and hence a level of
indirection.
The Singleton pattern supports flexibility of design so that you can
change the design to support "a variable number of instances."

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #14

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

Similar topics

11
by: Ted Mayett | last post by:
OK. Here is a glitch, sorry if this has been mentioned before. This is an erratic glitch. I am now up to three other people besides myself who have been able to see this glitch. It seems it...
19
by: E. Robert Tisdale | last post by:
In the context of the comp.lang.c newsgroup, the term "undefined behavior" actually refers to behavior not defined by the ANSI/ISO C 9 standard. Specifically, it is *not* true that "anything can...
23
by: Ken Turkowski | last post by:
The construct (void*)(((long)ptr + 3) & ~3) worked well until now to enforce alignment of the pointer to long boundaries. However, now VC++ warns about it, undoubtedly to help things work on 64...
6
by: Rob Thorpe | last post by:
Given the code:- r = sscanf (s, "%lf", x); What is the correct output if the string s is simply "-" ? If "-" is considered the beginning of a number, that has been cut-short then the...
13
by: olanglois | last post by:
Hi, I am trying to derive a new class that will add new functions but no new data members and the base class has overloaded operators (+,-,+=,-=,etc...) returning either (Base &) or (const Base)...
24
by: temper3243 | last post by:
Hi, Many people have used this code in my project. It works because b is using the extra memory for other 4 variables.I access obj max. well here are a few questions 1) Where does it fail. I...
6
by: Generic Usenet Account | last post by:
I ran a small experiment involving RTTI and dynamic casting. Basically what I did was to cast a base class pointer to a derived type (yes, I know that is not kosher). I then performed...
46
by: aarklon | last post by:
Hi, the following is actually a part of the pattern matching program which i tried ,memset is not setting the entire integer array with -1 #include <string.h> #include <stdio.h> #include...
33
by: coolguyaroundyou | last post by:
Will the following statement invoke undefined behavior : a^=b,b^=a,a^=b ; given that a and b are of int-type ?? Be cautious, I have not written a^=b^=a^=b ; which, of course, is undefined....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.