Object variable or With block variable not set.
-
Hi can someone tell me what wrong with my code i get the nullreference exception I know its something obvious but still new to .net here is the code: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click MsgBox(Spot(0).ToString) End Sub Function Spot() As Array Dim ObjArray As Array ObjArray = Nothing Dim Obj1 As New Object Dim Obj2 As New Object Dim Obj3 As New Object Obj1 = "Hello World" Obj2 = 69 Obj3 = False ObjArray(0) = Obj1 <-this is where the null reference is getting thrown ObjArray(1) = Obj2 ObjArray(2) = Obj3 Return ObjArray End Function thanks everyone =)
-
Hi can someone tell me what wrong with my code i get the nullreference exception I know its something obvious but still new to .net here is the code: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click MsgBox(Spot(0).ToString) End Sub Function Spot() As Array Dim ObjArray As Array ObjArray = Nothing Dim Obj1 As New Object Dim Obj2 As New Object Dim Obj3 As New Object Obj1 = "Hello World" Obj2 = 69 Obj3 = False ObjArray(0) = Obj1 <-this is where the null reference is getting thrown ObjArray(1) = Obj2 ObjArray(2) = Obj3 Return ObjArray End Function thanks everyone =)
Well, first of all, why do you want to create an array of all different types ? That kind of sucks. Second, never use MsgBox, or any other things that exist only to make VB6 coders feel warm and fuzzy. Use the .NET stuff, such as MessageBox.Show. In this case, it doesn't matter much, but it's a good habit to get into. Have you stepped through the code, to see what's happening ?
thedom2 wrote:
ObjArray = Nothing
If your array object is 'nothing', then it's not an array, is it ? You would do better to use a dynamic container such as ArrayList. Either way, if you want to have an array with 3 objects in it, you need to call new to create it. You've done the opposite, you've told the compiler that there's no memory to assign yet, and no object exists.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Well, first of all, why do you want to create an array of all different types ? That kind of sucks. Second, never use MsgBox, or any other things that exist only to make VB6 coders feel warm and fuzzy. Use the .NET stuff, such as MessageBox.Show. In this case, it doesn't matter much, but it's a good habit to get into. Have you stepped through the code, to see what's happening ?
thedom2 wrote:
ObjArray = Nothing
If your array object is 'nothing', then it's not an array, is it ? You would do better to use a dynamic container such as ArrayList. Either way, if you want to have an array with 3 objects in it, you need to call new to create it. You've done the opposite, you've told the compiler that there's no memory to assign yet, and no object exists.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
well ultimately i wanted a function that output 3 items(hence the array) when i try the below line dim objArray as NEW array intellisense tells me "cannot be used on a class that is declared 'mustinherit'" the objArray=nothing was me "trying" to debug/test the fucntion(that didnt work) any suggestions oh wise one :)
-
well ultimately i wanted a function that output 3 items(hence the array) when i try the below line dim objArray as NEW array intellisense tells me "cannot be used on a class that is declared 'mustinherit'" the objArray=nothing was me "trying" to debug/test the fucntion(that didnt work) any suggestions oh wise one :)
worked it out this is the code Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'this calls function and select 2nd array element MessageBox.Show(Spot(1).ToString) End Sub Function Spot() As Array Dim ObjArray As Array = Array.CreateInstance(GetType(Object), 3) Dim Obj1, obj2, obj3 As Object Obj1 = "Hello World" obj2 = 69 obj3 = False ObjArray(0) = Obj1 ObjArray(1) = obj2 ObjArray(2) = obj3 Return ObjArray End Function Christian you cant actually use new keyword on array member well thats what i read on msdn. you need to use createinstance to get the thing in memory and work with object. thanks for your help anyway
-
worked it out this is the code Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'this calls function and select 2nd array element MessageBox.Show(Spot(1).ToString) End Sub Function Spot() As Array Dim ObjArray As Array = Array.CreateInstance(GetType(Object), 3) Dim Obj1, obj2, obj3 As Object Obj1 = "Hello World" obj2 = 69 obj3 = False ObjArray(0) = Obj1 ObjArray(1) = obj2 ObjArray(2) = obj3 Return ObjArray End Function Christian you cant actually use new keyword on array member well thats what i read on msdn. you need to use createinstance to get the thing in memory and work with object. thanks for your help anyway
thedom2 wrote:
Christian you cant actually use new keyword on array member well thats what i read on msdn.
You typically wouldn't do this. In C# you'd do it like this object [] ObjArray = new object[3]; I have no idea how it's done in VB, but I'm sure there's a way.
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
-
thedom2 wrote:
Christian you cant actually use new keyword on array member well thats what i read on msdn.
You typically wouldn't do this. In C# you'd do it like this object [] ObjArray = new object[3]; I have no idea how it's done in VB, but I'm sure there's a way.
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
Christian Graus wrote:
I have no idea how it's done in VB, but I'm sure there's a way
Indeed there is
Dim myArray() as Object = new Object(3){}
:)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
Hi can someone tell me what wrong with my code i get the nullreference exception I know its something obvious but still new to .net here is the code: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click MsgBox(Spot(0).ToString) End Sub Function Spot() As Array Dim ObjArray As Array ObjArray = Nothing Dim Obj1 As New Object Dim Obj2 As New Object Dim Obj3 As New Object Obj1 = "Hello World" Obj2 = 69 Obj3 = False ObjArray(0) = Obj1 <-this is where the null reference is getting thrown ObjArray(1) = Obj2 ObjArray(2) = Obj3 Return ObjArray End Function thanks everyone =)
i think it would be much more easier if you wrote a class that holds the data and put cllas-instances into an ArrayList: ' The class that holds the data class mydata public StringValue as string public IntValue as integer public BoolValue as Boolean end class ' Your Function Spot Function Spot() As ArrayList Dim ObjArray As New ArrayList() Dim clsInstance1 As New mydata Dim clsInstance2 As New mydata With clsInstance1 .StringValue = "Hello World" .IntValue = 69 .BoolValue = False End With With clsInstance2 .StringValue = "Hello Again" .IntValue = 96 .BoolValue = True End With ObjArray.Add(clsInstance1) ObjArray.Add(clsInstance2) Return ObjArray End Function ' in your Button-Click: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click Dim arrList as ArrayList = Spot() MsgBox(arrList(1).IntValue.ToString()) End Sub - walter
-
i think it would be much more easier if you wrote a class that holds the data and put cllas-instances into an ArrayList: ' The class that holds the data class mydata public StringValue as string public IntValue as integer public BoolValue as Boolean end class ' Your Function Spot Function Spot() As ArrayList Dim ObjArray As New ArrayList() Dim clsInstance1 As New mydata Dim clsInstance2 As New mydata With clsInstance1 .StringValue = "Hello World" .IntValue = 69 .BoolValue = False End With With clsInstance2 .StringValue = "Hello Again" .IntValue = 96 .BoolValue = True End With ObjArray.Add(clsInstance1) ObjArray.Add(clsInstance2) Return ObjArray End Function ' in your Button-Click: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click Dim arrList as ArrayList = Spot() MsgBox(arrList(1).IntValue.ToString()) End Sub - walter