473,396 Members | 1,996 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,396 software developers and data experts.

Dynamic array with strong types?

Hi,

Is there any way of having a strongly typed dynamic array in C#?

ArrayList requires you to cast every time you want to access an
element
object[] isn't dynamic

I have resorted writing a hacky Add() function for object[] (below)

the other alternative was deriving a ArrayListBool etc.. from
ArrayList where the indexer did the cast and returned a strong type

but I would have though there must be a better way than these?

I want an equivalent to the C++ std::vector<type>

It seems like an astonishing oversight to leave that out of the
framework

Am I missing something obvious here?

Vin
Nov 16 '05 #1
11 5439
>I have resorted writing a hacky Add() function for object[] (below)

Forgot the code

private object[] AddToArray(object[] aSrc, object aNew)
{
if (aSrc.GetType() == typeof(int[]))
{
int[] aArray = new int[aSrc.GetLength(0) + 1];
aSrc.CopyTo(aArray, 0);
aSrc = aArray;
aSrc[aSrc.GetLength(0) - 1] = (int)aNew;
}
// and so on for other types
return aSrc;
}
Nov 16 '05 #2
Hello Vincent,

What you want are generic types, but they will be available only in the next version of the framework.
In .net 2.0 you can create an ArrayList<bool>, in 1.1 you have to extend the provided ArrayList as you did or write your own.
I recommend you download Codesmith and some collection´s templates that will help you automate the creation of strongly typed collections.

Codesmith: http://www.ericjsmith.net/codesmith/
Collection templates for Codesmith: http://www.kynosarges.de/Templates.html
Greetings,
-----
Ariel Popovsky

Hi,

Is there any way of having a strongly typed dynamic array in C#?

ArrayList requires you to cast every time you want to access an
element
object[] isn't dynamic
I have resorted writing a hacky Add() function for object[] (below)

the other alternative was deriving a ArrayListBool etc.. from
ArrayList where the indexer did the cast and returned a strong type

but I would have though there must be a better way than these?

I want an equivalent to the C++ std::vector<type>

It seems like an astonishing oversight to leave that out of the
framework

Am I missing something obvious here?

Vin


Nov 16 '05 #3
On Fri, 05 Nov 2004 04:42:20 -0800, Ariel Popovsky
<ap*******@hotspamNOmail.com> wrote:
Hello Vincent,

What you want are generic types, but they will be available only in the next version of the framework.
In .net 2.0 you can create an ArrayList<bool>, in 1.1 you have to extend the provided ArrayList as you did or write your own.
I recommend you download Codesmith and some collection´s templates that will help you automate the creation of strongly typed collections.

Codesmith: http://www.ericjsmith.net/codesmith/
Collection templates for Codesmith: http://www.kynosarges.de/Templates.html
Greetings,
-----
Ariel Popovsky


Still seems like an very bad oversight\decision

Oh well at least I know I amn't just missing the obvious

Thanks, Vin

Nov 16 '05 #4
Hi Vincent,

Is there any way of having a strongly typed dynamic array in C#? There is nothing out of the box, I'm afraid.

ArrayList requires you to cast every time you want to access an
element
object[] isn't dynamic

I have resorted writing a hacky Add() function for object[] (below)

the other alternative was deriving a ArrayListBool etc.. from
ArrayList where the indexer did the cast and returned a strong type

but I would have though there must be a better way than these?
The frame work has bas classes (e.g CollectionBase) which are meant to be
used for creating strongly typed collections. Look at
System.Collections.CollectionBase class

I want an equivalent to the C++ std::vector<type>


Generics (c# equivalents of C++'s templates) will be available in .NET 2.0.
Until then CollectionBase is the easiest way

--
HTH
Stoitcho Goutsev (100) [C# MVP]

Nov 16 '05 #5
On Fri, 5 Nov 2004 09:00:20 -0500, "Stoitcho Goutsev \(100\) [C# MVP]"
<10*@100.com> wrote:
Until then CollectionBase is the easiest way


Is there an advatage in Deriving from CollectionBase rather than
ArrayList?

If I derive from ArrayList I simply implement a 'new' indexer
deriving from CollectionBase leaves me having to implement everything
the implmentations are trivial but still a lot more code

Vin
Nov 16 '05 #6
You want strongy typed collection, right? Inheriting form ArrayList doesn't
give you that.

--

Stoitcho Goutsev (100) [C# MVP]
"Vincent Finn" <1@2.com> wrote in message
news:jf********************************@4ax.com...
On Fri, 5 Nov 2004 09:00:20 -0500, "Stoitcho Goutsev \(100\) [C# MVP]"
<10*@100.com> wrote:
Until then CollectionBase is the easiest way


Is there an advatage in Deriving from CollectionBase rather than
ArrayList?

If I derive from ArrayList I simply implement a 'new' indexer
deriving from CollectionBase leaves me having to implement everything
the implmentations are trivial but still a lot more code

Vin

Nov 16 '05 #7
> Is there an advatage in Deriving from CollectionBase rather than
ArrayList?
Collection base uses an ArrayList on the back-end so there is zero
benefit to using it, except that it is functional in adding validation
methods that don't exist on the ArrayList class.
If I derive from ArrayList I simply implement a 'new' indexer
deriving from CollectionBase leaves me having to implement everything
the implmentations are trivial but still a lot more code


It isn't difficult to write a strongly typed collection for each type of class.
In fact, I recall working on a tool back when I was the .NET QuickStarts
developer that dynamically emitted collection types scoped to a given
type. And I know there are several code template tools that would allow
you to write your collection once, and then replicate it for each type you
want to support.

--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
Nov 16 '05 #8
Hi Vincent... I have some sample code here including a generic read only
wrapper at:
http://www.geocities.com/jeff_louie/OOP/oop7.htm
Regards,
Jeff
Is there any way of having a strongly typed dynamic array in C#?<

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #9
On Fri, 5 Nov 2004 15:21:59 -0500, "Stoitcho Goutsev \(100\) [C# MVP]"
<10*@100.com> wrote:
You want strongy typed collection, right? Inheriting form ArrayList doesn't
give you that.


only in the return type from the indexer
don't care about internal structure

Vin
Nov 16 '05 #10
On Fri, 05 Nov 2004 22:00:13 -0800, Jeff Louie <je********@yahoo.com>
wrote:
Hi Vincent... I have some sample code here including a generic read only
wrapper at:
http://www.geocities.com/jeff_louie/OOP/oop7.htm
Regards,
Jeff


You went the route of deriving from CollectionBase to make it fully
strongly typed
I think I'll go the quick route of simply deriving from ArrayList to
make the indexer strong

Thanks, Vin
Nov 16 '05 #11
Vincent,

Yes, but in this case you'll end up having both indexers. With
CollectionBase in order to get not strongly typed indexer you need to cast
to IList (you can't get around this until generics). In your case strongly
typed and not strongly typed indexers are visible right away. Even more if
you inherit from CollectionBase you can control what is added to the
collection and throw an exception if it is not appropriate. If you
inheriting from ArrayList averyone can put any crap in your array.
The internal structure, though, in both cases is not strongly typed. So what
is easier is questionable.

--

Stoitcho Goutsev (100) [C# MVP]
"Vincent Finn" <1@2.com> wrote in message
news:00********************************@4ax.com...
On Fri, 05 Nov 2004 22:00:13 -0800, Jeff Louie <je********@yahoo.com>
wrote:
Hi Vincent... I have some sample code here including a generic read only
wrapper at:
http://www.geocities.com/jeff_louie/OOP/oop7.htm
Regards,
Jeff


You went the route of deriving from CollectionBase to make it fully
strongly typed
I think I'll go the quick route of simply deriving from ArrayList to
make the indexer strong

Thanks, Vin

Nov 16 '05 #12

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

Similar topics

7
by: JavaScriptRocks | last post by:
I've been trying to imitate / reverse engineer the add attachment feature in gmail composer. I managed to do it to say about 80% but its giving me trouble in IE on WinXP-Sp2. I am using PHP to do...
0
by: Pat Patterson | last post by:
I'm having serious issues with a page I'm developing. I just need some simple help, and was hoping someone might be able to help me out in here. I have a form, that consists of 3 pages of...
6
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
2
by: Martin | last post by:
Hi, I need to create the html below with dynamic controls <span class="myclass"> <h3>text1</h3> <h4><span>Price <strong>text2</strong> | Duration <strong>text3</strong></span></h4> </span>
4
by: Zark3 | last post by:
Hi all, I was wondering if anybody could enlighten me on the possibility of dynamic casting. Or, well, whether or not I'm actually trying to do this the right way. What I have is a base class...
11
by: Sean M. DonCarlos | last post by:
I have an unmanaged Win32 app that looks up the name of a DLL (unknown at compile time) from an external location, loads it with LoadLibrary, and then uses GetProcAddress on three exported...
2
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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,...

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.