|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
ARRAYLIST ADDING A CLASSsomething like the text below. I think that the problem is that every time I add a class V_Sensor into the arraylist it puts it in as a reference. So in the end all of the arraylist items turn out to be the same as the last sensor in the text file. I am new and I do not know how to declare and put a class into an arraylist so it will be a different reference for every loop. <text> NAME=SENSOR1 SLOPE=1.1 YINTERCEPT=1.2 NAME=SENSOR2 SLOPE=2.1 YINTERCEPT=2.2 etc.... </text> <code> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim V_Sensor As New AnalogSensor() Dim V_FileReader As StreamReader Dim V_TextLine As String Dim V_TextSplit(2) As String Const V_FileName As String = "HDTCHECKFIX.ini" V_FileReader = File.OpenText(V_FileName) V_TextLine = V_FileReader.ReadLine Do While Not V_TextLine = "" V_TextSplit = V_TextLine.Split("=") If V_TextSplit(0).ToUpper = "NAME" Then If Not V_Sensor.Name = "" Then AnalogSensors.Add(V_Sensor) End If V_Sensor.Name = "" V_Sensor.Name = V_TextSplit(1) End If If V_TextSplit(0).ToUpper = "SLOPE" Then V_Sensor.Slope = V_TextSplit(1) End If If V_TextSplit(0).ToUpper = "YINTERCEPT" Then V_Sensor.YIntercept = V_TextSplit(1) End If V_TextLine = V_FileReader.ReadLine Loop If Not V_Sensor.Name = "" Then AnalogSensors.Add(V_Sensor) End If V_Sensor.Name = "" V_FileReader.Close() End Sub </code> Thank you, If I read you code correctly, you are declaring V_Sensor as a new instance of
AnalogSensor before your loop then in your loop, you are using AnalogSensor.Add(V_Sensor). I don't know what your AnalogSensor class is but this doesn't make any sense! Why would you add an analogsensor to itself? What you might have in mind is: Dim myArrayList as New ArrayList Start loop Dim v_Sensor as new AnalogSensor ...... ..... MyArrayList.Add(v_Sensor) end loop Show quoteHide quote "blisspikle" wrote: > The following sub form1_load reads in a text file that will say > something like the text below. I think that the problem is that every > time I add a class V_Sensor into the arraylist it puts it in as a > reference. So in the end all of the arraylist items turn out to be the > same as the last sensor in the text file. I am new and I do not know > how to declare and put a class into an arraylist so it will be a > different reference for every loop. > > > <text> > NAME=SENSOR1 > SLOPE=1.1 > YINTERCEPT=1.2 > NAME=SENSOR2 > SLOPE=2.1 > YINTERCEPT=2.2 > etc.... > </text> > > <code> > Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles MyBase.Load > Dim V_Sensor As New AnalogSensor() > Dim V_FileReader As StreamReader > Dim V_TextLine As String > Dim V_TextSplit(2) As String > Const V_FileName As String = "HDTCHECKFIX.ini" > V_FileReader = File.OpenText(V_FileName) > V_TextLine = V_FileReader.ReadLine > Do While Not V_TextLine = "" > V_TextSplit = V_TextLine.Split("=") > If V_TextSplit(0).ToUpper = "NAME" Then > If Not V_Sensor.Name = "" Then > AnalogSensors.Add(V_Sensor) > End If > V_Sensor.Name = "" > V_Sensor.Name = V_TextSplit(1) > End If > If V_TextSplit(0).ToUpper = "SLOPE" Then > V_Sensor.Slope = V_TextSplit(1) > End If > If V_TextSplit(0).ToUpper = "YINTERCEPT" Then > V_Sensor.YIntercept = V_TextSplit(1) > End If > V_TextLine = V_FileReader.ReadLine > Loop > If Not V_Sensor.Name = "" Then > AnalogSensors.Add(V_Sensor) > End If > V_Sensor.Name = "" > V_FileReader.Close() > End Sub > </code> > > Thank you, > > Sorry. I guess that I forgot to mention that the "ANALOGSENSORS" that
was used throughout the code is a public arraylist. The code below seems to be working quite well after I messed with it. The part that is different is in the arraylist using ANALOGSENSORS.ADD(NEW ANALOGSENSOR()). I did not (still do not?) know how to create another instance of the analogsensor class for each do loop when the name in the text file changes. <code> Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim V_Sensor As New AnalogSensor() Dim V_FileReader As StreamReader Dim V_TextLine As String Dim V_TextSplit(2) As String Const V_FileName As String = "HDTCHECKFIX.ini" V_FileReader = File.OpenText(V_FileName) V_TextLine = V_FileReader.ReadLine Do While Not V_TextLine = "" V_TextSplit = V_TextLine.Split("=") If V_TextSplit(0).ToUpper = "NAME" Then AnalogSensors.Add(New AnalogSensor()) V_Sensor = AnalogSensors(AnalogSensors.Count - 1) End If If V_TextSplit(0).ToUpper = "SLOPE" Then V_Sensor.Slope = V_TextSplit(1) End If If V_TextSplit(0).ToUpper = "YINTERCEPT" Then V_Sensor.YIntercept = V_TextSplit(1) End If V_TextLine = V_FileReader.ReadLine Loop V_FileReader.Close() End Sub Public Class AnalogSensor Public Name As String Public Slope As Decimal Public Sub New() End Sub End Class </code> I guess the way I with that it would work is .... dim myclass1 as new class dim myarraylist1 as new arraylist for i = 0 to 100 myclass1 = new class myarraylist1(i).add(myclass1) myclass1.name = "bill " & i next My understanding is not very good yet. Based on what you have, it appears that your file might look something like
this: NAME=SENSOR1 SLOPE=0.75 YINTERCEPT=12 .... Another option, that won't force you to implicitly cast your V_Sensor to an Object (when you add it to the ArrayList) and then back to a V_Sensor immediately is to use a counter. This works if: 1) Every V_Sensor in your file is guaranteed to have all three entries, 2) NAME is guaranteed to be first every time Also note the .Split() line - I added ", 2" to it so that it will return a maximum of two items when is splits your string. Dim V_FileReader As StreamReader Dim V_TextLine As String Dim V_TextSplit(2) As String Const V_FileName As String = "HDTCHECKFIX.ini" V_FileReader = File.OpenText(V_FileName) V_TextLine = V_FileReader.ReadLine Dim V_Sensor As New AnalogSensor Dim AnalogSensors As New ArrayList Dim Counter As Integer = 0 Do While Not V_TextLine = "" V_TextSplit = V_TextLine.Split("=", 2) ' Initialize the counter to 1 when we read in the Name If V_TextSplit(0).ToUpper = "NAME" Then V_Sensor = New AnalogSensor V_Sensor.Name = V_TextSplit(1) Counter = 1 End If ' Increment the counter by one. If V_TextSplit(0).ToUpper = "SLOPE" Then V_Sensor.Slope = Convert.ToDecimal(V_TextSplit(1)) Counter += 1 End If ' Increment the counter by one. We explicitly convert the number to ' decimal here. If V_TextSplit(0).ToUpper = "YINTERCEPT" Then V_Sensor.YIntercept = Convert.ToDecimal(V_TextSplit(1)) Counter += 1 End If ' We only add the sensor after the third item is read If (Counter = 3) Then Counter = 0 AnalogSensors.Add(V_Sensor) End If V_TextLine = V_FileReader.ReadLine Loop V_FileReader.Close() Show quoteHide quote "blisspikle" <eklas***@metforming.com> wrote in message news:1112663124.398270.49690@l41g2000cwc.googlegroups.com... > Sorry. I guess that I forgot to mention that the "ANALOGSENSORS" that > was used throughout the code is a public arraylist. The code below > seems to be working quite well after I messed with it. The part that > is different is in the arraylist using ANALOGSENSORS.ADD(NEW > ANALOGSENSOR()). I did not (still do not?) know how to create another > instance of the analogsensor class for each do loop when the name in > the text file changes. > > > <code> > Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles MyBase.Load > Dim V_Sensor As New AnalogSensor() > Dim V_FileReader As StreamReader > Dim V_TextLine As String > Dim V_TextSplit(2) As String > Const V_FileName As String = "HDTCHECKFIX.ini" > V_FileReader = File.OpenText(V_FileName) > V_TextLine = V_FileReader.ReadLine > Do While Not V_TextLine = "" > V_TextSplit = V_TextLine.Split("=") > If V_TextSplit(0).ToUpper = "NAME" Then > AnalogSensors.Add(New AnalogSensor()) > V_Sensor = AnalogSensors(AnalogSensors.Count - 1) > End If > If V_TextSplit(0).ToUpper = "SLOPE" Then > V_Sensor.Slope = V_TextSplit(1) > End If > If V_TextSplit(0).ToUpper = "YINTERCEPT" Then > V_Sensor.YIntercept = V_TextSplit(1) > End If > V_TextLine = V_FileReader.ReadLine > Loop > V_FileReader.Close() > > End Sub > > Public Class AnalogSensor > Public Name As String > Public Slope As Decimal > Public Sub New() > End Sub > End Class > </code> > > > I guess the way I with that it would work is .... > > dim myclass1 as new class > dim myarraylist1 as new arraylist > for i = 0 to 100 > myclass1 = new class > myarraylist1(i).add(myclass1) > myclass1.name = "bill " & i > next > > My understanding is not very good yet. > Blispikle,
You cannot set a class in an arraylist. You can set an instanced object in an arraylist. And because you do that now do that only one time, are you placing all the time the same object in your arraylist. As advice, forget to instance datafields in the top of your program. That is from the Cobol time. Just do (in psuedo code) do while reading Dim V_Sensor As New AnalogSensor V_Sensoer.whatever = mydata myArraylist.Add(V_Sensoer) readnextone loop Now all those objects are made and not deleted because you have set a reference to it from the arraylist. And please don't use uppercases. That is called screaming in newsgroups. I hope this helps, Cor
VB.NET VERY Slow
sockets SQL connection two console apps. detect clicked column in datagrid Why "Application has generated an exception" error and not a good error message? How to check for scroll bars in web browser object? Transpose datagrid row to corresponding label text VB .NET 2k3 print directly to parallel port Upgrade from 6.0 (Printer) |
|||||||||||||||||||||||