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

Get Variable Out of Arraylist

P: n/a
Hello guys, Can not seem to get this straight. I have two comments
where I have questions on the following code.

Public Shared AnalogSensors As New System.Collections.ArrayList()

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim OB_TOP As AnalogSensor
Dim OB_BOT As AnalogSensor
Dim IB_TOP As AnalogSensor
Dim IB_BOT As AnalogSensor
OB_TOP.Name = "howdy"
MsgBox(OB_TOP.Name) 'Question, why can I output name
' here, but I can not do it directly from the Arraylist
AnalogSensors.Add(OB_TOP)
AnalogSensors.Add(OB_BOT)
AnalogSensors.Add(IB_TOP)
AnalogSensors.Add(IB_BOT)
' Msbox(AnalogSensors.Item(0).name) 'This is not correct,
' do I have to use a get, set and a property?
End Sub

Structure AnalogSensor
Public Name As String
Public Slope As Decimal
Public YIntercept As Decimal
End Structure

Thank you,

Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
The reason is simple. ArrayList is not TypeSafe.
All data stored/retrieved is in the form of Object.
So to use directly try typecasting first.

Ctype(AnalogSensors.Item(0),AnalogSensor).name

HTH
rawCoder

"blisspikle" <ek******@metforming.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hello guys, Can not seem to get this straight. I have two comments
where I have questions on the following code.

Public Shared AnalogSensors As New System.Collections.ArrayList()

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim OB_TOP As AnalogSensor
Dim OB_BOT As AnalogSensor
Dim IB_TOP As AnalogSensor
Dim IB_BOT As AnalogSensor
OB_TOP.Name = "howdy"
MsgBox(OB_TOP.Name) 'Question, why can I output name
' here, but I can not do it directly from the Arraylist
AnalogSensors.Add(OB_TOP)
AnalogSensors.Add(OB_BOT)
AnalogSensors.Add(IB_TOP)
AnalogSensors.Add(IB_BOT)
' Msbox(AnalogSensors.Item(0).name) 'This is not correct,
' do I have to use a get, set and a property?
End Sub

Structure AnalogSensor
Public Name As String
Public Slope As Decimal
Public YIntercept As Decimal
End Structure

Thank you,

Nov 21 '05 #2

P: n/a
Oh thank you,

I tried typecasting several different ways, but I didn't have the
format down right.

It came out as..

Msgbox(CType(AnalogSensors.Item(0), AnalogSensor).Name)

Thank you.

Nov 21 '05 #3

P: n/a
On the same subject. Is there an easy way to directly give one of the
structures that is added to the array list a value.

example.
AnalogSensors(0).name = "Sensor1"

I can not seem to find any good way of doing this.
Thank you,
Erick

Nov 21 '05 #4

P: n/a
Make your own TypeSafe and StronglyTyped ArrayList by inheriting from the
original one. Or better would be to inherit from CollectionBase. Msdn has
sample there. Just replace the Int16 with your custom structure.
http://msdn.microsoft.com/library/de...classtopic.asp
HTH
rawCoder
"blisspikle" <ek******@metforming.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
On the same subject. Is there an easy way to directly give one of the
structures that is added to the array list a value.

example.
AnalogSensors(0).name = "Sensor1"

I can not seem to find any good way of doing this.
Thank you,
Erick

Nov 21 '05 #5

P: n/a
The following code is from the link to msdn. I thought this part of
the code is where I should be the most concerned. I guess I am still
confused, because it looks like it just puts another object into the
arraylist. It doesn't actually set one of the properties already in the
arraylist.

Default Public Property Item(index As Integer) As Int16
Get
Return CType(List(index), Int16)
End Get
Set
List(index) = value
End Set
End Property

I quoted the below answer from Mattias Sjn from another post.

DirectCast(obj(index), ObjectType).Prop = NewValue

I can not get that to work, it gives me "Expression is a value and
therefore cannot be the target of as assignment." The error message
seemed to make sense, because I would think that Directcast would be
returning a value rather than setting a value.

I can not find the group again, but someone was talking about the
difference between boxing with classes and structures. I thought that
they said something like it I define as a Class rather than a structure
then I could do something like...

Public Class myclass
public name as string
end class

...add a bunch of myclass into myarraylist

dim dude as myclass
For each dude in myarraylist
dude.name = "Bob"
next

Would this code pass a reference to the class in the myarraylist so
when you change it, the value in the arraylist changes also? I get all
messed up with byref and byval problems.

my first ever VB.NET application is turning into an adventure. I
usually only use excel vba.
Thank you very much,

Nov 21 '05 #6

P: n/a
So... I tried out the class idea and it seams to work good, for all I
can see. I think it is easier to program a structure, and I have heard
for many things that structures are faster, but I think in the long run
using a class for this will go a little smoother. It still seems like
there would be an easy way to do structures just like this.

Module Module1
Public Class Class1
Public name As String
Public Sub New()
name = "weirdo"
End Sub
End Class

Sub main()
Dim mypeep As New Class1()
Dim mypeoples As New ArrayList()
MsgBox("#1: " & mypeep.name)
mypeep.name = "dude"
MsgBox("#2: " & mypeep.name)
mypeoples.Add(mypeep)
For Each mypeep In mypeoples
mypeep.name = "stud"
Next
MsgBox("#3: " & CType(mypeoples(0), Class1).name)

End Sub

End Module

Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.