By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,693 Members | 1,230 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,693 IT Pros & Developers. It's quick & easy.

Array problem with custom class

P: n/a
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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
.... removing off topic groups
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:OV***************@TK2MSFTNGP10.phx.gbl...
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


Nov 21 '05 #2

P: n/a
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" <nj********@hotmail.com> wrote in message
news:OV***************@TK2MSFTNGP10.phx.gbl...
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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

Nov 21 '05 #3

P: n/a
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
nj********@hotmail.com
http://www.nathansokalski.com/

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:OM*************@TK2MSFTNGP12.phx.gbl...
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" <nj********@hotmail.com> wrote in message
news:OV***************@TK2MSFTNGP10.phx.gbl...
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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/


Nov 21 '05 #4

P: n/a

"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:Oh*************@tk2msftngp13.phx.gbl...
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
Nov 21 '05 #5

P: n/a
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" <nj********@hotmail.com> wrote in message
news:Oh*************@tk2msftngp13.phx.gbl...
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
nj********@hotmail.com
http://www.nathansokalski.com/

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:OM*************@TK2MSFTNGP12.phx.gbl...
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" <nj********@hotmail.com> wrote in message
news:OV***************@TK2MSFTNGP10.phx.gbl...
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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/



Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.