Connecting Tech Pros Worldwide Forums | Help | Site Map

Intialize String to Nothing or ""?

Joe Duchtel
Guest
 
Posts: n/a
#1: Nov 20 '08
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
Guest
 
Posts: n/a
#2: Nov 20 '08

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@d23g2000yqc.googlegroups.com...
Quote:
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

kimiraikkonen
Guest
 
Posts: n/a
#3: Nov 20 '08

re: Intialize String to Nothing or ""?


On Nov 20, 8:57*pm, Joe Duchtel <duch...@gmail.comwrote:
Quote:
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..._23787895.html


Hope this helps,
Joe Duchtel
Guest
 
Posts: n/a
#4: Nov 20 '08

re: Intialize String to Nothing or ""?


On Nov 20, 2:13*pm, kimiraikkonen <kimiraikkone...@gmail.comwrote:
Quote:
On Nov 20, 8:57*pm, Joe Duchtel <duch...@gmail.comwrote:
>
Quote:
Hello -
>
Quote:
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 = ""
>
Quote:
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.WriteLine
(lDummy) after I initialize it to Nothing, it will output an empty
string.

Thanks,
Joe
kimiraikkonen
Guest
 
Posts: n/a
#5: Nov 20 '08

re: Intialize String to Nothing or ""?


On Nov 20, 9:49*pm, Joe Duchtel <duch...@gmail.comwrote:
Quote:
On Nov 20, 2:13*pm, kimiraikkonen <kimiraikkone...@gmail.comwrote:
>
>
>
>
>
Quote:
On Nov 20, 8:57*pm, Joe Duchtel <duch...@gmail.comwrote:
>
Quote:
Quote:
Hello -
>
Quote:
Quote:
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 = ""
>
Quote:
Quote:
Thanks,
Joe
>
Quote:
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".
>
Quote:
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.
>>
Quote:
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.
Quote:
I do realize that Nothing means nothing but if I use Console.WriteLine
(lDummy) after I initialize it to Nothing, it will output an empty
string.
>
Thanks,
Joe-

Try that instead:

Console.WriteLine(IDummy.ToString) 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

rowe_newsgroups
Guest
 
Posts: n/a
#6: Nov 20 '08

re: Intialize String to Nothing or ""?


I cannot see the solutions in experts-exchange.com as I don't have a
Quote:
subscription.
Not worth it in my opinion - stick to USENET (here) or
StackOverflow.com if you think you'd like it better.
Quote:
I do realize that Nothing means nothing but if I use Console.WriteLine
(lDummy) after I initialize it to Nothing, it will output an empty
string.
Yep, Console.WriteLine(...) 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]
http://sethrowe.blogspot.com/

Cholo Lennon
Guest
 
Posts: n/a
#7: Nov 20 '08

re: Intialize String to Nothing or ""?


kimiraikkonen wrote:
Quote:
On Nov 20, 8:57 pm, Joe Duchtel <duch...@gmail.comwrote:
Quote:
>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...Basic.NET/Q_23
787895.html
Quote:
>
>
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



Joe Duchtel
Guest
 
Posts: n/a
#8: Nov 20 '08

re: Intialize String to Nothing or ""?


On Nov 20, 3:59*pm, "Cholo Lennon" <chololen...@hotmail.comwrote:
Quote:
kimiraikkonen wrote:
Quote:
On Nov 20, 8:57 pm, Joe Duchtel <duch...@gmail.comwrote:
Quote:
Hello -
>
Quote:
Quote:
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 = ""
>
Quote:
Quote:
Thanks,
Joe
>
Quote:
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".
>
Quote:
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.
>
Quote:
Also see this:
>
http://www.experts-exchange.com/Prog...NET/Visual_Bas...
787895.html
>
>
>
Quote:
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
Jack Jackson
Guest
 
Posts: n/a
#9: Nov 20 '08

re: Intialize String to Nothing or ""?


On Thu, 20 Nov 2008 13:44:29 -0800 (PST), Joe Duchtel
<duchtel@gmail.comwrote:
Quote:
>On Nov 20, 3:59*pm, "Cholo Lennon" <chololen...@hotmail.comwrote:
Quote:
>kimiraikkonen wrote:
Quote:
On Nov 20, 8:57 pm, Joe Duchtel <duch...@gmail.comwrote:
>Hello -
>>
Quote:
>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 = ""
>>
Quote:
>Thanks,
>Joe
>>
Quote:
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".
>>
Quote:
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.
>>
Quote:
Also see this:
>>
>http://www.experts-exchange.com/Prog...NET/Visual_Bas...
>787895.html
>>
>>
>>
Quote:
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.
Cholo Lennon
Guest
 
Posts: n/a
#10: Nov 21 '08

re: Intialize String to Nothing or ""?


Joe Duchtel wrote:
Quote:
On Nov 20, 3:59 pm, "Cholo Lennon" <chololen...@hotmail.comwrote:
Quote:
>kimiraikkonen wrote:
Quote:
>>On Nov 20, 8:57 pm, Joe Duchtel <duch...@gmail.comwrote:
>>>Hello -
>>
Quote:
>>>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 = ""
>>
Quote:
>>>Thanks,
>>>Joe
>>
Quote:
>>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".
>>
Quote:
>>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.
>>
Quote:
>>Also see this:
>>
>http://www.experts-exchange.com/Prog...NET/Visual_Bas...
>787895.html
>>
>>
>>
Quote:
>>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


Joe Duchtel
Guest
 
Posts: n/a
#11: Nov 21 '08

re: Intialize String to Nothing or ""?


On Nov 20, 9:09*pm, "Cholo Lennon" <chololen...@hotmail.comwrote:
Quote:
Joe Duchtel wrote:
Quote:
On Nov 20, 3:59 pm, "Cholo Lennon" <chololen...@hotmail.comwrote:
Quote:
kimiraikkonen wrote:
>On Nov 20, 8:57 pm, Joe Duchtel <duch...@gmail.comwrote:
>>Hello -
>
Quote:
Quote:
>>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 = ""
>
Quote:
Quote:
>>Thanks,
>>Joe
>
Quote:
Quote:
>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".
>
Quote:
Quote:
>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.
>
Quote:
Quote:
>Also see this:
>>
Quote:
Quote:
>Hope this helps,
>
Quote:
Quote:
Perhaps the OP is confused with the use of interfaces (due to way he
named the variable)
>
Quote:
Quote:
Regards
>
Quote:
Quote:
--
Cholo Lennon
Bs.As.
ARG- Hide quoted text -
>
Quote:
Quote:
- Show quoted text -
>
Quote:
Hello -
>
Quote:
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
Cholo Lennon
Guest
 
Posts: n/a
#12: Nov 21 '08

re: Intialize String to Nothing or ""?


Joe Duchtel wrote:
Quote:
On Nov 20, 9:09 pm, "Cholo Lennon" <chololen...@hotmail.comwrote:
Quote:
>Joe Duchtel wrote:
Quote:
>>On Nov 20, 3:59 pm, "Cholo Lennon" <chololen...@hotmail.comwrote:
>>>kimiraikkonen wrote:
>>>>On Nov 20, 8:57 pm, Joe Duchtel <duch...@gmail.comwrote:
>>>>>Hello -
>>
Quote:
>>>>>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 = ""
>>
Quote:
>>>>>Thanks,
>>>>>Joe
>>
Quote:
>>>>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".
>>
Quote:
>>>>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.
>>
Quote:
>>>>Also see this:
>>>>
Quote:
>>>>Hope this helps,
>>
Quote:
>>>Perhaps the OP is confused with the use of interfaces (due to way
>>>he named the variable)
>>
Quote:
>>>Regards
>>
Quote:
>>>--
>>>Cholo Lennon
>>>Bs.As.
>>>ARG- Hide quoted text -
>>
Quote:
>>>- Show quoted text -
>>
Quote:
>>Hello -
>>
Quote:
>>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



Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#13: Nov 21 '08

re: Intialize String to Nothing or ""?


"Joe Duchtel" <duchtel@gmail.comschrieb:
Quote:
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/>

Closed Thread