[How come you can do this]:
string.Format("MyString {0}", var);
[But you can't do this]:
String str = "MyString {0}";
str.Format(var);
In otherwords why is Format a method of the string class but it can't
be used on an instance of the string class? 9 6648
Try this:
Int32 Num = 99;
String str = "";
str = str.Format("Hello: {0}", Num);
str ===> "Hello: 99"
Eventhough a method is static you can access that method with its instance,
though, method is static an instance is NOT REQUIRED,
Good Luck !
"Phill" <wa********@yahoo.com> wrote in message
news:ac**************************@posting.google.c om... [How come you can do this]:
string.Format("MyString {0}", var);
[But you can't do this]:
String str = "MyString {0}"; str.Format(var);
In otherwords why is Format a method of the string class but it can't be used on an instance of the string class?
I think the question is
WHY is Format static?
Why can't you do:
string fmt="<%d>";
string result=fmt.Format(2); // => result="<2>";
Why do you have to write:
string result=string.Format(fmt,2);
Why is 'Replace()' (replace a substring with a string) a method of the
string instance,
but 'Format()' (replace format place holders with values) a static method?
Nadav
Lord2702 <Lo******@MSN.com> wrote: Int32 Num = 99; String str = "";
str = str.Format("Hello: {0}", Num);
str ===> "Hello: 99"
No, that doesn't compile.
Eventhough a method is static you can access that method with its instance,
No you can't.
though, method is static an instance is NOT REQUIRED,
It's not only not required, it's prohibited in C# (thank goodness -
this is one of the flaws in Java).
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
"Lord2702" <Lo******@MSN.com> wrote in message news:<#A**************@TK2MSFTNGP09.phx.gbl>... Try this:
Int32 Num = 99; String str = "";
str = str.Format("Hello: {0}", Num);
str ===> "Hello: 99"
Eventhough a method is static you can access that method with its instance, though, method is static an instance is NOT REQUIRED,
Good Luck !
You Example causes the following Error In VS 2002:
Static member 'string.Format(string, object)' cannot be accessed with
an instance reference; qualify it with a type name instead
I am sorry ! I actually, test the example in Managed Visual C++ and send you
the C# code, just converting it. But today I check it is not even possible
to access the static method, as Intelliscence will not show the static
method with instatnce variable. This is agains the static rule, because in
Native C++, the rule is just like as I stated, i.e. Static method can be
access through instance variable, and with class name.
Sorry about that.
Good Luck !
"Phill" <wa********@yahoo.com> wrote in message
news:ac*************************@posting.google.co m... "Lord2702" <Lo******@MSN.com> wrote in message
news:<#A**************@TK2MSFTNGP09.phx.gbl>... Try this:
Int32 Num = 99; String str = "";
str = str.Format("Hello: {0}", Num);
str ===> "Hello: 99"
Eventhough a method is static you can access that method with its
instance, though, method is static an instance is NOT REQUIRED,
Good Luck !
You Example causes the following Error In VS 2002:
Static member 'string.Format(string, object)' cannot be accessed with an instance reference; qualify it with a type name instead
"Lord2702" <Lo******@MSN.com> wrote in message news:<e1**************@TK2MSFTNGP15.phx.gbl>... I am sorry ! I actually, test the example in Managed Visual C++ and send you the C# code, just converting it. But today I check it is not even possible to access the static method, as Intelliscence will not show the static method with instatnce variable. This is agains the static rule, because in Native C++, the rule is just like as I stated, i.e. Static method can be access through instance variable, and with class name.
Right, comming from A C++ background I would like static methods to be
accesible to instances of the class. I'm not sure I agree w/ John that
that is a flaw in C++ or Java. It seems to be reasonable to me that
you should be able to do this. But I'd be interested in an explanation
of why you think its a bad idea John.
"Phill" <wa********@yahoo.com> wrote in message
news:ac**************************@posting.google.c om... "Lord2702" <Lo******@MSN.com> wrote in message news:<e1**************@TK2MSFTNGP15.phx.gbl>... I am sorry ! I actually, test the example in Managed Visual C++ and send you the C# code, just converting it. But today I check it is not even possible to access the static method, as Intelliscence will not show the static method with instatnce variable. This is agains the static rule, because in Native C++, the rule is just like as I stated, i.e. Static method can be access through instance variable, and with class name.
Right, comming from A C++ background I would like static methods to be accesible to instances of the class. I'm not sure I agree w/ John that that is a flaw in C++ or Java. It seems to be reasonable to me that you should be able to do this. But I'd be interested in an explanation of why you think its a bad idea John.
A static method and an instance method are two very different things. Static
methods do not interact with instance data and are expected to have either
global effects or effects on parameters, while instance methods interact
with instance data and are expected have local effects. The argument against
allowing access to static methods via an instance variable is basically
that, by calling through an instance, a static method looks like an instance
method and therefore the caller does not expect his call to have global
consequences.
I consider calling a static method via an instance to be as bad as, say,
using an instance method to maintain a static list. It just isn't a good
idea.
Phill <wa********@yahoo.com> wrote: Right, comming from A C++ background I would like static methods to be accesible to instances of the class. I'm not sure I agree w/ John that that is a flaw in C++ or Java. It seems to be reasonable to me that you should be able to do this. But I'd be interested in an explanation of why you think its a bad idea John.
It hides the fact that the method is static. This means that someone
reading the code might:
a) Expect the method to be called with respect to the particular
instance
b) Expect a NullReferenceException if the expression used evaluates to
null
c) Expect polymorphism
Here's a sample piece of Java:
Thread t = new Thread(someRunnable);
t.Start();
t.Sleep(1000);
What does the third line do? It actually makes the current thread
sleep. That's not what the code implies though.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Bob |
last post by:
I'm having trouble the string.Format() throwing exceptions and I can't figure
out what I am doing wrong.
Given the following setup code:
string str = { "one", "two", "three", "four" };
double...
|
by: Alpha |
last post by:
Hi, I'm maintaining C# code and am fairly new with C# programming. I'm
looking for codes that's droping the 2nd digit of a nuber printed out and I
suspect it's the code below. Can someone tell me...
|
by: Dominique Vandensteen |
last post by:
after the very small & vs string.format discussion I did some speed tests...
loop of 1.000.000 concatenations of 5 public string variables in a class
gave following results:
result = a & b...
|
by: David Morris |
last post by:
Hi
Could somebody please explain what the following line of code means
String.Format("{0}\{1}.{2:00}", C:\, myfile.txt, 1
It's actually the first argument that I don't understand. What is...
|
by: nobody |
last post by:
I know that given a FormatString and a DateTime you can use
DateTime.ToString(...) to convert the DateTime to a String. My question is
how can you turn that around? Given a String and a...
| |
by: L. Scott M. |
last post by:
Have a quick simple question:
dim x as string
x = "1234567890"
-------------------------------------------------------
VB 6
dim y as string
|
by: AMP |
last post by:
Hello,
I want to add some variables to a string and this isnt working.
textBox1.Text="'BSL version='+ bslVerHi+ bslVerLo";
What am I doing wrong?
Thanks
Mike
|
by: Lucky |
last post by:
hi guys!
back again with another query.
the problem is like this.
i want to print a line like this:
"---------------------------------------------"
the easiest way is to simply assign it to...
|
by: shawrie |
last post by:
Hello everyone can anyone please help me?
I basically want to set the length of a string variable to help
spacing my simple report out.
i tried
dim test as string(14)
but it didnt like that
|
by: jpenguin |
last post by:
I'm doing a simple program to learn java. I have the input and math parts right. It just isn't displaying right. It outputs a list of about 11 things-- with the format "string $ xx.xx"
In each...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |