String.Format()

  • Thread starter Thread starter Phill
  • Start date Start date
P

Phill

[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?
 
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 !
 
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 said:
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).
 
Lord2702 said:
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 !
 
Lord2702 said:
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 said:
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 said:
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.
 
Back
Top