473,714 Members | 2,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Metho d()
vrs InstanceVar.Met hod()) 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)StaticIn stance).Show();
}
}

--Ken Baltrinic
Nov 15 '05 #1
13 1348
Kenneth Baltrinic <ne**********@b altrinic.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.Metho d()
vrs InstanceVar.Met hod()) 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.currentT hread().sleep(5 000);

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.co m>
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.co m

"Kenneth Baltrinic" <ne**********@b altrinic.com> wrote in message
news:Od******** ******@tk2msftn gp13.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.Metho d() vrs InstanceVar.Met hod()) 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)StaticIn stance).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(singleInstan ce==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**********@b altrinic.com> napisa³ w
wiadomo¶ci news:Od******** ******@tk2msftn gp13.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.Metho d() vrs InstanceVar.Met hod()) 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)StaticIn stance).Show();
}
}

--Ken Baltrinic

Nov 15 '05 #4
Saper(ek) <sa*****@tlen.p l> 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.co m>
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.co m> napisal w wiadomosci
news:MP******** *************** *@msnews.micros oft.com...
Saper(ek) <sa*****@tlen.p l> 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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #6
Saper(ek) <sa*****@tlen.p l> 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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Saper(ek) <sa*****@tlen.p l> 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.co m>
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.Insta nce.SomeFeature HereBlahSmerdyH erdyBorkBork

How simpler do you want it?

"news.microsoft .com" <di********@dis cussion.microso ft.com> wrote in message
news:u9******** ******@TK2MSFTN GP09.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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Saper(ek) <sa*****@tlen.p l> 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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 15 '05 #9
news.microsoft. com <di********@dis cussion.microso ft.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #10

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

Similar topics

11
3006
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 only happens in IE. And the <hr> causes this thing to happen. Now, I have ~tried~ to make this glitch, and that is tough to do. I had did it that one time, and I should have saved the work. But I didn't save the work :(
19
2575
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 happen" if your C code invokes "undefined behavior". Behavior not defined by the ANSI/ISO C 9 standard may be defined by some other standard (i.e. POSIX) or it may be defined by your compiler, your operating system or your machine architecture.
23
3207
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 bit machines, i.e. with 64 bit pointers. In the early days of C, where there were problems with the size of int being 16 or 32 bits, the response was that an int was guaranteed to hold a pointer (yes, there were 64Kb address spaces at one time!)....
6
3486
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 correct output is that r = EOF. If it is taken to be a letter in the stream, then the output should be r = 0, as far as I can see. My compiler gives EOF.
13
2002
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) depending on the operator: class Derived : public Base { };
24
1826
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 mean is there any exception where it willnot work assuming malloc works fine. 2) someone told me in C99 we have declar b obj , so that it can be declared at runtime. How do we do that. Do we have to malloc again .
6
1905
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 dynamic_casting and I invoked RTTI. In both instances, the run time environment did not pick up the fact that what I was claiming to be a derived pointer was in fact the base pointer. However, when I invoked a virtual method using the ostensibly derived...
46
1844
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 <stdlib.h>
33
2831
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. I am having some confusion with the former statement! Also, state the reason for the statement being undefined!
0
8707
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9174
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9074
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9015
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7953
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4725
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3158
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2110
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.