Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a class that has an ArrayList in it and I was wondering what is the proper way to set a value in the ArrayList?

 

Public Class MyClass
Private mCandidate As New ArrayList
  Public Property Candidate() As ArrayList
       Get
           Return mCandidate
       End Get
       Set(ByVal Value As ArrayList)
           mCandidate = Value
       End Set
   End Property

 

 

But couldn't I also do

 

Public Class MyClass
Private mCandidate As New ArrayList
  Public Property Candidate() As ArrayList
       Get
           Return mCandidate
       End Get
       Set(ByVal Value As ArrayList)
           mCandidate.Add(Value)
       End Set
   End Property

 

Which is the right way to do this?

Wanna-Be C# Superstar
  • *Experts*
Posted
Both examples do a different thing. In the first you are setting another ArrayList into your variable. In the second one you are adding an ArrayList to your current ArrayList. What excatly do you want to do?
Posted

Geez, I'm all screwed up.

 

I have a procedure in my main code that does something and then it returns a result from a database search. What I want is to tack that result on to the end of an ArrayList. So my ArrayList will keep growing but preserve the values that are in there already. Later I loop through the arraylist and perform some function on each member.

Should I be using an Array instead?

While I'm here what is the difference between using GetUpperBound, Count, Length, Capacity?

Wanna-Be C# Superstar
Posted

If you're only using the ArrayList inside your Class, I don't think you need to use properties, since properties are meant to be used by other objects to access your private members in a managed way.

 

To "tack that result on to the end of an ArrayList":

 

mCandidate.add(strResult)

 

Now, if you would still like to use properties, probably the best approach would be:

Private mCandidate As New ArrayList
Public Property Candidate() As ArrayList
  Get
	  Return mCandidate.Clone()
  End Get
  Set(ByVal Value As ArrayList)
	  mCandidate.Add(Value)
  End Set
End Property

 

mCandidate.Clone() is recommended so that other objects don't meddle with your original ArrayList

 

and use:

 

Me.Candidate=strResult

 

to "tack that result on to the end of an ArrayList"

 

Since your arrayList grows in size, I would stick with it instead of Arrays since all the resizing is done automatically for you.

[horizontalrule]-[/horizontalrule]

From MSDN:

GetUpperBound - Gets the upper bound of the specified dimension in the Array.

Length - Gets a 32-bit integer that represents the total number of elements in all the dimensions of the Array.

Capacity - Gets or sets the number of elements that the ArrayList can contain.

Count - Gets the number of elements actually contained in the ArrayList.

Posted
Thank you for the assistance. I think I have it straight now. The reason I am using a class to do this is because I have other objects that access the property. I am just beginning to understand all of the nuances of OOP. Basically the reason I started using classes is to get around the problem of trying to get a function to return 2 values. So I created a class that has a method that can alter any number of properties and then I can access those properties through the class. I'm not sure if this is the way to do this kind of thing but it does solve the problem...I think.
Wanna-Be C# Superstar
Posted

I am trying to implement your solution but am having some troubles.

 

Return mCandidate.Clone()

 

does not work because I have Option Strict On and it says that the cast from object to arraylist is disallowed. I suppose I can put Ctype in here to get around this.

 

 

Me.Candidate=strResult

 

Does not work because it says that string can't be converted to arraylist. This is not caused by Option Strict and ctype doesn't help.

Wanna-Be C# Superstar
Posted

My mistake, it should be:

Private mCandidate As New ArrayList
 Public Property Candidate() As ArrayList
 	  Get
 		  Return mCandidate.Clone()
 	  End Get
 	  Set(ByVal Value As [b]Object[/b])
 		  mCandidate.Add(Value)
 	  End Set
 End Property

 

[edit]That's what happens when you don't check what you copy[/edit]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...