473,414 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

ArrayList, Array or something else

Hi,

I would like to have something like an array (something like a
collection) in order to store the following things :

company name1, new1, old1, in1, out1
company name2, new2, old2, in2, out2
....

but i should be able to dynamically redimension it.
all data will be strings only.

I was thinking to use an ArrayList, but how to do a 2-dim Arraylist of
string ?

thanks a lot,
Maileen
Nov 21 '05 #1
8 1484
"Maileen" <no*****@nospam.com> schrieb:
I would like to have something like an array (something like a
collection) in order to store the following things :

company name1, new1, old1, in1, out1
company name2, new2, old2, in2, out2
...

but i should be able to dynamically redimension it.
all data will be strings only.

\\\
Public Class Company
Public Name As String
Public [New] As String
Public Old As String
Public ...
End Class
..
..
..
Dim a As New ArrayList()
Dim c As New Company()
....
a.Add(c)
....
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #2
Thanks a lot.
I was thinking about such thing, but i was not sure that it could be
possible.
once again thanks.

Maileen

Herfried K. Wagner [MVP] wrote:
"Maileen" <no*****@nospam.com> schrieb:
I would like to have something like an array (something like a
collection) in order to store the following things :

company name1, new1, old1, in1, out1
company name2, new2, old2, in2, out2
...

but i should be able to dynamically redimension it.
all data will be strings only.


\\\
Public Class Company
Public Name As String
Public [New] As String
Public Old As String
Public ...
End Class
.
.
.
Dim a As New ArrayList()
Dim c As New Company()
...
a.Add(c)
...
///

Nov 21 '05 #3
Maileen,

What you describe is a DataTable.

Or a class build with collectionbase however in my opinion that needs more
work and endless changing to get the possibilities from a DataTable, when
more functionality is wanted.

I hope this helps,

Cor
Nov 21 '05 #4

Herfried ,,,

just curious why you choose this aproach

Wouldn`t it be better to use a structure in this situation ( as it is more
lightweight ) ?

regards

Michel Posseth
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:OT*************@tk2msftngp13.phx.gbl...
"Maileen" <no*****@nospam.com> schrieb:
I would like to have something like an array (something like a
collection) in order to store the following things :

company name1, new1, old1, in1, out1
company name2, new2, old2, in2, out2
...

but i should be able to dynamically redimension it.
all data will be strings only.

\\\
Public Class Company
Public Name As String
Public [New] As String
Public Old As String
Public ...
End Class
.
.
.
Dim a As New ArrayList()
Dim c As New Company()
...
a.Add(c)
...
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5
"m.posseth" <mi*****@nohausystems.nl> schrieb:
just curious why you choose this aproach

Wouldn`t it be better to use a structure in this situation ( as it is
more lightweight ) ?


Well, that's hard to decide without further information. Structures
generally should be small (<= 16 bytes), otherwise classes are preferrable.
BTW: I forgot to mention that it might be better to use properties instead
of the public variables.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #6

"m.posseth" <mi*****@nohausystems.nl> wrote in message
news:Oy**************@TK2MSFTNGP14.phx.gbl...
:
: Herfried ,,,
:
: just curious why you choose this aproach
:
: Wouldn`t it be better to use a structure in this situation
: ( as it is more lightweight ) ?
:
: regards
:
: Michel Posseth
Structures can have unexpected behavior in these sort of applications.
Consider the following:
-----------------------------
Public Class Cls
Public Parm As String
End Class

Public Class [class]
Public Shared Sub Main
Dim A As New ArrayList
Dim c As New Cls

c.Parm = "One"
A.Add(c)
Console.WriteLine(A.Item(0).Parm)

A.Item(0).Parm = "Two"
Console.WriteLine(A.Item(0).Parm)
End Sub
End Class
-----------------------------

The output from this is:

One
Two

However, change the class to a structure and then try:
-----------------------------
Public Structure Cls
Public Parm As String
End Structure

Public Class [class]
Public Shared Sub Main
Dim A As New ArrayList
Dim c As New Cls
c.Parm = "One"
A.Add(c)
Console.WriteLine(A.Item(0).Parm)

'here is the offending line
A.Item(0).Parm = "Two"
Console.WriteLine(A.Item(0).Parm)
End Sub
End Class
-----------------------------
This time, you get a runtime error.

Latebound assignment to a field of value type 'Cls' is
not valid when 'Cls' is the result of a latebound expression.
As I understand it, this is a limitation of the framework, not VB.Net,
because C# will generate a similar error.
In addition, structures are value types and copy assignments may not
behave as expected.

-----------------------------
Public Structure Cls
Public Parm As String
End Structure

Public Class [class]
Public Shared Sub Main
Dim A As New ArrayList
Dim c As New Cls
Dim c2 As Cls

c.Parm = "One"
A.Add(c)
Console.WriteLine(A.Item(0).Parm)

'we're grabbing the VALUE type at item 0
'and copying to the c2 variable
c2 = A.Item(0)

'the change to the copy is not reflected
'in the original item
c2.Parm = "Two"
Console.WriteLine(A.Item(0).Parm)
End Sub
End Class

-----------------------------

The output from this is:

One
One
Change this to a class again and you'll get different results:
-----------------------------
Public Structure Cls
Public Parm As String
End Structure

Public Class [class]
Public Shared Sub Main
Dim A As New ArrayList
Dim c As New Cls
Dim c2 As Cls
'we're grabbing the REFERENCE type at item 0
'and copying the reference to the c2 variable
c.Parm = "One"
A.Add(c)
Console.WriteLine(A.Item(0).Parm)

'the change to the copy IS reflected
'in the original item
c2 = A.Item(0)
c2.Parm = "Two"
Console.WriteLine(A.Item(0).Parm)
End Sub
End Class

-----------------------------
The output from this is:

One
Two
The recommendation I've seen regarding classes vs. structures is you
should normally stick with classes unless you have a specific need to do
otherwise.
<snip>
Ralf
--
----------------------------------------------------------
* ^~^ ^~^ *
* _ {~ ~} {~ ~} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *
----------------------------------------------------------
There are no advanced students in Aikido - there are only
competent beginners. There are no advanced techniques -
only the correct application of basic principles.
Nov 21 '05 #7
"_AnonCoward" <ab*@xyz.com> schrieb:
-----------------------------
Public Structure Cls
Public Parm As String
End Structure

Public Class [class]
Public Shared Sub Main
Dim A As New ArrayList
Dim c As New Cls
c.Parm = "One"
A.Add(c)
Console.WriteLine(A.Item(0).Parm)

'here is the offending line
A.Item(0).Parm = "Two"
Console.WriteLine(A.Item(0).Parm)
End Sub
End Class
-----------------------------
This time, you get a runtime error.

Latebound assignment to a field of value type 'Cls' is
not valid when 'Cls' is the result of a latebound expression.
As I understand it, this is a limitation of the framework, not VB.Net,
because C# will generate a similar error.

One thing you can do is using 'DirectCast':

\\\
DirectCast(A.Item(0), Cls).Parm = "Two"
///

However, note that this won't work with structures, because a copy of the
actual object will be returned, and changing a property of the copy doesn't
make any sense.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #8

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
:
: "_AnonCoward" <ab*@xyz.com> schrieb:
: >
: > -----------------------------
: > Public Structure Cls
: > Public Parm As String
: > End Structure
: >
: > Public Class [class]
: > Public Shared Sub Main
: > Dim A As New ArrayList
: > Dim c As New Cls
: >
: >
: > c.Parm = "One"
: > A.Add(c)
: > Console.WriteLine(A.Item(0).Parm)
: >
: > 'here is the offending line
: > A.Item(0).Parm = "Two"
: >
: >
: > Console.WriteLine(A.Item(0).Parm)
: > End Sub
: > End Class
: > -----------------------------
: >
: >
: > This time, you get a runtime error.
: >
: > Latebound assignment to a field of value type 'Cls' is
: > not valid when 'Cls' is the result of a latebound expression.
: >
: >
: > As I understand it, this is a limitation of the framework, not
: > VB.Net, because C# will generate a similar error.
:
:
: One thing you can do is using 'DirectCast':
:
: \\\
: DirectCast(A.Item(0), Cls).Parm = "Two"
: ///
:
: However, note that this won't work with structures, because a copy of
: the actual object will be returned, and changing a property of the
: copy doesn't make any sense.
Agreed. In this case, it gets rid of the error message but you still
can't achieve the desired effect - you need a class for this.
The problems with structures go even deeper. Consider this variation:

---------------------------------
Public Structure Struct
Public n As Integer
End Structure

Public Class Cls
Private s As New Struct

Public Property Str() As Struct
Get
Return s
End Get
Set
s = Value
End Set
End Property
End Class
Public Class [class]
Public Shared Sub Main
Dim c As New Cls
c.Str.n = 1
End Sub
End Class
---------------------------------
This generates the compiler following error:
Expression is a value and therefore cannot be
the target of an assignment.

c.Str.n = 1
~~~~~~~

You cannot access the value type "Struct" directly from the parent Cls
object. However, if Struct is declared as a class, this compiles just
fine.
This is easy enough to work around, of course - just make the following
modification (however, this does serve to illustrate how quirky
structures are):
---------------------------------
Public Class [class]
Public Shared Sub Main
Dim c As New Cls
Dim s As Struct = c.Str
s.n = 1
End Sub
End Class
---------------------------------
As long as we're on this subject, and going back to the original
question of why a class instead of a structure, there is another
consideration as well:
<modification to original code example>

\\\
Public Structure Company
Public Name As String
Public [New] As String
Public Old As String
Public ...
End Structure
..
..
..
Dim a As New ArrayList()
Dim c As New Company()
....
a.Add(c)
///
As I understand it, since the ArrayList.Add function only accepts
objects, inserting a value type (i.e.: the Structure 'Company' above)
will involve boxing and unboxing. There are expensive operations that
would offset any gains realized by using a structure in this case.
Ralf
--
----------------------------------------------------------
* ^~^ ^~^ *
* _ {~ ~} {~ ~} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *
----------------------------------------------------------
There are no advanced students in Aikido - there are only
competent beginners. There are no advanced techniques -
only the correct application of basic principles.
Nov 21 '05 #9

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

Similar topics

0
by: Stephen | last post by:
I have been getting on well with help from this forum trying to create an array list and work with it. Everything is working fine apart from displaying my array list items into the labels in my...
6
by: Dan V. | last post by:
I would like to create a 2D string list (2D ArrayList ???). I would like to pass in a table or query as a parameter and have both columns transform into a 2D ArrayList. When I sort the one...
9
by: vbportal | last post by:
Hi, I would like to add BitArrays to an ArrayList and then remove any duplicates - can someone please help me forward. I seem to have (at leaset ;-) )2 problems/lack of understanding (see test...
2
by: RBCC | last post by:
I have a form with a 2 textboxes and a listbox, The data is read in by a file called "memberphones" The listnbox lists names in this way (lastname,first name) sorted by the last name. the...
2
by: Russ Green | last post by:
I am currently working on a small VB.NET utility that accesses the Microstation VBA object model via COM. I have this code which I am trying to get to work... The key bits of information are ...
3
by: Sam | last post by:
Hi Everyone, I have a stucture below stored in an arraylist and I want to check user's input (point x,y) to make sure there is no duplicate point x,y entered (string label can be duplicated). Is...
16
by: Allen | last post by:
I have a class that returns an arraylist. How do I fill a list box from what is returned? It returns customers which is a arraylist but I cant seem to get the stuff to fill a list box. I just...
20
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...
18
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...
14
by: Kym | last post by:
Hey, I have an arraylist which I have added a number of objects to (all the same type of object), I need to be able to check if the array already contain the object before adding it, I have tried...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...
0
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...

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.