Array problem with custom class

N

Nathan Sokalski

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(ByVal name As String, ByVal text As
String, ByVal URL As String, ByVal parentname As String)

NavButtonInfo.name = name

NavButtonInfo.text = text

NavButtonInfo.URL = URL

NavButtonInfo.parentname = parentname

End Sub

End Class


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

ButtonList(0).SetButtonInfo("homeindex", "Life With Nate", "/index.aspx",
Nothing)


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

[NullReferenceException: 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.
 
D

David Browne

.... removing off topic groups
Nathan Sokalski said:
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(ByVal name As String, ByVal text As
String, ByVal URL As String, ByVal parentname As String)

NavButtonInfo.name = name

NavButtonInfo.text = text

NavButtonInfo.URL = URL

NavButtonInfo.parentname = parentname

End Sub

End Class


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
 
K

Karl Seguin

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

dim ButtonList(10) as NavButtonInfo

ButtonList(0) = new NavButtonInfo
ButtonList(0).SetButtonInfo(...)

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
 
N

Nathan Sokalski

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
(e-mail address removed)
http://www.nathansokalski.com/

Karl Seguin said:
you are declaring the array but never instantiating instances of it..

dim ButtonList(10) as NavButtonInfo

ButtonList(0) = new NavButtonInfo
ButtonList(0).SetButtonInfo(...)

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 said:
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(ByVal name As String, ByVal text As
String, ByVal URL As String, ByVal parentname As String)

NavButtonInfo.name = name

NavButtonInfo.text = text

NavButtonInfo.URL = URL

NavButtonInfo.parentname = parentname

End Sub

End Class


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

ButtonList(0).SetButtonInfo("homeindex", "Life With Nate", "/index.aspx",
Nothing)


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

[NullReferenceException: 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.
 
D

David Browne

Nathan Sokalski said:
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.
--

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
 
K

Karl Seguin

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/chapter_4915_08.asp
http://www.ondotnet.com/pub/a/dotnet/2003/05/20/introvbnetoo.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 said:
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
(e-mail address removed)
http://www.nathansokalski.com/

Karl Seguin said:
you are declaring the array but never instantiating instances of it..

dim ButtonList(10) as NavButtonInfo

ButtonList(0) = new NavButtonInfo
ButtonList(0).SetButtonInfo(...)

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 said:
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(ByVal name As String, ByVal text As
String, ByVal URL As String, ByVal parentname As String)

NavButtonInfo.name = name

NavButtonInfo.text = text

NavButtonInfo.URL = URL

NavButtonInfo.parentname = parentname

End Sub

End Class


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

ButtonList(0).SetButtonInfo("homeindex", "Life With Nate",
"/index.aspx", Nothing)


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

[NullReferenceException: 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top