Shared AND Overridable <-- Why not?!

  • Thread starter Thread starter Frank Osterberg
  • Start date Start date
F

Frank Osterberg

I want to make some shared methds overridable but dotNet will not allow me
to do so.

Why not? What possible reason could there be to not allow that?

Yes, i know i can make a sigelton and wrap shared methods, but that is just
moronic considering that a the shared members are just references to special
forms of a 'singelton' instance, or am i wrong here?

Is it just one of those 'it not BEST programming practices = stick up the
a**' reasons, like ALWAYS having to EXPLICITLY type cast EVERYTHING in some
stupid languages like java?
You know, the sort of reasons that just make you hate a programming
language?

Or will it work with .Net 2008?

Or maybe there IS a better way to do what i want to do:
Have a class with a name like SystemDB with shared members.
It will coordinate all calls from different modules/threads with one (or
two) database connections so that i can do something like:

For Each oRecord As DataRecord in SystemDB.Query("select * from dual;")
' etc
Next

or

SystemDB.InsertAsync("insert into....")

without having to care/worry about which thread i am using or what my local
instance reference was called.

I am already using it throughout my code, but now i would like to make it
extensible, so that i can have one base class called GlobalDB that must be
inherited but wher different protected methods can be overridden.

So considering that, is there a better way? I definitely do not want to have
any additional prefixes like:
otherClass.SystemDB.Something
since that makes the whole thing ugly again..

Thanks for any help/comment/info/suggestion,

Regards,

Frank
 
You just don't need it - 'Overrides' is only meaningful for instance methods
anyway - just use 'Overloads' (you actually don't even need that if you can
accept the warning):
Class one
Public Shared Sub test()
MsgBox("in base class 'test'")
End Sub
End Class
Class two
Inherits one
Public Overloads Shared Sub test()
MsgBox("in derived class 'test'")
End Sub
End Class
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
Java to C#
Java to VB
Instant C#: convert VB to C#
Instant VB: convert C# to VB
Instant C++: VB, C#, or Java to C++/CLI
 
Frank Osterberg said:
I want to make some shared methds overridable but dotNet will not allow me
to do so.

Why not? What possible reason could there be to not allow that?

Yes, i know i can make a sigelton and wrap shared methods, but that is
just moronic considering that a the shared members are just references to
special forms of a 'singelton' instance, or am i wrong here?

Is it just one of those 'it not BEST programming practices = stick up the
a**' reasons, like ALWAYS having to EXPLICITLY type cast EVERYTHING in
some stupid languages like java?
You know, the sort of reasons that just make you hate a programming
language?

Or will it work with .Net 2008?

Or maybe there IS a better way to do what i want to do:
Have a class with a name like SystemDB with shared members.
It will coordinate all calls from different modules/threads with one (or
two) database connections so that i can do something like:

For Each oRecord As DataRecord in SystemDB.Query("select * from dual;")
' etc
Next

or

SystemDB.InsertAsync("insert into....")

without having to care/worry about which thread i am using or what my
local instance reference was called.

I am already using it throughout my code, but now i would like to make it
extensible, so that i can have one base class called GlobalDB that must be
inherited but wher different protected methods can be overridden.

So considering that, is there a better way? I definitely do not want to
have any additional prefixes like:
otherClass.SystemDB.Something
since that makes the whole thing ugly again..

Thanks for any help/comment/info/suggestion,

Regards,

Frank

To understand why not, you first must understand what a static/shared method
is. A static (Shared in Visual Basic) method is a method that belongs to
the class itself, not to any instances of a class. Visual Basic went a
little further and allows you to call a static method by using an instance
variable instead of using the class name directly. Because it belongs to
the class and not any particular instance, static members/methods are not
inherited in derived classes. Therefore, when a member/method is not
inherited, it cannot be overridden. This has nothing to do with the design
of any particular language, but rather the framework itself. None of the
other languages should/will allow you to perform overriding a static
member/method either.

What you can do is create a Singleton class that contains the methods you
want to make public. You may then derive other singleton's from this base
class. Just make sure they aren't Shared on there or you'll run into the
same problem (and defeat the purpose of using a Singleton in the first
place).

HTH,
Mythran
 
Back
Top