K
Kenneth Baltrinic
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
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