PC Review


Reply
Thread Tools Rate Thread

ArrayList, Array or something else

 
 
Maileen
Guest
Posts: n/a
 
      13th Aug 2005
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
 
Reply With Quote
 
 
 
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      13th Aug 2005
"Maileen" <(E-Mail Removed)> 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/>
 
Reply With Quote
 
Maileen
Guest
Posts: n/a
 
      14th Aug 2005
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" <(E-Mail Removed)> 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)
> ...
> ///
>

 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      14th Aug 2005
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


 
Reply With Quote
 
m.posseth
Guest
Posts: n/a
 
      14th Aug 2005

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]" <hirf-spam-me-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Maileen" <(E-Mail Removed)> 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/>



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      14th Aug 2005
"m.posseth" <(E-Mail Removed)> 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/>

 
Reply With Quote
 
_AnonCoward
Guest
Posts: n/a
 
      14th Aug 2005

"m.posseth" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
:
: 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.


 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      14th Aug 2005
"_AnonCoward" <(E-Mail Removed)> 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/>

 
Reply With Quote
 
_AnonCoward
Guest
Posts: n/a
 
      14th Aug 2005

"Herfried K. Wagner [MVP]" <hirf-spam-me-(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
:
: "_AnonCoward" <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ArrayList's Array? Jia Lu Microsoft C# .NET 1 15th Apr 2009 04:10 AM
Array not ArrayList John Sutor Microsoft C# .NET 1 6th Apr 2005 12:59 PM
arraylist to array =?Utf-8?B?Q2hyaXN0aW5l?= Microsoft C# .NET 8 8th Oct 2004 01:59 PM
array in a arraylist Ricardo Microsoft C# .NET 4 5th May 2004 03:41 AM
Array to ArrayList Jerry S Microsoft C# .NET 4 1st Nov 2003 03:07 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:41 PM.