overriding static and non static method

R

rlvladbob

Hi,

I've try to access a static method using an instance instead of a
class.

public class test{
public static void ShowAText(string ThisText)
{
System.Console.WriteLine("->{0}",ThisText);
}
public static void Main()
{
test.ShowAText("MyText");
}
}
This part work, the call was made using a class (test).
What i need to do, is to call ShowAText using an isntance:

.... Main()
{
test t = new test();
t.ShowAText();
}
This work if I change the ShowAText into a non static method (removing
static keyword).
But then, it's impossible to call it from a non-static call
(test.ShowAText()).

I've try to override it:
public static void ShowAText(string ThisText){...}
public void ShowAText(string ThisText){...}

The compiler refuse since it's a redefinition.
This method can be called from both context since it does not need
class member to run (only a System.Console.WriteLine).

Is there any way arround, except by renaming the function?
I really want to be able to do
.... Main()
{
test t= new test();
t.ShowAText("...");
test.ShowAText("...");
}

Thanks a lot.

Robert

PS: This is an example, my function do much more than just that, but
it's useless to show all my code.
 
P

Pete Davis

The obvious question is: Why do you need a non-static version of the method?

If the method doesn't reference non-static members, then it should be static
and only static.

There is no way to do it other than to create a separate member, one a
static version and one an instance version and as you discovered, you'll
have to rename the instance version to something else.

Pete
 
J

Jianwei Sun

[> I've try to access a static method using an instance instead of a
]

Isn't it defeat the whole purpose of "static"?

What do u want to acheive here?
 
C

Chad Z. Hower aka Kudzu

I've try to access a static method using an instance instead of a
class.

You cant override statics in C# or VB. .NET does not maintain a VMT and it will always call the
static of the type of the reference. Delphi supports this and I find its a feature I miss very much
and often spend a lot of time implementing in much more complex ways in C#.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Empower ASP.NET with IntraWeb
http://www.atozed.com/IntraWeb/
 
R

rlvladbob

Hi,

What i want to achieve: Stop wasting time writing the class name name
when i have an instance already created. For me, a static method is
simply a method. So why not accessing them the same way as other
method? This mean, i'll have to change every static to non static.
But another problem arise when i need to call a method without creating
an instance.
function1()
{
class1 x = new class1();
x.ShowAText()
}
function2()
{
class1.ShowAText();
}
For function1, since ShowAText is a class1 method, i prefer calling it
using the instance.
For function2, i don't need an instance, so why creating one?
(I know i can do "new class1().ShowAText();", but i don't like writing
like that).

I'm not really interesting in changing the function name, but that's
what i'm doing since i code. So, i still need pointer to function ;)

Thanks, I have the answer i seeked. There is no way to do it.

Robert
 
H

Helge Jensen

Hi,

What i want to achieve: Stop wasting time writing the class name name
when i have an instance already created.

Aaahhh, the "convinience" argument.

Are you really asking for "free-functions" (outside-class)? I am really
missing those. Many calculations are naturally *functions* and putting
them as statics in a class (instead of a namespace) is just a HACK one
has to do in "pure-OO" languages ;)
For me, a static method is
simply a method. So why not accessing them the same way as other
method? This mean, i'll have to change every static to non static.

There really is a big difference between a method on an instance and on
a class. The instance method executes in another scope (having access to
"this").

One could argue that this makes instance-methods a sub-type of
static-methods, and thus allow invocation of static-methods with
instance-syntax.

Of the OO-languages i know of, only JAVA supports this.
I'm not really interesting in changing the function name, but that's
what i'm doing since i code. So, i still need pointer to function ;)

It sounds a bit like you are going a long way for a bit of convinience.
Thanks, I have the answer i seeked. There is no way to do it.

I don't like it too much, but it's what there is.

However, the usage you showed can be serviced by the pattern of
"canonical instance":

public class Foo {
public static Foo Global = new Foo();
public T f(R r) { ...; }
}

Which allows use by instance:

Foo foo = new Foo();
foo.f(r);

And by "static":

Foo.Global.f(r);
 
J

Jon Skeet [C# MVP]

What i want to achieve: Stop wasting time writing the class name name
when i have an instance already created. For me, a static method is
simply a method. So why not accessing them the same way as other
method?

Because it leads to much worse code readability. If it looks like
you're calling a method on an instance, it's reasonable to think that
the method applies to that particular instance. For example, if you
create a thread and then call:

myThread.Sleep(1000);

then it *looks* like you're making that thread sleep - when in fact
you're calling the static Thread.Sleep method, which makes the current
thread sleep.

The time spent typing the class name will be miniscule compared with
any time you (or someone else reading the code) might lose when
debugging your app, trying to work out what's really going on.
This mean, i'll have to change every static to non static.

No, it means you have to write the class name when you call static
methods - unless you're in the same class hierarchy, of course.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top