473,326 Members | 2,076 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,326 software developers and data experts.

"inherit" from a static class

Hi I have a static class written by other team which encapsulates a
database instance. but I need to extend it to incldue other things. I
know that C# static class is sealed and can;t be inherited & I don't
want to copy + paste code. How can I inherit those member variables?
Thanks

May 22 '07 #1
13 20721
HI,

You could wrap the class in another class of yours

Other than that I see no other way.

"learning" <ed**********@gmail.comwrote in message
news:11*********************@z28g2000prd.googlegro ups.com...
Hi I have a static class written by other team which encapsulates a
database instance. but I need to extend it to incldue other things. I
know that C# static class is sealed and can;t be inherited & I don't
want to copy + paste code. How can I inherit those member variables?
Thanks

May 22 '07 #2
"learning" <ed**********@gmail.comwrote in message
news:11*********************@z28g2000prd.googlegro ups.com...
Hi I have a static class written by other team which encapsulates a
database instance. but I need to extend it to incldue other things. I
know that C# static class is sealed and can;t be inherited & I don't
want to copy + paste code. How can I inherit those member variables?
Thanks
Call them and ask them to make a version that is not sealed and tell them
what you are doing. Either they will see why their class should be
inheritable or tell you that what you are doing you should do some other way
;)

--
LTP

:)
May 22 '07 #3
"learning" <ed**********@gmail.comschrieb im Newsbeitrag
news:11*********************@z28g2000prd.googlegro ups.com...
Hi I have a static class written by other team which encapsulates a
database instance. but I need to extend it to incldue other things. I
know that C# static class is sealed and can;t be inherited & I don't
want to copy + paste code. How can I inherit those member variables?
Thanks
Just write another static class and add the extensions there. Since static
members can't be virtual, there is no need to overwrite at all.

Christof
May 22 '07 #4
The way i see it is that the static class you are inheriting should adopt a
singleton pattern if there has to be only one instance of that class. If you
use the singleton pattern the class can either be used as a single instance
or the class can be used as multiple instances and can also be inherited, I
recomend you do this.

The only way i can think to inherite the class without changing the class is
to incapuslation the static class. This will mean you can only access the
public methods, however to gain access to the protected methods which I DO
NOT recommend, (it is a hack for a badly designed class) is that you can use
reflection to call the protected methods (I some times used this techquie in
unit testing my protected functions to gain full coverage).

"Christof Nordiek" wrote:
"learning" <ed**********@gmail.comschrieb im Newsbeitrag
news:11*********************@z28g2000prd.googlegro ups.com...
Hi I have a static class written by other team which encapsulates a
database instance. but I need to extend it to incldue other things. I
know that C# static class is sealed and can;t be inherited & I don't
want to copy + paste code. How can I inherit those member variables?
Thanks

Just write another static class and add the extensions there. Since static
members can't be virtual, there is no need to overwrite at all.

Christof
May 22 '07 #5
On May 22, 12:30 pm, burlo <b...@discussions.microsoft.comwrote:
The way i see it is that the static class you are inheriting should adopt a
singleton pattern if there has to be only one instance of that class.
By definition, there are no instances of a static class. There are no
constructors to create instances with.
If you
use the singleton pattern the class can either be used as a single instance
or the class can be used as multiple instances and can also be inherited, I
recomend you do this.
The singleton pattern tends to prevent derived classes from being
created, as there should only be a private constructor in the
singleton class. The exception here is that you could have derived
nested types, which have access to the private constructor.
The only way i can think to inherite the class without changing the class is
to incapuslation the static class. This will mean you can only access the
public methods, however to gain access to the protected methods which I DO
NOT recommend, (it is a hack for a badly designed class) is that you can use
reflection to call the protected methods (I some times used this techquie in
unit testing my protected functions to gain full coverage).
Protected members aren't allowed in static classes anyway.

Jon

May 22 '07 #6
Ignacio Machin ( .NET/ C# MVP ) wrote:
You could wrap the class in another class of yours
That works for a class that isn't static, but as you can't create
instances of a static class, you can't wrap it inside another class.

--
Göran Andersson
_____
http://www.guffa.com
May 22 '07 #7


On May 21, 11:38 pm, "Christof Nordiek" <c...@nospam.dewrote:
"learning" <edwardt.t...@gmail.comschrieb im Newsbeitragnews:11*********************@z28g2000pr d.googlegroups.com...
Hi I have a static class written by other team which encapsulates a
database instance. but I need to extend it to incldue other things. I
know that C# static class is sealed and can;t be inherited & I don't
want to copy + paste code. How can I inherit those member variables?
Thanks

Just write another static class and add the extensions there. Since static
members can't be virtual, there is no need to overwrite at all.

Christof
So but by doing that does it mean like I need to replciate all the
memebers,a nd just assign each of the replicated members to have value
same as the statiac class.. how can I sheild myself that if there is
any additona/deletion go teh satic member variables of the original
static class, i also need to keep in sync with that in my static
class.. is there a way that it can be automatic?

And I jsut need to worry about my own extension?

thanks
May 22 '07 #8
On Tue, 22 May 2007 11:38:24 -0700, learning <ed**********@gmail.com>
wrote:
So but by doing that does it mean like I need to replciate all the
memebers,a nd just assign each of the replicated members to have value
same as the statiac class.. how can I sheild myself that if there is
any additona/deletion go teh satic member variables of the original
static class, i also need to keep in sync with that in my static
class.. is there a way that it can be automatic?

And I jsut need to worry about my own extension?
I've been following this thread a bit, and still haven't figured out
exactly what it is you are trying to do. That is, *why* you feel it would
be useful to inherit a new static class from an existing static class.

Inheritance is useful in situations where you have an instance, because
you can pass that instance around without always needing to know that it's
the more-derived version. It's nice that you can inherit behavior from a
base class, but the real power if you will comes from being able to have a
derived class passed to something that only knows the base class and still
have the inherited methods available, as well as being able to override
virtual methods (which allow the derived instance to change the behavior
of a method defined in the base instance, again without the caller
necessarily needing to know of the derived instance).

But those benefits rely on there being an actual instance of the class.
There's no instance of a static class, and so you can't pass an instance
of a derived static class to something that expects the base class. In
all cases of using a static class, the caller of the static class always
has to know which static class they are dealing with.

What does this mean in context of your question? Well, as Christof has
pointed out, there's no need to copy and paste code. Wherever you want to
use the functionality from the "base" static class, the caller will
specify that class, and wherever you want to use the functionality from
the "derived" static class, the caller will specify that class instead.

There is never any situation in which you are accessing a member of a
static class in which you don't specify the type name of that static
class, so there's really no point in wanting a method in some "base"
static class to magically show up some "derived" static class. If you
have new methods not contained in the "base" static class, just put them
in a new static class separate from the original static class, and then
call them directly from that class instead.

(And note that if you are concerned that your new static class won't have
access to hidden members of the "base" static class, members that you
would ordinarily have used "protected" access modifiers with, you can make
those members "internal" and your new static class will have access as
long as it's in the same assembly).

Pete
May 22 '07 #9

Not true. When calling static members from within the same class you
do not specify the name of the class, you just call the method. The
same is true for statics from an inherited class.

log4net takes advantage of this by providing an AssertionHelper class
which can be used as a base class for unit test classes to shorten the
code required to write constraints. So instead of

Assert.That(actual, Is.EqualTo(expected))

One can write

Expect(actual, EqualTo(expected))

which may not seem like much of a difference, but considering the
majority of the code in a unit test class is constraint statements,
the shorter syntax and improved intellisense makes a difference.

I have my own helper class which has only static methods which extends
AssertionHelper. Even though it's all statics I can not declare it as
a static class simply because the compiler won't allow static classes
to inherit, even though the non-static declared class only has statics
and the inheritance does provide a useful purpose.

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Tue, 22 May 2007 13:06:56 -0700, "Peter Duniho"
<Np*********@nnowslpianmk.comwrote:
>On Tue, 22 May 2007 11:38:24 -0700, learning <ed**********@gmail.com>
wrote:
....
>
There is never any situation in which you are accessing a member of a
static class in which you don't specify the type name of that static
class, so there's really no point in wanting a method in some "base"
static class to magically show up some "derived" static class. If you
have new methods not contained in the "base" static class, just put them
in a new static class separate from the original static class, and then
call them directly from that class instead.
....
>
Pete
May 22 '07 #10
On Tue, 22 May 2007 13:34:24 -0700, Samuel R. Neff <sa********@nomail.com>
wrote:
>
Not true. When calling static members from within the same class you
do not specify the name of the class, you just call the method. The
same is true for statics from an inherited class.
That's true...sorry, I was only thinking of the clients of the class, not
the class itself.

That said, I don't see "less typing" as a signficant issue, and if it is
to you, it is simple enough to wrap calls to the "base" static class in
new defintions in the "derived" static class, which addresses entirely the
implementaton inheritance issue (though I admit it still requires more
up-front typing that just adding an inheritence specifier).

Pete
May 22 '07 #11
"learning" <ed**********@gmail.comschrieb im Newsbeitrag
news:11**********************@y2g2000prf.googlegro ups.com...
>Just write another static class and add the extensions there. Since
static
members can't be virtual, there is no need to overwrite at all.

Christof

So but by doing that does it mean like I need to replciate all the
memebers
No, don't replicate any members, simply add only the new members in the new
class.

The point is, that besides naming and accessebility, there is no difference
of static members belonging to one or another class.

What do you think would be possible, if the new class is inheriting from
another class?

Christof
May 23 '07 #12
"Samuel R. Neff" <sa********@nomail.comschrieb im Newsbeitrag
news:gm********************************@4ax.com...
>
Not true. When calling static members from within the same class you
do not specify the name of the class, you just call the method. The
same is true for statics from an inherited class.
Yes, but that's only a naming issue.
>
log4net takes advantage of this by providing an AssertionHelper class
which can be used as a base class for unit test classes to shorten the
code required to write constraints. So instead of

Assert.That(actual, Is.EqualTo(expected))

One can write

Expect(actual, EqualTo(expected))
Yes, that's a point. But that's not enough reason, to introduce inheritence
of static classes.

A better solution would be, to extent the using directive. In VB.NET one can
'import' types, so that staic members of that type can be called directly as
simple name.
I don't know, why C# shouldn't have an analogous feature.

Christof
May 23 '07 #13
Yes, that's a point. But that's not enough reason, to introduce
inheritence of static classes.

A better solution would be, to extent the using directive. In VB.NET one
can 'import' types, so that staic members of that type can be called
directly as simple name.
I don't know, why C# shouldn't have an analogous feature.
This is far better, of course, because .NET doesn't support multiple
inheritance, but you could add any number of classes to the identifier
search scope.
>
Christof


May 23 '07 #14

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

Similar topics

24
by: Alf P. Steinbach | last post by:
The eighth chapter (chapter 2.1) of my attempted Correct C++ tutorial is now available, although for now only in Word format -- comments welcome! Use the free & system-independent Open Office...
7
by: Matt Jensen | last post by:
Howdy I want to simulate with .Net what I used to do with classic ASP where you would have a series of include files with utility functions that you would include when you needed them. I read...
6
by: Marty | last post by:
Hi, I have a class that I modified to be static. It is now a public sealed class and all function are static, no more constructor but a init() function to do the constructor job. This class...
2
by: Wade | last post by:
Hi all, We have created some "Base" class pages for our WebForms and UserControls. For instance, when we create a WebForm called "WebForm1.aspx", instead of inheriting from "System.Web.UI.Page"...
7
by: relient | last post by:
Question: Why can't you access a private inherited field from a base class in a derived class? I have a *theory* of how this works, of which, I'm not completely sure of but makes logical sense to...
37
by: Jan Wagner | last post by:
Hi, can't figure this one out, what's the CSS way to specify the language? In HTML it would be simply an lang="xx" attribute, or XHTML xml:lang="xx", but, how about in CSS? This would be...
3
by: rickeringill | last post by:
Hi comp.lang.javascript, I'm throwing this in for discussion. First up I don't claim to be any sort of authority on the ecmascript language spec - in fact I'm a relative newb to these more...
6
by: Doc John | last post by:
Can I create a "base" button that all the buttons in my Windows Forms can inherit from? Thanks. VS2005
8
by: ssecorp | last post by:
I first learned about OO from Java. I much prefer to program in Python though. However I am consufed about 2 things. 1. Why do I have to pass self into every method in a class? Since I am...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.