question about get/set properties

A

Adam Sandler

Hello,

If I want to store a value and keep that value through postbacks, I've
used code like this before...

Private _MyName As String

Public Property MyName() As String
Get
_MyName = Me.ViewState("MyName").ToString
Return _MyName
End Get
Set(ByVal value As String)
_MyName = value
Me.ViewState("MyName") = _MyName
End Set
End Property

But this only stores one value... what if a need arises to store
multiple values? And since I'm assuming one might want to store any
given number of values then I'll need a container which can grow. So I
tried something like this...

Private _myMatches As ArrayList

Public Property myMatches() As ArrayList
Get
_myMatches = Me.ViewState("myMatches")
Return _myMatches
End Get
Set(ByVal value As ArrayList)
_myMatches = value
Me.ViewState("myMatches") = _MyName
End Set
End Property

The problem comes in when I try to start throwing values in the
property. In the logic which loops through values which may need to be
stored, I have this snip of code:

' i is the counter used for itterations through the loop
myMatches.Capacity = i
myMatches.Item(i) = currentString

This code throws a null reference exception on the "myMatches.Capacity
= i" line. I made an assumption that the array list size needs to be
specified -- hence the Capacity property (kinda like ReDim'ing a plain
'ol array at runtime). But if that Capacity line is removed the null
reference exception is just thrown on the next line.

Suggestions are greatly appreciated. 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

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,817
Latest member
AdalbertoT

Latest Threads

Top