Get Variable Out of Arraylist

  • Thread starter Thread starter blisspikle
  • Start date Start date
B

blisspikle

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,
 
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
 
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.
 
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
 
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 Sjí¹Ÿí²¥n 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,
 
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
 
Back
Top