Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Object variable or With block variable not set.

Object variable or With block variable not set.

Scheduled Pinned Locked Moved Visual Basic
csharpdata-structures
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    thedom2
    wrote on last edited by
    #1

    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 =)

    C W 2 Replies Last reply
    0
    • T thedom2

      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 =)

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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

      T 1 Reply Last reply
      0
      • C Christian Graus

        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

        T Offline
        T Offline
        thedom2
        wrote on last edited by
        #3

        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 :)

        T 1 Reply Last reply
        0
        • T thedom2

          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 :)

          T Offline
          T Offline
          thedom2
          wrote on last edited by
          #4

          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

          C 1 Reply Last reply
          0
          • T thedom2

            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

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            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

            C 1 Reply Last reply
            0
            • C Christian Graus

              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

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • T thedom2

                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 =)

                W Offline
                W Offline
                Walter_H
                wrote on last edited by
                #7

                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

                T 1 Reply Last reply
                0
                • W Walter_H

                  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

                  T Offline
                  T Offline
                  thedom2
                  wrote on last edited by
                  #8

                  thanks Walter thats a great suggestions cheers Dom

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups