Array problem with custom class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nathan Sokalski

    #1

    Array problem with custom class

    I have an array declared as follows:

    Dim ButtonList() As NavButtonInfo

    and a Class defined as follows:

    Public Class NavButtonInfo

    Public Shared name As String

    Public Shared text As String

    Public Shared URL As String

    Public Shared parentname As String

    Public Shared Sub SetButtonInfo(B yVal name As String, ByVal text As
    String, ByVal URL As String, ByVal parentname As String)

    NavButtonInfo.n ame = name

    NavButtonInfo.t ext = text

    NavButtonInfo.U RL = URL

    NavButtonInfo.p arentname = parentname

    End Sub

    End Class


    I am adding data to my array using the following code:

    ButtonList(0).S etButtonInfo("h omeindex", "Life With Nate", "/index.aspx",
    Nothing)


    When I build and run my application, I recieve an error that tells me the
    following:

    [NullReferenceEx ception: Object reference not set to an instance of an
    object.]

    I think I must be doing something wrong or forgetting something when I add
    data to the array, but I am not sure what. Can anyone tell me what I am
    forgetting? Thanks.
    --
    Nathan Sokalski
    njsokalski@hotm ail.com
    有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。



  • David Browne

    #2
    Re: Array problem with custom class

    .... removing off topic groups
    "Nathan Sokalski" <njsokalski@hot mail.com> wrote in message
    news:OVzC%23XbW FHA.584@TK2MSFT NGP10.phx.gbl.. .[color=blue]
    >I have an array declared as follows:
    >
    > Dim ButtonList() As NavButtonInfo
    >
    > and a Class defined as follows:
    >
    > Public Class NavButtonInfo
    >
    > Public Shared name As String
    >
    > Public Shared text As String
    >
    > Public Shared URL As String
    >
    > Public Shared parentname As String
    >
    > Public Shared Sub SetButtonInfo(B yVal name As String, ByVal text As
    > String, ByVal URL As String, ByVal parentname As String)
    >
    > NavButtonInfo.n ame = name
    >
    > NavButtonInfo.t ext = text
    >
    > NavButtonInfo.U RL = URL
    >
    > NavButtonInfo.p arentname = parentname
    >
    > End Sub
    >
    > End Class
    >
    >[/color]


    Your class is broken. Don't use shared members, use member variables, and
    replace SetButtonInfo with a constructor.



    Public Class NavButtonInfo
    Public name As String
    Public text As String
    Public URL As String
    Public parentname As String

    Public Shared Sub New(ByVal name As String, ByVal text As
    String, ByVal URL As String, ByVal parentname As String)
    me.name = name
    me.text = text
    me.URL = URL
    me.parentname = parentname
    End Sub

    End Class


    Then when you declare the array, you need to actually create the array with
    some members.


    Here's one way.

    Dim ButtonList(0) As NavButtonInfo = new NavButtonInfo() { new
    NavButtonInfo(" homeindex", "Life With Nate", "/index.aspx", Nothing) }

    Read up on how to use arrays in the VB docs.

    David




    Comment

    • Karl Seguin

      #3
      Re: Array problem with custom class

      you are declaring the array but never instantiating instances of it..

      dim ButtonList(10) as NavButtonInfo

      ButtonList(0) = new NavButtonInfo
      ButtonList(0).S etButtonInfo(.. .)

      seems though like SetButtonInfo should be your custructor instead so that
      you can do:

      ButtonList(0) = new NavButtonInfo(" homeindex", "Life With Nate",
      "/index.aspx", Nothing)

      If you don't know the size of the Array consider using an ArrayList.


      Karl

      --
      MY ASP.Net tutorials
      http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
      http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
      come!)


      "Nathan Sokalski" <njsokalski@hot mail.com> wrote in message
      news:OVzC%23XbW FHA.584@TK2MSFT NGP10.phx.gbl.. .[color=blue]
      >I have an array declared as follows:
      >
      > Dim ButtonList() As NavButtonInfo
      >
      > and a Class defined as follows:
      >
      > Public Class NavButtonInfo
      >
      > Public Shared name As String
      >
      > Public Shared text As String
      >
      > Public Shared URL As String
      >
      > Public Shared parentname As String
      >
      > Public Shared Sub SetButtonInfo(B yVal name As String, ByVal text As
      > String, ByVal URL As String, ByVal parentname As String)
      >
      > NavButtonInfo.n ame = name
      >
      > NavButtonInfo.t ext = text
      >
      > NavButtonInfo.U RL = URL
      >
      > NavButtonInfo.p arentname = parentname
      >
      > End Sub
      >
      > End Class
      >
      >
      > I am adding data to my array using the following code:
      >
      > ButtonList(0).S etButtonInfo("h omeindex", "Life With Nate", "/index.aspx",
      > Nothing)
      >
      >
      > When I build and run my application, I recieve an error that tells me the
      > following:
      >
      > [NullReferenceEx ception: Object reference not set to an instance of an
      > object.]
      >
      > I think I must be doing something wrong or forgetting something when I add
      > data to the array, but I am not sure what. Can anyone tell me what I am
      > forgetting? Thanks.
      > --
      > Nathan Sokalski
      > njsokalski@hotm ail.com
      > http://www.nathansokalski.com/
      >[/color]


      Comment

      • Nathan Sokalski

        #4
        Re: Array problem with custom class

        Thanks. And you are right, I probably should make SetButtonInfo my
        constructor. The reason I didn't do that is because I don't know how (I was
        never really taught how to make constructors in VB.NET). If you could give
        me a simple example, I would appreciate it. Thanks.
        --
        Nathan Sokalski
        njsokalski@hotm ail.com
        有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。


        "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
        wrote in message news:OMV3U0bWFH A.612@TK2MSFTNG P12.phx.gbl...[color=blue]
        > you are declaring the array but never instantiating instances of it..
        >
        > dim ButtonList(10) as NavButtonInfo
        >
        > ButtonList(0) = new NavButtonInfo
        > ButtonList(0).S etButtonInfo(.. .)
        >
        > seems though like SetButtonInfo should be your custructor instead so that
        > you can do:
        >
        > ButtonList(0) = new NavButtonInfo(" homeindex", "Life With Nate",
        > "/index.aspx", Nothing)
        >
        > If you don't know the size of the Array consider using an ArrayList.
        >
        >
        > Karl
        >
        > --
        > MY ASP.Net tutorials
        > http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
        > http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
        > come!)
        >
        >
        > "Nathan Sokalski" <njsokalski@hot mail.com> wrote in message
        > news:OVzC%23XbW FHA.584@TK2MSFT NGP10.phx.gbl.. .[color=green]
        >>I have an array declared as follows:
        >>
        >> Dim ButtonList() As NavButtonInfo
        >>
        >> and a Class defined as follows:
        >>
        >> Public Class NavButtonInfo
        >>
        >> Public Shared name As String
        >>
        >> Public Shared text As String
        >>
        >> Public Shared URL As String
        >>
        >> Public Shared parentname As String
        >>
        >> Public Shared Sub SetButtonInfo(B yVal name As String, ByVal text As
        >> String, ByVal URL As String, ByVal parentname As String)
        >>
        >> NavButtonInfo.n ame = name
        >>
        >> NavButtonInfo.t ext = text
        >>
        >> NavButtonInfo.U RL = URL
        >>
        >> NavButtonInfo.p arentname = parentname
        >>
        >> End Sub
        >>
        >> End Class
        >>
        >>
        >> I am adding data to my array using the following code:
        >>
        >> ButtonList(0).S etButtonInfo("h omeindex", "Life With Nate", "/index.aspx",
        >> Nothing)
        >>
        >>
        >> When I build and run my application, I recieve an error that tells me the
        >> following:
        >>
        >> [NullReferenceEx ception: Object reference not set to an instance of an
        >> object.]
        >>
        >> I think I must be doing something wrong or forgetting something when I
        >> add data to the array, but I am not sure what. Can anyone tell me what I
        >> am forgetting? Thanks.
        >> --
        >> Nathan Sokalski
        >> njsokalski@hotm ail.com
        >> http://www.nathansokalski.com/
        >>[/color]
        >
        >[/color]


        Comment

        • David Browne

          #5
          Re: Array problem with custom class


          "Nathan Sokalski" <njsokalski@hot mail.com> wrote in message
          news:Ohw0W$bWFH A.628@tk2msftng p13.phx.gbl...[color=blue]
          > Thanks. And you are right, I probably should make SetButtonInfo my
          > constructor. The reason I didn't do that is because I don't know how (I
          > was never really taught how to make constructors in VB.NET). If you could
          > give me a simple example, I would appreciate it. Thanks.
          > --[/color]

          My previous post had an error in the constructor (retained the Shared).


          Public Class NavButtonInfo
          Public name As String
          Public text As String
          Public URL As String
          Public parentname As String

          Public Sub New(ByVal name As String, ByVal text As
          String, ByVal URL As String, ByVal parentname As String)
          me.name = name
          me.text = text
          me.URL = URL
          me.parentname = parentname
          End Sub

          David


          Comment

          • Karl Seguin

            #6
            Re: Array problem with custom class

            Public Sub New(ByVal name As String, ByVal text As tring, ByVal URL As
            String, ByVal parentname As String)
            me.name = name
            me.text = text
            me.URL = URL
            me.parentname = parentname
            End Sub

            I just realized your SetButtonInfo was shared, as are your public fields.
            This would imply that you can ever have 1 value in them at a time...yet you
            want to declare an array of them. My guess is that you don't mean for them
            to be shared...but fell into the trap of making them shared one of at a time
            to get it quasi-working. This is what your class should look like (unless I
            misunderstand the context, which is highly likely):

            Do note that changing from shared to not is a pretty big change, so you
            should learn the difference between the two before blindly accepting what
            I'm saying..both are legitimate, depending on what you are doing
            (http://www.vbip.com/books/1861004915...er_4915_08.asp
            http://www.ondotnet.com/pub/a/dotnet...rovbnetoo.html Google)

            Public Class NavButtonInfo
            private _name as string
            private _text as string
            private _url as string
            private _parentName as string

            public property Name As String
            get
            return _name
            end get
            set (value as string)
            _name = value
            end set
            end property
            public property Text As String
            get
            return _text
            end get
            set (value as string)
            _name = value
            end set
            end property
            public property Url As String
            get
            return _url
            end get
            set (value as string)
            _url = value
            end set
            end property
            public property ParentName As String
            get
            return _parentName
            end get
            set (value as string)
            _parentName = value
            end set
            end property

            Public Sub New(ByVal name As String, ByVal text As tring, ByVal URL As
            String, ByVal parentname As String)
            _name = name
            _text = text
            _URL = URL
            _parentname = parentname
            End Sub

            End Class


            --
            MY ASP.Net tutorials
            http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
            http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
            come!)


            "Nathan Sokalski" <njsokalski@hot mail.com> wrote in message
            news:Ohw0W$bWFH A.628@tk2msftng p13.phx.gbl...[color=blue]
            > Thanks. And you are right, I probably should make SetButtonInfo my
            > constructor. The reason I didn't do that is because I don't know how (I
            > was never really taught how to make constructors in VB.NET). If you could
            > give me a simple example, I would appreciate it. Thanks.
            > --
            > Nathan Sokalski
            > njsokalski@hotm ail.com
            > http://www.nathansokalski.com/
            >
            > "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
            > wrote in message news:OMV3U0bWFH A.612@TK2MSFTNG P12.phx.gbl...[color=green]
            >> you are declaring the array but never instantiating instances of it..
            >>
            >> dim ButtonList(10) as NavButtonInfo
            >>
            >> ButtonList(0) = new NavButtonInfo
            >> ButtonList(0).S etButtonInfo(.. .)
            >>
            >> seems though like SetButtonInfo should be your custructor instead so that
            >> you can do:
            >>
            >> ButtonList(0) = new NavButtonInfo(" homeindex", "Life With Nate",
            >> "/index.aspx", Nothing)
            >>
            >> If you don't know the size of the Array consider using an ArrayList.
            >>
            >>
            >> Karl
            >>
            >> --
            >> MY ASP.Net tutorials
            >> http://www.openmymind.net/ - New and Improved (yes, the popup is
            >> annoying)
            >> http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
            >> come!)
            >>
            >>
            >> "Nathan Sokalski" <njsokalski@hot mail.com> wrote in message
            >> news:OVzC%23XbW FHA.584@TK2MSFT NGP10.phx.gbl.. .[color=darkred]
            >>>I have an array declared as follows:
            >>>
            >>> Dim ButtonList() As NavButtonInfo
            >>>
            >>> and a Class defined as follows:
            >>>
            >>> Public Class NavButtonInfo
            >>>
            >>> Public Shared name As String
            >>>
            >>> Public Shared text As String
            >>>
            >>> Public Shared URL As String
            >>>
            >>> Public Shared parentname As String
            >>>
            >>> Public Shared Sub SetButtonInfo(B yVal name As String, ByVal text As
            >>> String, ByVal URL As String, ByVal parentname As String)
            >>>
            >>> NavButtonInfo.n ame = name
            >>>
            >>> NavButtonInfo.t ext = text
            >>>
            >>> NavButtonInfo.U RL = URL
            >>>
            >>> NavButtonInfo.p arentname = parentname
            >>>
            >>> End Sub
            >>>
            >>> End Class
            >>>
            >>>
            >>> I am adding data to my array using the following code:
            >>>
            >>> ButtonList(0).S etButtonInfo("h omeindex", "Life With Nate",
            >>> "/index.aspx", Nothing)
            >>>
            >>>
            >>> When I build and run my application, I recieve an error that tells me
            >>> the following:
            >>>
            >>> [NullReferenceEx ception: Object reference not set to an instance of an
            >>> object.]
            >>>
            >>> I think I must be doing something wrong or forgetting something when I
            >>> add data to the array, but I am not sure what. Can anyone tell me what I
            >>> am forgetting? Thanks.
            >>> --
            >>> Nathan Sokalski
            >>> njsokalski@hotm ail.com
            >>> http://www.nathansokalski.com/
            >>>[/color]
            >>
            >>[/color]
            >
            >[/color]


            Comment

            Working...