Roy Crielaard
Roy Crielaard

Reputation: 645

Variable declaration and assignment

As a beginner programmer I am trying to understand the concepts of variable declaration and assigning values in Visual Basic. I am considering this code from a tutorial lesson:

 Imports System.Text

 Module Module1

        Sub Main()

            Dim myString As StringBuilder = New StringBuilder

            For i = 1 To 10
               myString.Append("-")
               myString.Append(i)
            Next

            Console.WriteLine(myString)
            Console.ReadLine()

        End Sub

 End Module

My confusion is due to the line:

Dim myString As StringBuilder = New StringBuilder

What I think is happening is that I declare the variable myString as a new object instance of the StringBuilder class (I hope I am using the words ‘object’ and 'class' correctly here). However, why does myString subsequently needs to be assigned to New StringBuilder? Didn't I just make it a StringBuilder with Dim? And what is the role of the New operator in this example?

Any explanation that helps me understand is much appreciated.

Upvotes: 0

Views: 928

Answers (3)

Steve
Steve

Reputation: 216313

The line

 Dim myString as StringBuilder 

declares a variable that is meant to reference a StringBuilder instance. That instance doesn't exist until you call New. New creates the instance somewhere in memory and returns the reference to this memory area where the instance exists.

Let's try to examine the line .
When you write

 Dim myString as StringBuilder = New StringBuilder

(you could also use Dim myString = new StringBuilder(), the compiler can recognize the type of the variable from the context)

you are putting in one line the two instructions

Dim myString As StringBuilder
myString = New StringBuilder

In the first line, you declare a variable that you want to reference a StringBuilder instance but, at this point, the variable (think about it as an area of 4 bytes in memory) contains nothing. The variable's value has not been assigned to anything and you can prove it with

Dim myString As StringBuilder
Dim b As Boolean = False
b = myString Is Nothing
Console.WriteLine(b)  ' => outputs True

Now, you call the New StringBuilder operator who searches an area of memory where there is enough space to store all the internal fields that are part of a StringBuilder instance, after founding this area, the New operator calls the parameterless constructor of the StringBuilder class (that is supposed to initialize the internal fields to whatever is required) and RETURNS the value that points to the area of memory allocated for the StringBuilder instance. It is this value (a reference to...) that will be assigned to your myString variable that now is no more Nothing

myString = New StringBuilder()
b = myString Is Nothing
Console.WriteLine(b)   ' => outputs False

Upvotes: 1

Mathter
Mathter

Reputation: 747

String builder is a class.

When you do Dim myString as StringBuilder, you are creating a variable, that is, a memory space, that will point to a location in memory of an Instance of StringBuilder.

Whenever you Dim something, its value will be the default value for that class. For most classes the default is null, although ValueTypes such as Integer and Boolean, have other default values (0 for Integer, false for Boolean)

When you do = New StringBuilder, you are Instantiating an object of the StringBuilder class, and now your variable myString is pointing to that place in memory.

If you omit this step, myString.Append("-") will throw a NullPointerException, because myString is null.

Dim myString As StringBuilder = New StringBuilder is an easy way of doing:

Dim myString As StringBuilder
myString = New StringBuilder

Upvotes: 0

Henry
Henry

Reputation: 3013

Here's what happened, you did a shortcut to:

Dim myString As StringBuilder
myString = new StringBuilder()

So it does not need to be assign a new StringBuilder, because you already made the assignment.

Now the Console.WriteLine should be myString.ToString(), like

Console.WriteLine (myString.ToString())

This will convert the StringBuilder object to a string.

Upvotes: 0

Related Questions