473,772 Members | 2,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
....
CallToStaticMet hod();
....
somelogic
"

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

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

So my workaround for now was to pass the type of the derived class to the
baseclass
StaticMethodOnl yInBaseClass(ty peof(DerivedCla ss));
and in the base class implement something like:
static void StaticMethodOnl yInBaseClass(Ty pe callingType)
{
callingType.Get Method("CallToS taticMethod").I nvoke(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 1775
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.co m

"MSDNAndi" <MS******@noema il.noemail> wrote in message
news:3B******** *************** ***********@mic rosoft.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
...
CallToStaticMet hod();
...
somelogic
"

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

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

So my workaround for now was to pass the type of the derived class to the
baseclass
StaticMethodOnl yInBaseClass(ty peof(DerivedCla ss));
and in the base class implement something like:
static void StaticMethodOnl yInBaseClass(Ty pe callingType)
{
callingType.Get Method("CallToS taticMethod").I nvoke(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.CreateIns tance(...)
or
XmlReader.Creat e(...).

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.MySta ticMethod();
Dec 15 '05 #5
Mistake in usage :)

It would be :

ClassD.MyStatic Method();

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();
DoSomethingPecu liarToBaseClass ();
DoPart3();
DoSomethingElse PeculiarToBaseC lass();
DoPart4();
}

and then, in the derived classes:

public void static StaticMethod()
{
BaseClass.DoPar t1();
BaseClass.DoPar t2();
DoSomethingPecu liarToDerivedCl ass();
BaseClass.DoPar t3();
// Skip the other thing the base class did here
BaseClass.DoPar t4();
}

Dec 15 '05 #7

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

Similar topics

11
2168
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 methods, such as public void Add(MyClass c). How can I enforce the same behavior (of requiring to implement a member with a new return type in an inherited class) in the master class (similar to the CollectionBase)? I have a class called...
8
52758
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 it. thanks, Steven
16
2530
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 methods? This objects do not need instances.
1
336
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 down and look at it, I'm wondering why it behaves the way it does, and also how to improve its functionality. First, understand the basic format of a type-safe collection:
11
3843
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 experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
4
5074
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() { private static T singleton = new T();
4
1801
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. That aside, I encountered source akin to what's shown below today and I was almost convinced the source is wrought with trouble. On second thought it appears legal. The static ptr_foo object and it's use in assignment to other foo objects (ptr_1...
1
1940
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 be used in the Print subclass. The reason being I want to add an extra parameter (printNumber) to these methods in the Print Class. This section of code from the Artwork class is what I want to be inherited: /** * Prints an Artwork piece to the...
9
5849
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 the static Parse method of the conversion class. if (InValue is string) return T.Parse((string)InValue); else return base.ConvertFrom(context, culture, InValue);
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8934
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.