# Visual Basic > Visual Basic .NET >  Why do primitive types not require the 'NEW' keyword?

## BitFlipper88

Beginner at VB.NET here. Just a noob question for the experts...

I've read at various places in an OOP language, everything is an "object". And I was amazed to see that a variable of type STRING has properties that can be accessed. This is awesome.

It never occurred to me until now that a STRING, for instance (pun intended?), doesn't require the 'NEW' keyword.

So my question(s) is, is the STRING primitive type actually an object? A structure? 

Is it merely a matter of the inner workings of the language itself?

Thanks

----------


## jmcilhinney

String is a class and works exactly like all other classes, e.g. if you declare a variable of type String and don't initialise it then the variable is Nothing and refers to no object, so trying to access members will throw a NullReferenceException.

When you declare a variable, a memory location is allocated for it and filled with zero bits, which corresponds to Nothing. What Nothing means depends on the type. All reference type (class) variables contain the memory location of an object, so Nothing means no object. All value type (structure) variables contain a value, so Nothing represents the default value for that type. That means the number zero for numeric types, False for Boolean, midnight on the morning of January 1, 0001 for DateTime, etc.

There are two reasons that String seems to behave like a value type when its actually a reference type. Firstly, as with pretty much all programming languages, VB supports literal Strings. That is why you can create a String object without invoking a constructor with the New keyword. Secondly, Strings are immutable, meaning that you cannot change a String object once you've created it. If you take these two things into account, you can see how Strings still behave like other reference types. String is not the only class that is immutable, e.g. Font, and it is not even the only one that supports literals, as VB also supports XML literals.

----------


## schoemr

John,

Nothing is not a valid value for a reference type variable. A reference type variable is assigned a reference to an object OR it is assigned the special value null (the absence of an object reference)

----------


## jmcilhinney

> John,
> 
> Nothing is not a valid value for a reference type variable. A reference type variable is assigned a reference to an object OR it is assigned the special value null (the absence of an object reference)


The absence of an object IS Nothing in VB. Try this code:

VB.net Code:
Dim var As Object
 If var Is Nothing Then
    Console.WriteLine("Looks like you owe someone an apology")
End If

----------


## schoemr

Try this code:



```
Dim obj As Object

Try
    obj = Nothing

Catch ex As Exception
 Console.WriteLine("Error: " & ex.Message)
End Try

Console.WriteLine("You think?")
```

----------


## dbasnett

> Try this code:
> 
> 
> 
> ```
> Dim obj As Object
> 
> Try
>     obj = Nothing
> ...


Not sure the point of this.

I'd look at https://learn.microsoft.com/en-us/do...eference-types

and here https://learn.microsoft.com/en-us/do...erence/nothing  you'll find this,




> If a variable is of a reference type, assigning Nothing to the variable sets it to a null reference of the variable's type. A variable that is set to a null reference is not associated with any object.


So this statement,




> Nothing is not a valid value for a reference type variable.


seems inaccurate.

----------


## schoemr

I'm sorry but I still think it is accurate:

In Vb there are two kinds of data types: value types and reference types

- Value types - hold a value directly
- Reference types hold a reference to an object in memory.

The keyword "Nothing" is used to represent *the default* value of a reference type, which is a null reference.

When you assign the value "Nothing" to a reference type variable, it means that *the variable does not refer to an instance of an object.*

----------


## techgnome

It's still a valid assignment ... it may not be a value, any more than null isn't also a value, but it's still a valid assignment. And rigtht, that variable no longer points to an instance, but it is still a valid assignment.

-tg

----------


## dbasnett

> I'm sorry but I still think it is accurate:


What?  Are you saying that this is accurate,




> Nothing is not a valid value for a reference type variable.


Well despite what you think the documentation says otherwise, and in practice you can.

----------


## BitFlipper88

Cool discussion. I don't really know the difference between reference types and value types - aside from passing arguments between functions, which i believe is the same/related principle.

But I noticed I cannot do the following.... 



```
Dim MyString as New String
```

Is it because the object is forcing me to call the constructor?



```

            Dim MyStr As New String("mystring")  'success

            MyStr = "hello world"   '<--- so whats this now?
```

It appears the above is assigning a value... but also initializing an object. 

Perhaps in VB you can set a 'default' value for an object that takes an assignment? I'm just a noob in OOP.

----------


## techgnome

> Cool discussion. I don't really know the difference between reference types and value types - aside from passing arguments between functions, which i believe is the same/related principle.
> 
> But I noticed I cannot do the following.... 
> 
> 
> 
> ```
> Dim MyString as New String
> ```
> ...


Yes, when you use the New keyword, that invokes the constructor ... which happens to be a function .... SO, you need to call it with () at the end.


```
Dim MyString as New String() '<-- this works
```



```
Dim MyStr As New String("mystring")  'success
MyStr = "hello world"   '<--- so whats this now?
```

So what's happening here is that you're creating a string and initializing it to "mystring"
The second then assigns the value "hello world" to it ... there's no magic behind it.
I'ts the same as this:


```
dim MyString as String
MyString = "mystring"
MyString = "hello world"

- or -
dim MyString as String = "mystring"
MyString = "hello world"

- or -
Dim X as Integer = 2
X = 12
```

-tg

----------


## jmcilhinney

> Try this code:
> 
> 
> 
> ```
> Dim obj As Object
> 
> Try
>     obj = Nothing
> ...


What the hell are you on about? Not that I needed to but I did just try that code and no exception was thrown, proving that assigning Nothing to a reference type variable is valid. If you were trying to prove that you were wrong, you succeeded spectacularly.

----------


## jmcilhinney

> Nothing is not a valid value for a reference type variable.





> The keyword "Nothing" is used to represent *the default* value of a reference type, which is a null reference.
> 
> When you assign the value "Nothing" to a reference type variable, it means that *the variable does not refer to an instance of an object.*


So it's not a valid value but it's the default value and it's a value you can assign?! Yeah, that makes perfect sense.



> A reference type variable is assigned a reference to an object OR it is assigned the special value null (the absence of an object reference)


Yeah, and that special value is represented by Nothing in VB, as you yourself pointed out in that later post.

Maybe you could go back and reread post #2, then quote the specific part of that that you think is wrong and tell us exactly why. I've generally given up banging my head against this particular brick wall but I would like to ensure the OP here understands what is good information and what is bad, so they are not confused when writing their own code in the future.

----------


## jmcilhinney

> Cool discussion. I don't really know the difference between reference types and value types - aside from passing arguments between functions, which i believe is the same/related principle.
> 
> But I noticed I cannot do the following.... 
> 
> 
> 
> ```
> Dim MyString as New String
> ```
> ...


Yes, the New keyword is used to invoke a constructor, which is a special method that constructs an instance of a type. Just like any other method, a constructor has a parameter list, which may be empty. Just like any other method, you can only call a constructor with arguments that match the parameters declared in the type. Here is the documentation for the list of constructors for the String class:

https://learn.microsoft.com/en-au/do...r?view=net-7.0

As you can see from that, there is no String constructor that has a single parameter of type String. That means that this:

vb.net Code:
Dim MyStr As New String("mystring")
is not really valid. It will work if you have Option Strict Off but, if you mouse over the String keyword in that code you can see that the actual overload being invoked is the one with a Char array parameter. When you run that code, the String you pass in will be implicitly converted to a Char array and that will be passed to the constructor. If you turn Option Strict On, which you ALWAYS should, then that code will not compile and you'll be told that a String cannot be implicitly converted to a Char array.

The reason that the String class has no constructor that takes a single String argument is, as I said before, String literals are supported. There's no need or point to this:

vb.net Code:
Dim MyStr As New String("mystring")
when you can do this:

vb.net Code:
Dim MyStr As String = "mystring"
You're using a String literal either way so why go through a constructor when you can simply assign directly?

As I said previously, the reason you're getting confused here is that the String class appears to behave different to other classes and actually like a value type, but in actual fact that's simply because of literal support and immutability. String is a class and behaves like a class. If you do your testing with a class other than String, you'll see the difference because there will be no option to use literals of that type.

----------


## dday9

I've cleaned up this thread by removing several posts. JMcIlhinney/Schoemer please calm down.

----------

