By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,169 Members | 1,233 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,169 IT Pros & Developers. It's quick & easy.

"inherit" from a static class

P: n/a
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
Share this Question
Share on Google+
13 Replies


P: n/a
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

P: n/a
"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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a


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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
"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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.