Dynamic array with strong types?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vincent Finn

    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<typ e>

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

    Am I missing something obvious here?

    Vin
  • Vincent Finn

    #2
    Re: Dynamic array with strong types?

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

    Forgot the code

    private object[] AddToArray(obje ct[] aSrc, object aNew)
    {
    if (aSrc.GetType() == typeof(int[]))
    {
    int[] aArray = new int[aSrc.GetLength( 0) + 1];
    aSrc.CopyTo(aAr ray, 0);
    aSrc = aArray;
    aSrc[aSrc.GetLength( 0) - 1] = (int)aNew;
    }
    // and so on for other types
    return aSrc;
    }


    Comment

    • Ariel Popovsky

      #3
      Re: Dynamic array with strong types?

      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

      [color=blue]
      > 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<typ e>
      >
      > It seems like an astonishing oversight to leave that out of the
      > framework
      >
      > Am I missing something obvious here?
      >
      > Vin
      >[/color]

      Comment

      • Vincent Finn

        #4
        Re: Dynamic array with strong types?

        On Fri, 05 Nov 2004 04:42:20 -0800, Ariel Popovsky
        <apopovsky@hots pamNOmail.com> wrote:
        [color=blue]
        >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[/color]

        Still seems like an very bad oversight\decis ion

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

        Thanks, Vin

        Comment

        • Stoitcho Goutsev \(100\) [C# MVP]

          #5
          Re: Dynamic array with strong types?

          Hi Vincent,

          [color=blue]
          > Is there any way of having a strongly typed dynamic array in C#?[/color]
          There is nothing out of the box, I'm afraid.

          [color=blue]
          > 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?[/color]

          The frame work has bas classes (e.g CollectionBase) which are meant to be
          used for creating strongly typed collections. Look at
          System.Collecti ons.CollectionB ase class
          [color=blue]
          >
          > I want an equivalent to the C++ std::vector<typ e>
          >[/color]

          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]



          Comment

          • Vincent Finn

            #6
            Re: Dynamic array with strong types?

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

            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

            Comment

            • Stoitcho Goutsev \(100\) [C# MVP]

              #7
              Re: Dynamic array with strong types?

              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:jf9no0d40c ou6advpkdk8g6bj ftdl7i56v@4ax.c om...[color=blue]
              > On Fri, 5 Nov 2004 09:00:20 -0500, "Stoitcho Goutsev \(100\) [C# MVP]"
              > <100@100.com> wrote:
              >[color=green]
              >>Until then CollectionBase is the easiest way[/color]
              >
              > 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[/color]


              Comment

              • Justin Rogers

                #8
                Re: Dynamic array with strong types?

                > Is there an advatage in Deriving from CollectionBase rather than[color=blue]
                > ArrayList?[/color]

                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.
                [color=blue]
                > 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[/color]

                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


                Comment

                • Jeff Louie

                  #9
                  Re: Dynamic array with strong types?

                  Hi Vincent... I have some sample code here including a generic read only
                  wrapper at:
                  Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!

                  Regards,
                  Jeff[color=blue]
                  >Is there any way of having a strongly typed dynamic array in C#?<[/color]


                  *** Sent via Developersdex http://www.developersdex.com ***
                  Don't just participate in USENET...get rewarded for it!

                  Comment

                  • Vincent Finn

                    #10
                    Re: Dynamic array with strong types?

                    On Fri, 5 Nov 2004 15:21:59 -0500, "Stoitcho Goutsev \(100\) [C# MVP]"
                    <100@100.com> wrote:
                    [color=blue]
                    >You want strongy typed collection, right? Inheriting form ArrayList doesn't
                    >give you that.[/color]

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

                    Vin

                    Comment

                    • Vincent Finn

                      #11
                      Re: Dynamic array with strong types?

                      On Fri, 05 Nov 2004 22:00:13 -0800, Jeff Louie <jeff_louie@yah oo.com>
                      wrote:
                      [color=blue]
                      >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[/color]

                      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

                      Comment

                      • Stoitcho Goutsev \(100\) [C# MVP]

                        #12
                        Re: Dynamic array with strong types?

                        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:0037p05309 aqsbfrlplckfv3d 0afpc5el8@4ax.c om...[color=blue]
                        > On Fri, 05 Nov 2004 22:00:13 -0800, Jeff Louie <jeff_louie@yah oo.com>
                        > wrote:
                        >[color=green]
                        >>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[/color]
                        >
                        > 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[/color]


                        Comment

                        Working...