Solved

Nothing vs String.Empty

Posted on 2008-10-04
10
2,075 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 150 total points
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

17 Experts available now in Live!

Get 1:1 Help Now