SiteMap  | About    

How to VB.NET String Null

How to handle null String ?

A VB.NET string is an array of characters declared using the string keyword. String objects are immutable, meaning that they cannot be changed once they have been created.

What is VB.NET null ?

A null value is a value that doesnt refer to any object. Strings are reference types and can be equal to the null value like any other reference type. VB.NET uses the keyword Nothing for null values. When defining a string in a class, dont initialize it to null. Instead, initialize it to the constant String.Empty.

What is an Empty Strings ?

An empty string is an instance of a System.String object that contains zero characters. Empty strings are used quite commonly in various programming scenarios to represent a blank text field. Empty strings are initialized like this:

What is a Null Strings ?

A null string does not refer to an instance of a System.String object and any attempt to call a method on a null string results in a NullReferenceException. e.g. given below.

When run the above code it will throw NullReferenceException.

How to check null String in ?

VB.NET uses the keyword Nothing for null values.

In the above code we created a string Object and assigned Nothing and next we check the string is null or not.

IsNullOrEmpty method

IsNullOrEmpty is a convenience method that enables you to simultaneously test whether a String is Nothing or its value is Empty. It is equivalent to the following code:

result = s Is Nothing OrElse s = String.Empty


NullReferenceException indicates that you are trying to access member fields, or function types, on an object reference that points to null. That means the reference to an Object which is not initialized. More about.... NullReferenceException

Public Class Form1

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim str As String = Nothing

		If str = Nothing Then
			MsgBox("String is null")
			MsgBox("String is not null")
		End If

		str = "notempty"

		If String.IsNullOrEmpty(str) Then
			MsgBox("String is null or empty")
			MsgBox("String is not null or empty")
		End If
	End Sub

End Class (C) 2019    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.