Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi! I have a base class called A and the derived class called B.

In class A I have a virtual method called Select() that I call using an instance of class B. Class B does not override the Select() method.

For some reason I have to get the name of the derived class in the Select() method. I do that using this:

string typeName  = this.GetType().ToString();

 

Now I have to change the Select() method and make it static. So now I cant use the "this" keyword to get the object that called the method. And when I call "B.Select();" I have to know that the select method was called using the B class (so I can get the class name).

Sorry if I wasnt very clear.

 

Any help would be appreciated.

  • Leaders
Posted

Hm... you can't make a virtual static member. You can shadow it, but if it is called from a variable of type A that points to a B then that won't work as desired.

 

It seems that what you are trying to do is really contrary to the nature of the static method. You probably know this, but a static function call is effectively independant of any instance of the class in which it is defined or any derived class; it is not actually called from the B class.

 

If you declare a varable [b myvariable;] where class B inherits from A, the compiler will interperet myvariable.Select() exactly the same as A.Select(). There is no way to detect if it was an instance of the derived class because it gets compiled as though it wasn't.

 

Why is it that you need to know the calling type, or why can't you create two functions?

[sIGPIC]e[/sIGPIC]
Posted

Thanks, I just realised that static methods cant be overrided.

Anyway, I was doing this because I have a 3-layer arquitecture, and most of the business layer classes have the same behavior when it comes to Delete, Insert, Update and Select. They call their data layer correspondent class. So I got the Insert and Update functions and put it on the business layer base class. So the base class has to use some reflection to get the data layer class and execute these methods. Thats why I have to know the object thats is calling the method, I use the name of the object to do the reflection.

On the Insert and Update methods I had no problem doing this, but the Select and Delete methods are static. So I cant get the class that is calling it to do the reflection.

But I guess Ill just keep these methods on the derived classes.

 

Thanks ;)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...