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

overriding static and non static method

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.

Nov 17 '05 #1
6 5756
[> I've try to access a static method using an instance instead of a
class. ]

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

What do u want to acheive here?

rl*******@yahoo.com wrote: Hi,
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.

Nov 17 '05 #2
rl*******@yahoo.com wrote in news:11*********************@g14g2000cwa.googlegro ups.com:
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/
Nov 17 '05 #3
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
<rl*******@yahoo.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
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.

Nov 17 '05 #4
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

Nov 17 '05 #5


rl*******@yahoo.com wrote:
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);

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #6
rl*******@yahoo.com <rl*******@yahoo.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7

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

Similar topics

2
by: Ovid | last post by:
Hi, I'm trying to determine the cleanest way to override class data in a subclass. class Universe { public String name; private static double PI = 3.1415; Universe(String name) {
3
by: Cheng Mo | last post by:
When overriding operator new & delte of one class, the method is implicitly declared as static. However, overriding operator new & delete of template cannot be static.The compiler says cannot...
3
by: news.microsoft.com | last post by:
Hi, It is possible to override a non virtual method with the "new" keyword So how is this different from specifying a method as virtual then providing the override keyword? Is there any...
4
by: Rafael Veronezi | last post by:
I have some questions about override in inheritance, and virtual members. I know that you can you override a method by two ways in C#, one, is overriding with the new keyword, like: public new...
4
by: Aamir Mahmood | last post by:
Hi all, I have a question. Can static members (methods and nested types) be made virtual in a class? So that they can be overridden in the child classes. For example: -------------------...
17
by: Bob Weiner | last post by:
What is the purpose of hiding intead of overriding a method? I have googled the question but haven't found anything that makes any sense of it. In the code below, the only difference is that...
8
by: Kenneth Baltrinic | last post by:
When one overrides the Equals() method of an object, one is supposed to override GetHashCode() as well and this makes good sense. But I have seen lots of people who do this and do not override the...
4
dmjpro
by: dmjpro | last post by:
Look at my code carefully..... class Super { protected int a; } class Derived extends Super { protected int a; //Here Data Overriding or Data Hiding
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.