473,386 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
Nov 20 '08 #1
12 13659

or

String.Empty ?
dim abc as string = string.Empty

I go with the string.Empty myself.


"Joe Duchtel" <du*****@gmail.comwrote in message
news:98**********************************@d23g2000 yqc.googlegroups.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

Nov 20 '08 #2
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..._23787895.html
Hope this helps,
Nov 20 '08 #3
On Nov 20, 2:13*pm, kimiraikkonen <kimiraikkone...@gmail.comwrote:
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.WriteLine
(lDummy) after I initialize it to Nothing, it will output an empty
string.

Thanks,
Joe
Nov 20 '08 #4
On Nov 20, 9:49*pm, Joe Duchtel <duch...@gmail.comwrote:
On Nov 20, 2:13*pm, kimiraikkonen <kimiraikkone...@gmail.comwrote:


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'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

Nov 20 '08 #5
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/

Nov 20 '08 #6
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...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
--
Cholo Lennon
Bs.As.
ARG

Nov 20 '08 #7
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 -
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
Nov 20 '08 #8
On Thu, 20 Nov 2008 13:44:29 -0800 (PST), Joe Duchtel
<du*****@gmail.comwrote:
>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 -
>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.
Nov 20 '08 #9
Joe Duchtel wrote:
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 -
>>>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
Nov 21 '08 #10
On Nov 20, 9:09*pm, "Cholo Lennon" <chololen...@hotmail.comwrote:
Joe Duchtel wrote:
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 -
>>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- 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
Nov 21 '08 #11
Joe Duchtel wrote:
On Nov 20, 9:09 pm, "Cholo Lennon" <chololen...@hotmail.comwrote:
>Joe Duchtel wrote:
>>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 -
>>>>>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- 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

Nov 21 '08 #12
"Joe Duchtel" <du*****@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/>

Nov 21 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Jaime Wyant | last post by:
Will someone explain this to me? >>> "test".find("") 0 Why is the empty string found at position 0? Thanks! jw
1
by: verb13 | last post by:
In an asp page in localhost I have this: Set x = Server.CreateObject("Scripting.Dictionary") And I get this error: Invalid class string If I omit Server like this: Set x =...
32
by: Indrid Colt | last post by:
Thank's for your help ! Indrid
9
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
8
by: Peter Merwood | last post by:
I'm using some sample code from MSDN. The code includes the following statement: Dim content as = .Empty I'm not familiar with the use of the square brackets. Can someone please explain to...
9
by: Fei Liu | last post by:
In Accellerated C++, the author recommends that in a header file one should not declare using std::string, using std::vector etc instead one should directly specify the namespace specifier in...
10
by: =?Utf-8?B?RWxlbmE=?= | last post by:
I am surprised to discover that c# automatically converts an integer to a string when concatenating with the "+" operator. I thought c# was supposed to be very strict about types. Doesn't it seem...
6
by: =?Utf-8?B?SmVmZg==?= | last post by:
I thought this would already be covered here, but my search turned up nothing. In VS2005, if I use "String" to define a new variable/class, it colors it in the Aqua color as it does other...
1
by: FOREST GOLDEN | last post by:
i'M want to select multiple choice by using checkbox, but it come out the above mistake, plz help me solve it, thankx....... my ItemCode is using text eg:cg001 <%@ Language=VBScript %> <HTML>...
1
by: mato81 | last post by:
Hi all! I am a newbie to WSDL. I have a questions which has been driving me crazy... If I would have a WSDL with a types element somewhat like below, what is the point of the third last row...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.