ArrayList, Array or something else

M

Maileen

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
 
H

Herfried K. Wagner [MVP]

Maileen said:
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

Maileen

Thanks a lot.
I was thinking about such thing, but i was not sure that it could be
possible.
once again thanks.

Maileen
Maileen said:
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)
...
///
 
C

Cor Ligthert [MVP]

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
 
M

m.posseth

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 said:
Maileen said:
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)
...
///
 
H

Herfried K. Wagner [MVP]

m.posseth said:
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.
 
A

_AnonCoward

:
: 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
--
 
H

Herfried K. Wagner [MVP]

_AnonCoward said:
-----------------------------
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.
 
A

_AnonCoward

:
: >
: > -----------------------------
: > 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
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top