|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Get Variable Out of Arraylistwhere 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 Show quoteHide quote "blisspikle" <eklas***@metforming.com> wrote in message news:1112202682.017002.107170@f14g2000cwb.googlegroups.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, > 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 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/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionscollectionbaseclasstopic.asp HTH rawCoder Show quoteHide quote "blisspikle" <eklas***@metforming.com> wrote in message news:1112325977.870576.68760@g14g2000cwa.googlegroups.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 > 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
ReInstantiate an Object?
How can I determine WHICH exception I got in my CATCH? Advice needed using StringBuilder class for concatenation? How to restrict the multiple opening of the Same window... a checkbox in a datagrid Process.Start("WinWord.exe") problem add dsn programmatorically tooltip on combobox selection Re: Printing in Win98 problem |
|||||||||||||||||||||||