473,657 Members | 2,801 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(strin g 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(strin g ThisText){...}
public void ShowAText(strin g 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 5766
[> 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(strin g 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(strin g ThisText){...}
public void ShowAText(strin g 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******** *************@g 14g2000cwa.goog legroups.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/
"Programmin g 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*******@yaho o.com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.com...
Hi,

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

public class test{
public static void ShowAText(strin g 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(strin g ThisText){...}
public void ShowAText(strin g 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.ShowATex t();
}
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().ShowAT ext();", 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 "convinienc e" 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*******@yaho o.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.co m>
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
3767
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
7799
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 declare the function a static linkage. Why? C++ is so complex and so many situation should be considered.
3
4795
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 differences between these two methods of overriding? Thanks.
4
2216
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 bool Equals(object obj) {} Another is using the override keyword, like: public override bool Equals(object obj) {}
4
8764
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: ------------------- public class Parent { public enum AllowedColors = {Red, Blue, Green} public static void DoSomething() {
17
2909
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 when the Poodle is upcast to the Dog (in its wildest dreams) it then says "bow wow" where the bernard always says "woof" (see code). Basically, it appears that I'm hiding the poodle's speak method from everything except the poodle. Why would I...
8
3737
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 == and != opperators. Am I missing something or when would one want to have different implementations for Equals and ==? --Ken
4
2346
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
105177
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 make use of these methods assume that the methods obey these contracts so it is necessary to ensure that if your classes override these methods, they do so correctly. In this article I'll take a look at the equals and hashCode methods. ...
0
8302
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8820
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
8718
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...
0
7314
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
6162
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
5630
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
4150
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...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.