Intialize String to Nothing or ""?

  • Thread starter Thread starter Joe Duchtel
  • Start date Start date
J

Joe Duchtel

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
 
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/Programming/Languages/.NET/Visual_Basic.NET/Q_23787895.html


Hope this helps,
 
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/Programming/Languages/.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
 
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.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
 
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.com if you think you'd like it better.
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/
 
kimiraikkonen said:
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/Programming/Languages/.NET/Visual_Basic.NET/Q_23
787895.html


Hope this helps,

Perhaps the OP is confused with the use of interfaces (due to way he named the
variable)

Regards
 
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
 
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.
 
Joe said:
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
 
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
 
Joe said:
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
 
Joe Duchtel said:
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.)
 
Joe said:
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

Yes, that is a terrible use of hungarian notation. Try to change the
standard to just naming local variables using lower case:

Dim dummy As String
 
Göran Andersson said:
Yes, that is a terrible use of hungarian notation. Try to change the
standard to just naming local variables using lower case:

Dim dummy As String

What's the advantage of this convention over the other one? It's just a
stupid, arbitrary convention too. Using a prefix like 'l' (which I do not
encourage) at least helps when using IntelliSense because it makes it easier
to complete the variable name by typing "l".
 
Back
Top