473,382 Members | 1,766 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,382 software developers and data experts.

inheriting with static methods and type passing, recommendation?

Hi,

I have a baseclass (non-static) with some static and some non-static
methods/fields/properties.

In the baseclass in one of the static methods I need to do something like
"
somelogic
....
CallToStaticMethod();
....
somelogic
"

The problem is, that I need to be able to change CallToStaticMethod() in the
derived class and call this instead.
I do not want to copy / paste
"
somelogic
....
CallToStaticMethod();
....
somelogic
"
into the derived class.

Since it is a static, I cannot use on "CallToStaticMethod"
"virtual/override" or "abstract/override", if I use "new" I hide the
"CallToStaticMethod", however, calls in the base-class of course go to the
original CallToStaticMethod

So my workaround for now was to pass the type of the derived class to the
baseclass
StaticMethodOnlyInBaseClass(typeof(DerivedClass));
and in the base class implement something like:
static void StaticMethodOnlyInBaseClass(Type callingType)
{
callingType.GetMethod("CallToStaticMethod").Invoke (null, null);
}

This is quite clumsy...
can anyone think of another way?
Would c# generics help me here?

(Please do not forget that I need to have both, non-static and static members)

If so, how?

Thanks,
Dec 14 '05 #1
6 1753
Why does the method need to be static? Maybe it's just my foggy brain,
but I can't think of any reason why a _method_ would _have_ to be
static in this context, unless it's being invoked in other situations.
New, static _state_, that's a whole other can of worms. So, I see two
possible solutions, depending upon your situation:

1. You're struggling to make a static method participate in inheritance
when the static method is used only in this context. Solution: make the
static method a virtual instance method that manipulates static data.

2. You need the static method because it's called in other contexts as
a static method, so it absolutely can't be an instance method.
Solution: write a virtual instance method that does nothing but call
the static method. Override it in derived classes to call other static
methods in those classes.

Dec 14 '05 #2
MSDNAndi,

The only way you can do this would be to actually implement it as an
instance method which is virtual, and then call the virtual method. It's a
small cost to pay for the type safety you would have.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"MSDNAndi" <MS******@noemail.noemail> wrote in message
news:3B**********************************@microsof t.com...
Hi,

I have a baseclass (non-static) with some static and some non-static
methods/fields/properties.

In the baseclass in one of the static methods I need to do something like
"
somelogic
...
CallToStaticMethod();
...
somelogic
"

The problem is, that I need to be able to change CallToStaticMethod() in
the
derived class and call this instead.
I do not want to copy / paste
"
somelogic
...
CallToStaticMethod();
...
somelogic
"
into the derived class.

Since it is a static, I cannot use on "CallToStaticMethod"
"virtual/override" or "abstract/override", if I use "new" I hide the
"CallToStaticMethod", however, calls in the base-class of course go to the
original CallToStaticMethod

So my workaround for now was to pass the type of the derived class to the
baseclass
StaticMethodOnlyInBaseClass(typeof(DerivedClass));
and in the base class implement something like:
static void StaticMethodOnlyInBaseClass(Type callingType)
{
callingType.GetMethod("CallToStaticMethod").Invoke (null, null);
}

This is quite clumsy...
can anyone think of another way?
Would c# generics help me here?

(Please do not forget that I need to have both, non-static and static
members)

If so, how?

Thanks,

Dec 14 '05 #3
Hi,

Think of it something similar like
Array.CreateInstance(...)
or
XmlReader.Create(...).

Actually it is a little more difficult than that, since there is an actual
constructor, the static methods in this case do something else, however in
the way of processing they need to actually create an instance object of the
same type and do something with it (e.g. write a Serialization of a default
instance object somewhere).

In my case, the base-class does quite some things in the equivalent to the
"Create".
However, one of the other static methods called inside the base classes
"Create" I need to change in order to provide different functionality in the
derived-class.
So, I cannot really create an instance of the derived class in the base
class, since the base-class does not know much about the derived class.

I checked the mechanisms I could use with constructors, however those do not
seem to help me here either (I would have to repeat too much of the code in
the derived class again).

Dec 15 '05 #4
What do you think about that:
public class ClassB<T> where T : ClassA,new()
{
private static T myField = new T();

public static void MyStaticMethod()
{
myField.Method();
}
}

public class ClassA
{
public virtual void Method()
{
//do "static" work
}
}

If you need some new functionality you can change generic parameter or inherit from generic class

public class ClassC : ClassA
{
public void override Method()
{
//do new "static" work
}
}

public ClassD : ClassB<ClassC>
{

}

Usage:

ClassD cInstance = new ClassD();
cInstance.MyStaticMethod();
Dec 15 '05 #5
Mistake in usage :)

It would be :

ClassD.MyStaticMethod();

Best Regards
Darek
Dec 15 '05 #6
> However, one of the other static methods called inside the base classes "Create" I need to change in order to provide different functionality in the derived-class.

First, I feel compelled to point out that, at least in C#, this
statement doesn't make much sense. The base class's "Create" can't
provide "different functionality in the derived class". Static methods
are completely stand-alone and do not participate in inheritance, so a
base class static method is simply that: of the base class, and has
nothing to do with the derived class at all except where variable
scoping and permissions are concerned. By the same token, a derived
class static method is related to the base class only where permissions
are concerned. Just because the two methods have the same name doesn't
make one somehow an "override" of the other.

That said, I think I see what you're driving at. Do you mean this:

"I have a base class with a static method, let's call it
'StaticMethod'. The derived classes of this base class will also have
static methods (with the same name, but that's not really an important
aspect of the problem) that will do almost the same thing, but with
slight variations in behavior. I don't want to duplicate all of the
code for the base class's static method in all derived classes, but
neither can I simply call the base class's static method from derived
classes, because the behavior difference is buried in the middle of the
method. How do I write the derived class's static methods with a
minimum duplication of code?"

If that is indeed your question, then you have to handle it like a
procedural programming problem, because you are, in effect, back in the
world of procedural programming: static means no inheritance, which
means, effectively, no O-O. Back in my old procedural days, we used to
handle this by breaking up said static method into smaller methods,
implementing those in the "base class", and then having the "derived
class" methods use the same parts, viz:

public static void StaticMethod()
{
DoPart1();
DoPart2();
DoSomethingPeculiarToBaseClass();
DoPart3();
DoSomethingElsePeculiarToBaseClass();
DoPart4();
}

and then, in the derived classes:

public void static StaticMethod()
{
BaseClass.DoPart1();
BaseClass.DoPart2();
DoSomethingPeculiarToDerivedClass();
BaseClass.DoPart3();
// Skip the other thing the base class did here
BaseClass.DoPart4();
}

Dec 15 '05 #7

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

Similar topics

11
by: Noah Coad [MVP .NET/C#] | last post by:
How do you make a member of a class mandatory to override with a _new_ definition? For example, when inheriting from System.Collections.CollectionBase, you are required to implement certain...
8
by: Steven Livingstone | last post by:
Anyone able to explain to me why you cannot define an interface that can then be implemented using static methods? I understand the C# CLS states this, but just interested in the reasons behind...
16
by: Bruno Rodrigues | last post by:
Hi In a class with simple methods like: Products.Insert(string name, string desc) Products.Update(int id, string name, string desc) Products.Delete(int id) What is better? Static or common...
1
by: Matthew Roberts | last post by:
Howdy Everyone, I am having trouble understanding the process of creating a type-safe collection by inheriting from the CollectionBase class. I have done it plenty of times, but now that I sit...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
4
by: Ole Nielsby | last post by:
Here comes a generic class with a static property. Let's say they are exotic singleton pets. abstract class Pet {...} abstract class SharedPet<T>: Pet where T: SharedPet<T>, new() {...
4
by: ma740988 | last post by:
Referencing source snippet below, the actual contruction of the foo objects is done in a class. In that regard, I chose methods, class1_construct and class2_construct for demonstration purposes....
1
by: UptownYardy | last post by:
Hi excuse the mistake in the title. I'm having trouble on inheriting methods from a superclass in Java. Basically I've forgotten how to. I want to inherit 3 methods from the Artwork superclass to...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
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...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.