Solved

Nothing vs String.Empty

Posted on 2008-10-04
10
2,112 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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