Intialize String to Nothing or ""?

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

    Intialize String to Nothing or ""?

    Hello -

    This might be a silly question but I was wondering if there is "a"
    preferred way of initializing a String ... so either ...
    Dim lDummy As String = Nothing
    .... or ...
    Dim lDummy As String = ""

    Thanks,
    Joe
  • sloan

    #2
    Re: Intialize String to Nothing or "" ?


    or

    String.Empty ?


    dim abc as string = string.Empty

    I go with the string.Empty myself.




    "Joe Duchtel" <duchtel@gmail. comwrote in message
    news:987f21cb-7ae7-4624-9a86-ee18b38864a8@d2 3g2000yqc.googl egroups.com...
    Hello -
    >
    This might be a silly question but I was wondering if there is "a"
    preferred way of initializing a String ... so either ...
    Dim lDummy As String = Nothing
    ... or ...
    Dim lDummy As String = ""
    >
    Thanks,
    Joe

    Comment

    • kimiraikkonen

      #3
      Re: Intialize String to Nothing or &quot;&quot; ?

      On Nov 20, 8:57 pm, Joe Duchtel <duch...@gmail. comwrote:
      Hello -
      >
      This might be a silly question but I was wondering if there is "a"
      preferred way of initializing a String ... so either ...
      Dim lDummy As String = Nothing
      ... or ...
      Dim lDummy As String = ""
      >
      Thanks,
      Joe
      They are not the same. An empty string (String.Empty or "") is used to
      assign an empty value to the variable. But setting it to "Nothing"
      means no value is assigned to the variable(null reference), and same
      as "Dim IDummy As String".

      So, you need to choice what you'll do with the string in further
      processing. I'd prefer to assign a value to it if you'll use that
      instance in your project as well.

      Also see this:



      Hope this helps,

      Comment

      • Joe Duchtel

        #4
        Re: Intialize String to Nothing or &quot;&quot; ?

        On Nov 20, 2:13 pm, kimiraikkonen <kimiraikkone.. .@gmail.comwrot e:
        On Nov 20, 8:57 pm, Joe Duchtel <duch...@gmail. comwrote:
        >
        Hello -
        >
        This might be a silly question but I was wondering if there is "a"
        preferred way of initializing a String ... so either ...
        Dim lDummy As String = Nothing
        ... or ...
        Dim lDummy As String = ""
        >
        Thanks,
        Joe
        >
        They are not the same. An empty string (String.Empty or "") is used to
        assign an empty value to the variable. But setting it to "Nothing"
        means no value is assigned to the variable(null reference), and same
        as "Dim IDummy As String".
        >
        So, you need to choice what you'll do with the string in further
        processing. I'd prefer to assign a value to it if you'll use that
        instance in your project as well.
        >
        Also see this:http://www.experts-exchange.com/Prog...NET/Visual_Bas...
        >
        Hope this helps,
        Hello -

        I cannot see the solutions in experts-exchange.com as I don't have a
        subscription.

        I do realize that Nothing means nothing but if I use Console.WriteLi ne
        (lDummy) after I initialize it to Nothing, it will output an empty
        string.

        Thanks,
        Joe

        Comment

        • kimiraikkonen

          #5
          Re: Intialize String to Nothing or &quot;&quot; ?

          On Nov 20, 9:49 pm, Joe Duchtel <duch...@gmail. comwrote:
          On Nov 20, 2:13 pm, kimiraikkonen <kimiraikkone.. .@gmail.comwrot e:
          >
          >
          >
          >
          >
          On Nov 20, 8:57 pm, Joe Duchtel <duch...@gmail. comwrote:
          >
          Hello -
          >
          This might be a silly question but I was wondering if there is "a"
          preferred way of initializing a String ... so either ...
          Dim lDummy As String = Nothing
          ... or ...
          Dim lDummy As String = ""
          >
          Thanks,
          Joe
          >
          They are not the same. An empty string (String.Empty or "") is used to
          assign an empty value to the variable. But setting it to "Nothing"
          means no value is assigned to the variable(null reference), and same
          as "Dim IDummy As String".
          >
          So, you need to choice what you'll do with the string in further
          processing. I'd prefer to assign a value to it if you'll use that
          instance in your project as well.
          >>
          Hope this helps,
          >
          Hello -
          >
          I cannot see the solutions in experts-exchange.com as I don't have a
          subscription.
          I'm not also their member. Just scroll down the page a bit more,
          you'll see the whole discussion.
          I do realize that Nothing means nothing but if I use Console.WriteLi ne
          (lDummy) after I initialize it to Nothing, it will output an empty
          string.
          >
          Thanks,
          Joe-

          Try that instead:

          Console.WriteLi ne(IDummy.ToStr ing) it'll throw a null reference
          exception just like when you do the same in a MsgBox (MessageBox).

          Again, by using "Dim IDummy As String", you are not assigning any
          value to the variable. So, go with an empty string, preferably,
          String.Empty.

          Thanks,

          Onur Güzel

          Comment

          • rowe_newsgroups

            #6
            Re: Intialize String to Nothing or &quot;&quot; ?

            I cannot see the solutions in experts-exchange.com as I don't have a
            subscription.
            Not worth it in my opinion - stick to USENET (here) or
            StackOverflow.c om if you think you'd like it better.
            I do realize that Nothing means nothing but if I use Console.WriteLi ne
            (lDummy) after I initialize it to Nothing, it will output an empty
            string.
            Yep, Console.WriteLi ne(...) actually uses string.Format(. ..) and is
            smart enough to not blow up on nulls.

            As for what I do, I use string.Empty.

            Thanks,

            Seth Rowe [MVP]


            Comment

            • Cholo Lennon

              #7
              Re: Intialize String to Nothing or &quot;&quot; ?

              kimiraikkonen wrote:
              On Nov 20, 8:57 pm, Joe Duchtel <duch...@gmail. comwrote:
              >Hello -
              >>
              >This might be a silly question but I was wondering if there is "a"
              >preferred way of initializing a String ... so either ...
              >Dim lDummy As String = Nothing
              >... or ...
              >Dim lDummy As String = ""
              >>
              >Thanks,
              >Joe
              >
              They are not the same. An empty string (String.Empty or "") is used to
              assign an empty value to the variable. But setting it to "Nothing"
              means no value is assigned to the variable(null reference), and same
              as "Dim IDummy As String".
              >
              So, you need to choice what you'll do with the string in further
              processing. I'd prefer to assign a value to it if you'll use that
              instance in your project as well.
              >
              Also see this:
              >

              787895.html
              >
              >
              Hope this helps,
              Perhaps the OP is confused with the use of interfaces (due to way he named the
              variable)

              Regards


              --
              Cholo Lennon
              Bs.As.
              ARG



              Comment

              • Joe Duchtel

                #8
                Re: Intialize String to Nothing or &quot;&quot; ?

                On Nov 20, 3:59 pm, "Cholo Lennon" <chololen...@ho tmail.comwrote:
                kimiraikkonen wrote:
                On Nov 20, 8:57 pm, Joe Duchtel <duch...@gmail. comwrote:
                Hello -
                >
                This might be a silly question but I was wondering if there is "a"
                preferred way of initializing a String ... so either ...
                Dim lDummy As String = Nothing
                ... or ...
                Dim lDummy As String = ""
                >
                Thanks,
                Joe
                >
                They are not the same. An empty string (String.Empty or "") is used to
                assign an empty value to the variable. But setting it to "Nothing"
                means no value is assigned to the variable(null reference), and same
                as "Dim IDummy As String".
                >
                So, you need to choice what you'll do with the string in further
                processing. I'd prefer to assign a value to it if you'll use that
                instance in your project as well.
                >
                Also see this:
                >
                http://www.experts-exchange.com/Prog...NET/Visual_Bas...
                787895.html
                >
                >
                >
                Hope this helps,
                >
                Perhaps the OP is confused with the use of interfaces (due to way he named the
                variable)
                >
                Regards
                >
                --
                Cholo Lennon
                Bs.As.
                ARG- Hide quoted text -
                >
                - Show quoted text -
                Hello -

                Okay ... what is wrong with the way I named the variable? This was
                just a simple example and was not taken from actual code ... what am I
                confused about?

                Thanks,
                Joe

                Comment

                • Jack Jackson

                  #9
                  Re: Intialize String to Nothing or &quot;&quot; ?

                  On Thu, 20 Nov 2008 13:44:29 -0800 (PST), Joe Duchtel
                  <duchtel@gmail. comwrote:
                  >On Nov 20, 3:59 pm, "Cholo Lennon" <chololen...@ho tmail.comwrote:
                  >kimiraikkone n wrote:
                  On Nov 20, 8:57 pm, Joe Duchtel <duch...@gmail. comwrote:
                  >Hello -
                  >>
                  >This might be a silly question but I was wondering if there is "a"
                  >preferred way of initializing a String ... so either ...
                  >Dim lDummy As String = Nothing
                  >... or ...
                  >Dim lDummy As String = ""
                  >>
                  >Thanks,
                  >Joe
                  >>
                  They are not the same. An empty string (String.Empty or "") is used to
                  assign an empty value to the variable. But setting it to "Nothing"
                  means no value is assigned to the variable(null reference), and same
                  as "Dim IDummy As String".
                  >>
                  So, you need to choice what you'll do with the string in further
                  processing. I'd prefer to assign a value to it if you'll use that
                  instance in your project as well.
                  >>
                  Also see this:
                  >>
                  >http://www.experts-exchange.com/Prog...NET/Visual_Bas...
                  >787895.html
                  >>
                  >>
                  >>
                  Hope this helps,
                  >>
                  >Perhaps the OP is confused with the use of interfaces (due to way he named the
                  >variable)
                  >>
                  >Regards
                  >>
                  >--
                  >Cholo Lennon
                  >Bs.As.
                  >ARG- Hide quoted text -
                  >>
                  >- Show quoted text -
                  >
                  >Hello -
                  >
                  >Okay ... what is wrong with the way I named the variable? This was
                  >just a simple example and was not taken from actual code ... what am I
                  >confused about?
                  >
                  >Thanks,
                  >Joe
                  I think Cholo thought you named the variable IDummy instead of lDummy.
                  The convention is to name Interfaces starting with I.

                  Comment

                  • Cholo Lennon

                    #10
                    Re: Intialize String to Nothing or &quot;&quot; ?

                    Joe Duchtel wrote:
                    On Nov 20, 3:59 pm, "Cholo Lennon" <chololen...@ho tmail.comwrote:
                    >kimiraikkone n wrote:
                    >>On Nov 20, 8:57 pm, Joe Duchtel <duch...@gmail. comwrote:
                    >>>Hello -
                    >>
                    >>>This might be a silly question but I was wondering if there is "a"
                    >>>preferred way of initializing a String ... so either ...
                    >>>Dim lDummy As String = Nothing
                    >>>... or ...
                    >>>Dim lDummy As String = ""
                    >>
                    >>>Thanks,
                    >>>Joe
                    >>
                    >>They are not the same. An empty string (String.Empty or "") is used
                    >>to assign an empty value to the variable. But setting it to
                    >>"Nothing"
                    >>means no value is assigned to the variable(null reference), and same
                    >>as "Dim IDummy As String".
                    >>
                    >>So, you need to choice what you'll do with the string in further
                    >>processing. I'd prefer to assign a value to it if you'll use that
                    >>instance in your project as well.
                    >>
                    >>Also see this:
                    >>
                    >http://www.experts-exchange.com/Prog...NET/Visual_Bas...
                    >787895.html
                    >>
                    >>
                    >>
                    >>Hope this helps,
                    >>
                    >Perhaps the OP is confused with the use of interfaces (due to way he
                    >named the variable)
                    >>
                    >Regards
                    >>
                    >--
                    >Cholo Lennon
                    >Bs.As.
                    >ARG- Hide quoted text -
                    >>
                    >- Show quoted text -
                    >
                    Hello -
                    >
                    Okay ... what is wrong with the way I named the variable? This was
                    just a simple example and was not taken from actual code ... what am I
                    confused about?
                    >
                    Well... Jack gave you the answer about my suposition. BTW if you used "1" instead of "I" then you're wrong... you cannot start an
                    identifier with a number.

                    Regards

                    --
                    Cholo Lennon
                    Bs.As.
                    ARG


                    Comment

                    • Joe Duchtel

                      #11
                      Re: Intialize String to Nothing or &quot;&quot; ?

                      On Nov 20, 9:09 pm, "Cholo Lennon" <chololen...@ho tmail.comwrote:
                      Joe Duchtel wrote:
                      On Nov 20, 3:59 pm, "Cholo Lennon" <chololen...@ho tmail.comwrote:
                      kimiraikkonen wrote:
                      >On Nov 20, 8:57 pm, Joe Duchtel <duch...@gmail. comwrote:
                      >>Hello -
                      >
                      >>This might be a silly question but I was wondering if there is "a"
                      >>preferred way of initializing a String ... so either ...
                      >>Dim lDummy As String = Nothing
                      >>... or ...
                      >>Dim lDummy As String = ""
                      >
                      >>Thanks,
                      >>Joe
                      >
                      >They are not the same. An empty string (String.Empty or "") is used
                      >to assign an empty value to the variable. But setting it to
                      >"Nothing"
                      >means no value is assigned to the variable(null reference), and same
                      >as "Dim IDummy As String".
                      >
                      >So, you need to choice what you'll do with the string in further
                      >processing. I'd prefer to assign a value to it if you'll use that
                      >instance in your project as well.
                      >
                      >Also see this:
                      >>
                      >Hope this helps,
                      >
                      Perhaps the OP is confused with the use of interfaces (due to way he
                      named the variable)
                      >
                      Regards
                      >
                      --
                      Cholo Lennon
                      Bs.As.
                      ARG- Hide quoted text -
                      >
                      - Show quoted text -
                      >
                      Hello -
                      >
                      Okay ... what is wrong with the way I named the variable?  This was
                      just a simple example and was not taken from actual code ... what am I
                      confused about?
                      >
                      Well... Jack gave you the answer about my suposition. BTW if you used "1"instead of "I" then you're wrong... you cannot start an
                      identifier with a number.
                      >
                      Regards
                      >
                      --
                      Cholo Lennon
                      Bs.As.
                      ARG- Hide quoted text -
                      >
                      - Show quoted text -
                      Hello -

                      I used a lower-case "L" sinde the variable is a local variable. This
                      is per our coding standard. I know people will disagree with that but
                      that's the reason.

                      Thanks!
                      Joe

                      Comment

                      • Cholo Lennon

                        #12
                        Re: Intialize String to Nothing or &quot;&quot; ?

                        Joe Duchtel wrote:
                        On Nov 20, 9:09 pm, "Cholo Lennon" <chololen...@ho tmail.comwrote:
                        >Joe Duchtel wrote:
                        >>On Nov 20, 3:59 pm, "Cholo Lennon" <chololen...@ho tmail.comwrote:
                        >>>kimiraikkone n wrote:
                        >>>>On Nov 20, 8:57 pm, Joe Duchtel <duch...@gmail. comwrote:
                        >>>>>Hello -
                        >>
                        >>>>>This might be a silly question but I was wondering if there is
                        >>>>>"a" preferred way of initializing a String ... so either ...
                        >>>>>Dim lDummy As String = Nothing
                        >>>>>... or ...
                        >>>>>Dim lDummy As String = ""
                        >>
                        >>>>>Thanks,
                        >>>>>Joe
                        >>
                        >>>>They are not the same. An empty string (String.Empty or "") is
                        >>>>used to assign an empty value to the variable. But setting it to
                        >>>>"Nothing"
                        >>>>means no value is assigned to the variable(null reference), and
                        >>>>same as "Dim IDummy As String".
                        >>
                        >>>>So, you need to choice what you'll do with the string in further
                        >>>>processin g. I'd prefer to assign a value to it if you'll use that
                        >>>>instance in your project as well.
                        >>
                        >>>>Also see this:
                        >>>>
                        >>>>Hope this helps,
                        >>
                        >>>Perhaps the OP is confused with the use of interfaces (due to way
                        >>>he named the variable)
                        >>
                        >>>Regards
                        >>
                        >>>--
                        >>>Cholo Lennon
                        >>>Bs.As.
                        >>>ARG- Hide quoted text -
                        >>
                        >>>- Show quoted text -
                        >>
                        >>Hello -
                        >>
                        >>Okay ... what is wrong with the way I named the variable? This was
                        >>just a simple example and was not taken from actual code ... what
                        >>am I confused about?
                        >>
                        >Well... Jack gave you the answer about my suposition. BTW if you
                        >used "1" instead of "I" then you're wrong... you cannot start an
                        >identifier with a number.
                        >>
                        >Regards
                        >>
                        >--
                        >Cholo Lennon
                        >Bs.As.
                        >ARG- Hide quoted text -
                        >>
                        >- Show quoted text -
                        >
                        Hello -
                        >
                        I used a lower-case "L" sinde the variable is a local variable. This
                        is per our coding standard. I know people will disagree with that but
                        that's the reason.
                        >
                        Ok, I have to change the font of my newsreader, it's terrible :-P

                        Regards

                        --
                        Cholo Lennon
                        Bs.As.
                        ARG



                        Comment

                        • Herfried K. Wagner [MVP]

                          #13
                          Re: Intialize String to Nothing or &quot;&quot; ?

                          "Joe Duchtel" <duchtel@gmail. comschrieb:
                          This might be a silly question but I was wondering if there is "a"
                          preferred way of initializing a String ... so either ...
                          Dim lDummy As String = Nothing
                          ... or ...
                          Dim lDummy As String = ""
                          I prefer to let the compiler initize it with its default value (which is a
                          reference to 'Nothing' by default):

                          \\\
                          Dim lDummy As String
                          ///

                          (BTW, I have disabled the BC42104 warning because I believe it does not make
                          sense.)

                          --
                          M S Herfried K. Wagner
                          M V P <URL:http://dotnet.mvps.org/>
                          V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

                          Comment

                          Working...