Solved

Nothing vs String.Empty

Posted on 2008-10-04
10
2,084 Views
Last Modified: 2012-06-27
Hi, I'm using VB 2005, WinForms. I have a question about initializing variables and setting their value from a best practices point of view.

Let say I have a String variable called "strMyString" and I want to initialize its value to "No Value".

What is the best way to accomplish this, so that I can avoid problems in my code with DBNulls later at runtime.

Which of the following would be better -- or does it make a difference.

Dim strMyString as String = Nothing
or
Dim strMyString as String = String.Empty
or
Dim strMyString as String = ""

Is there a better way?

Thanks for your help,
Fulano
0
Comment
Question by:Mr_Fulano
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 48

Expert Comment

by:jpaulino
ID: 22642504
Hi Mr_Fulano,

String.Empty or "" is the same but  you type " " (with a space in the middle) and cause you problems. Nothing is used for objects and not string.

I always use String.Empty

0
 

Author Comment

by:Mr_Fulano
ID: 22642521
Hi JPaulino, nice to hear from you...

Interesting...I didn't know that Nothing was limited to Objects. It works for Strings, but lately I've begun to get a little worried that I'm using it too often.

Thanks for the input!
Fulano
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22642523
Indeed a string can be Nothing because it is a reference type.
I think the best equivalence to DbNull is Nothing.
String.Empty is a string that is empty (zero-length) but it is not Nothing

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22642528
to elaborate a bit more, it is not the same:

UPDATE SOMETABLE SET SOMEFIELD = ''      (can be represented by String.Empty)

than

UPDATE SOMETABLE SET SOMEFIELD = NULL    (can be represented by Nothing)
0
 

Author Comment

by:Mr_Fulano
ID: 22642533
Hi Jaime,

OK, so let me see if I understand your perspective. -- Are you saying that it is better to use Nothing to avoid a DBNull, or that if you use Nothing, you almost guarantee yourself a DBNull?

Thanks,
Fulano
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 150 total points
ID: 22642608
There are situations where you will need to distinguish between nothing and empty, but this depends on your own database design.
I use DbNull when user has not entered anything in a field, by example in a new record, and Empty when user left a field blank when inputting. But this is a matter of preferences.
About DbNull, it is not example the same as Nothing, DbNull is DbNull, not nothing, but certainly it is not the same as String.Empty.
When you read a DbNull value from a table, you can convert it to Nothing or String.Empty (if the value is string type, of course), again it is a matter of preferences.
0
 
LVL 48

Assisted Solution

by:jpaulino
jpaulino earned 150 total points
ID: 22642619
>> I use DbNull when user has not entered anything in a field, by example in a new record, and Empty when user left a field blank when inputting. But this is a matter of preferences.

You can also check the two at the same tipe using String.IsNullOrEmpty() method
0
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 200 total points
ID: 22642628
How you initialise your strings has nothing to do with DbNull values. A DbNull value is an object that represent a null value when reading a database. A string variable can not contain a DbNull value. The only source of a DbNull value is when you populate a data table with data from a database, then the method will put a DbNull value in the fields where the database returns null values.

In VB all variables are initialised to their default value. The default value of a String (like any other reference type) is Nothing, so assigning Nothing to the variable and leaving the default value has the same effect.

String.Empty is a predefined property that has the value of an empty string, i.e. the "" string. It's clearer to use String.Empty in your code when you want an empty string, that way you know that it's not a string where you forgot to type something between the quotation marks.

So these two give the same result:

Dim myString As String
Dim myString As String = Nothing

And these two give the same result:

Dim myString As String = ""
Dim myString As String = String.Empty
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 22642658
also consider this:

Dim value as DbNull
Dim myString as String = value.ToString()     ' will return you an empty (not null) string
0
 

Author Comment

by:Mr_Fulano
ID: 22645440
All very good advice and I learn a few things along the way. I thank you all for your suggestions and believe that a split of the points is equitable, given all the on-point suggestions made.

Thanks,
Fulano
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to open a link from vb.net app 3 22
WPF - Tooltips for ComboBox items 5 35
Exit the loop 4 34
Web Form VB.Net  import CSV 4 24
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now