473,785 Members | 2,919 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ARRAYLIST ADDING A CLASS

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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) 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.in i"
V_FileReader = File.OpenText(V _FileName)
V_TextLine = V_FileReader.Re adLine
Do While Not V_TextLine = ""
V_TextSplit = V_TextLine.Spli t("=")
If V_TextSplit(0). ToUpper = "NAME" Then
If Not V_Sensor.Name = "" Then
AnalogSensors.A dd(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.YInter cept = V_TextSplit(1)
End If
V_TextLine = V_FileReader.Re adLine
Loop
If Not V_Sensor.Name = "" Then
AnalogSensors.A dd(V_Sensor)
End If
V_Sensor.Name = ""
V_FileReader.Cl ose()
End Sub
</code>

Thank you,

Nov 21 '05 #1
4 1323
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.Ad d(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
"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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) 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.in i"
V_FileReader = File.OpenText(V _FileName)
V_TextLine = V_FileReader.Re adLine
Do While Not V_TextLine = ""
V_TextSplit = V_TextLine.Spli t("=")
If V_TextSplit(0). ToUpper = "NAME" Then
If Not V_Sensor.Name = "" Then
AnalogSensors.A dd(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.YInter cept = V_TextSplit(1)
End If
V_TextLine = V_FileReader.Re adLine
Loop
If Not V_Sensor.Name = "" Then
AnalogSensors.A dd(V_Sensor)
End If
V_Sensor.Name = ""
V_FileReader.Cl ose()
End Sub
</code>

Thank you,

Nov 21 '05 #2
Sorry. I guess that I forgot to mention that the "ANALOGSENS ORS" 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.A DD(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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) 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.in i"
V_FileReader = File.OpenText(V _FileName)
V_TextLine = V_FileReader.Re adLine
Do While Not V_TextLine = ""
V_TextSplit = V_TextLine.Spli t("=")
If V_TextSplit(0). ToUpper = "NAME" Then
AnalogSensors.A dd(New AnalogSensor())
V_Sensor = AnalogSensors(A nalogSensors.Co unt - 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.YInter cept = V_TextSplit(1)
End If
V_TextLine = V_FileReader.Re adLine
Loop
V_FileReader.Cl ose()

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.

Nov 21 '05 #3
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.in i"
V_FileReader = File.OpenText(V _FileName)
V_TextLine = V_FileReader.Re adLine
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.Spli t("=", 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.ToDecim al(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.YInter cept = Convert.ToDecim al(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.A dd(V_Sensor)
End If
V_TextLine = V_FileReader.Re adLine
Loop
V_FileReader.Cl ose()
"blisspikle " <ek******@metfo rming.com> wrote in message
news:11******** *************@l 41g2000cwc.goog legroups.com...
Sorry. I guess that I forgot to mention that the "ANALOGSENS ORS" 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.A DD(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(ByVa l sender As System.Object, ByVal e As
System.EventArg s) 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.in i"
V_FileReader = File.OpenText(V _FileName)
V_TextLine = V_FileReader.Re adLine
Do While Not V_TextLine = ""
V_TextSplit = V_TextLine.Spli t("=")
If V_TextSplit(0). ToUpper = "NAME" Then
AnalogSensors.A dd(New AnalogSensor())
V_Sensor = AnalogSensors(A nalogSensors.Co unt - 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.YInter cept = V_TextSplit(1)
End If
V_TextLine = V_FileReader.Re adLine
Loop
V_FileReader.Cl ose()

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.

Nov 21 '05 #4
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.whate ver = 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
Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1718
by: Hans De Schrijver | last post by:
I have a private ArrayList variable that holds objects of various types, though they're all derived from a common base class (User). What I would like to do is provide public accessor properties per type. I have written some code that does the trick now, but it involves looping through the private ArrayList and creating a new Array with just the objects of the type corresponding to the property. Problem is, this hapens every time you...
20
5988
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the arraylist then I have to handle this with a sort method that uses comparer. I can reference the properties of the Arraylist directly such as dim mylist as new FrameList mylist.Add(new FrameStructure) mylist(0).first = "blabla..." mylist(0).second...
18
4750
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the...
18
3255
by: Sam | last post by:
Hi All I'm planing to write an application which allows users dynamically add their points (say you can add upto 30,000) and then draw xy graph. Should I use an array for my coordinate point storage and dynamically resize it when there is a new point or should I use ArrayList? Is speed noticable between the two? Regards,
6
3142
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it to the client. Before adding data to the arraylist, I check if the depth of the arraylist is longer than iMaxQueueDepth, and if it is, I clear the arraylist. Is it possible that while I am clearing the arraylist, the ThreadMain at the same time...
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10087
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.