Hi I have just started learning how to program with visualbasic.net.
Is there a way to initilise a varible as null ?
for example, somthing like this, but this gives me an error.
Dim a as Integer
a = null
Any suggestions!?
Thanks a lot,
Rauland
Printable View
Hi I have just started learning how to program with visualbasic.net.
Is there a way to initilise a varible as null ?
for example, somthing like this, but this gives me an error.
Dim a as Integer
a = null
Any suggestions!?
Thanks a lot,
Rauland
i have never heard of null integers !.rather use a=0
Thanks for the reply, I understand your point,
But as you know there is a big difference between
a=0
and
a= null,
Can you make a variable equal to null in visual basic?
I know that by default an integer variable has the value of 0, but If I haven´t assign a value to variable a, how can it have value = 0 ?? shouldn´t it be null ??
I´m confused!
This is the Classic VB sub Forum, if you are using VB.Net you are in the wrong forum. Go there (VB.NET FORUM) and specify which version you're using (01/02/05) or maybe a mod could move this thread if you confirm you want it in Vb.net forumQuote:
Originally Posted by Rauland
Cheers
I´ve asked for it to be moved!,
In the mean time any sugestions are welcome!
Why you would you need it to be Null? What are you trying to do?
You can't set a primitive type to be Null/nothing. It will always have a value and if you do not assign an initial value it will default to zero.
"Nothing" works with reference types as it removes the reference so the variable does not point to an object.
What are you trying to achieve?
Thats a long story! I would explain it but I think it would complicate things to much! But I appreciate your interest! But basically what I´m trying to do is:
I´m just trying to work with conditions,
if a variable is null then something happens
if it has a value different to null like 0,1,2,3,1 then sth else would happen.
In the past I read some javascript, and you could define, as I remember!, variables as null.
I know this is another language! but I thought that the fact you can declare varables as null was a common thing to all programing laguages. But I think I am wrong!.
You can assign null to variables in Javascript because Javascript is a weakly-typed language. In a weakly-typed language various bits of information about the variable type are stored along with the variable contents itself. VB on the other hand is a strongly typed language and so you cannot do that. If you declare an Integer variable then 4 bytes of Integer are reserved for you and automatically zeroed (to get rid of any rubbish that may have been in that memory space). Because the whole of the variable's memory space is occupied by the variable value itself, there is no room for a concept of "null" - there is no way to identify it as such.
I understand what you are trying to accomplish - for that purpose you would probably be best served by using an additional boolean variable to indicate whether the variable has been set.
BTW, welcome to VBForums :)
Well, sometimes you can pull values back from a database and then you'd use something like:
If Not IsNull(x) Then
'do something
End If
where x would usually be something like rs.Fields("Fieldname")
as far as i know only string variables can be set to null by vbnullstring ,but integers cant be set to null.i am somewhat familiar with java also and never heard of anything like that
i believe you can set a string to vbNullString, but that's about the only "null" reference I can remember
(Sorry, the page didn't load properly the first time! I didn't see all the previous replies :()!
Javascript != Java
- deleted: I forgot this was VB.NET -
VBNullString is not the same than Null. VBNullString = "" and it's a String.
What 12ngo said is an special case in VB6 that occurs when working i.e. with recordsets, it's System.DBNull in VB.Net.
And Remember this thread's destination is the VB.Net Forum.
Well, Thank you everbody for all your replies!
I think I have enough information to understand that it is not possible to asign null to variables!
Again Thanks!
Rauland
I don't know if you're talking about VB.NET or VB6, but in VB6 there is a difference between vbNullString and zero-length string. vbNullString has no address in memory:Quote:
Originally Posted by jcis
VB Code:
Dim sZeroLen As String, sNull As String sZeroLen = "" svbNull = vbNullString Debug.Print StrPtr(sZeroLen), StrPtr(svbNull)
I will take that to mean that you have an answer, and therefore, it would be pointless to move this to VB.NETQuote:
Originally Posted by Rauland
If this is incorrect, let me know, and I will move it.
Thanks.
okay because this thread is all abt null .is there anything like a null object .i once had a heated arg with my classmate .that there's nothing like a null object,but there are object refernces.
e.g
aaa a1 = new a1 is a stmt in c++ or c# that creates an object a1 of class aaa.
if we write aaa a1 then a1 is a reference to the class a1 ,am i wrong ??
Moved.
Null in a database is not the same thing as a VB.NET object being Nothing. When you assign Nothing to a variable then that variable is filled with zeroes. For reference types that amounts to referring to no object. For value types its meaning is dependent on the type. For numerical types it means the number zero. For Booleans it means False. For Dates it means midnight on Jan 1, 0001. If you want to put a null value in a database then that is something else entirely. You would assign DBNull.Value to a field in a DataRow and that would be inserted in the database as a null value.
As an aside, note that there is the new Nullable type in .NET 2.0 that allows you to specify Nothing for a value type, in a manner of speaking.