String.Format()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phill

    String.Format()

    [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?
  • Mattias Sjögren

    #2
    Re: String.Format()

    [color=blue]
    >In otherwords why is Format a method of the string class but it can't
    >be used on an instance of the string class?[/color]

    Because it's static.



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • Lord2702

      #3
      Re: String.Format()

      Try this:

      Int32 Num = 99;
      String str = "";

      str = str.Format("Hel lo: {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" <wackyphill@yah oo.com> wrote in message
      news:ac94631d.0 409181538.7b1e9 c37@posting.goo gle.com...[color=blue]
      > [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?[/color]


      Comment

      • Nadav

        #4
        Re: String.Format()

        I think the question is
        WHY is Format static?

        Why can't you do:

        string fmt="<%d>";
        string result=fmt.Form at(2); // => result="<2>";

        Why do you have to write:

        string result=string.F ormat(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

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: String.Format()

          Lord2702 <Lord2702@MSN.c om> wrote:[color=blue]
          > Int32 Num = 99;
          > String str = "";
          >
          > str = str.Format("Hel lo: {0}", Num);
          >
          > str ===> "Hello: 99"[/color]

          No, that doesn't compile.
          [color=blue]
          > Eventhough a method is static you can access that method with its instance,[/color]

          No you can't.
          [color=blue]
          > though, method is static an instance is NOT REQUIRED,[/color]

          It's not only not required, it's prohibited in C# (thank goodness -
          this is one of the flaws in Java).

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Phill

            #6
            Re: String.Format()

            "Lord2702" <Lord2702@MSN.c om> wrote in message news:<#A06RignE HA.3628@TK2MSFT NGP09.phx.gbl>. ..[color=blue]
            > Try this:
            >
            > Int32 Num = 99;
            > String str = "";
            >
            > str = str.Format("Hel lo: {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 ![/color]

            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

            Comment

            • Lord2702

              #7
              Re: String.Format()

              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" <wackyphill@yah oo.com> wrote in message
              news:ac94631d.0 409190749.a0dd3 02@posting.goog le.com...[color=blue]
              > "Lord2702" <Lord2702@MSN.c om> wrote in message[/color]
              news:<#A06RignE HA.3628@TK2MSFT NGP09.phx.gbl>. ..[color=blue][color=green]
              > > Try this:
              > >
              > > Int32 Num = 99;
              > > String str = "";
              > >
              > > str = str.Format("Hel lo: {0}", Num);
              > >
              > > str ===> "Hello: 99"
              > >
              > > Eventhough a method is static you can access that method with its[/color][/color]
              instance,[color=blue][color=green]
              > > though, method is static an instance is NOT REQUIRED,
              > >
              > > Good Luck ![/color]
              >
              > 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[/color]


              Comment

              • Phill

                #8
                Re: String.Format()

                "Lord2702" <Lord2702@MSN.c om> wrote in message news:<e1F4kynnE HA.3876@TK2MSFT NGP15.phx.gbl>. ..[color=blue]
                > 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.
                >[/color]

                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.

                Comment

                • Daniel O'Connell [C# MVP]

                  #9
                  Re: String.Format()


                  "Phill" <wackyphill@yah oo.com> wrote in message
                  news:ac94631d.0 409192001.5507e 6c9@posting.goo gle.com...[color=blue]
                  > "Lord2702" <Lord2702@MSN.c om> wrote in message
                  > news:<e1F4kynnE HA.3876@TK2MSFT NGP15.phx.gbl>. ..[color=green]
                  >> 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.
                  >>[/color]
                  >
                  > 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.[/color]

                  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.


                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: String.Format()

                    Phill <wackyphill@yah oo.com> wrote:[color=blue]
                    > 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.[/color]

                    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 NullReferenceEx ception if the expression used evaluates to
                    null
                    c) Expect polymorphism


                    Here's a sample piece of Java:

                    Thread t = new Thread(someRunn able);
                    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 - <skeet@pobox.co m>
                    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                    If replying to the group, please do not mail me too

                    Comment

                    Working...