Array of arrays

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

    #1

    Array of arrays

    I need to make a two dimensional array of objects. These objects are of type
    Influence[].
    How do I declare such an array?

    Now I use:
    private object[,] influences;
    influences[x, y] = GetInfluences() ; // GetInfluences returns type
    Influence[]

    But when I need to use something from te array I have to typecast from
    object to Influence[].
    I would like to have a two dimensional array of type Influence[] in stead of
    type object.

    private Influence[][,] influences; // This does compile
    influences[x, y] = GetInfluences() ; // Compile error : Wrong number of
    indices inside [], expected '1'

    Does anyone have an idea?

    Thanks in advance,
    Kurzweil


  • mk

    #2
    Array of arrays

    Hi,
    Look at your individual declarations, where you create
    the initial object array, you declare it as [,]. Where
    you create the Influences array, you declare it as [][,]!
    Unless you've changed the GetInfluences() method, you're
    not going to return the correct number of dimensions.
    I suspect that this is just a syntax error on your part.

    HTH,

    martin


    [color=blue]
    >-----Original Message-----
    >I need to make a two dimensional array of objects. These[/color]
    objects are of type[color=blue]
    >Influence[].
    >How do I declare such an array?
    >
    >Now I use:
    >private object[,] influences;
    >influences[x, y] = GetInfluences() ; // GetInfluences[/color]
    returns type[color=blue]
    >Influence[]
    >
    >But when I need to use something from te array I have to[/color]
    typecast from[color=blue]
    >object to Influence[].
    >I would like to have a two dimensional array of type[/color]
    Influence[] in stead of[color=blue]
    >type object.
    >
    >private Influence[][,] influences; // This does compile
    >influences[x, y] = GetInfluences() ; // Compile error :[/color]
    Wrong number of[color=blue]
    >indices inside [], expected '1'
    >
    >Does anyone have an idea?
    >
    >Thanks in advance,
    >Kurzweil
    >
    >
    >.
    >[/color]

    Comment

    • Jon Skeet

      #3
      Re: Array of arrays

      Kurzweil <raykurz_@hotma il.com> wrote:

      <snip>
      [color=blue]
      > I would like to have a two dimensional array of type Influence[] in stead of
      > type object.
      >
      > private Influence[][,] influences; // This does compile
      > influences[x, y] = GetInfluences() ; // Compile error : Wrong number of
      > indices inside [], expected '1'
      >
      > Does anyone have an idea?[/color]

      Yes - you've declared it the wrong way round. You meant:

      private Influence[,][] influences;

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Oscar Papel

        #4
        Re: Array of arrays

        Hello,

        I would suggest that you create a strongly typed collection to help simplify
        things.

        I would suggest two collections in this case:
        InfluenceMatrix and InfluenceCollec tion
        InfluenceMatrix would hold only InfluenceCollec tion objects
        That way, your code would look like this:

        private InfluenceMatrix influences=new InfluenceMatrix ();
        influences[x,y]=GetInfluences( ) // change GetInfluences to return
        InfluenceCollec tion instead of Influence[]

        There are many tools that generate strongly typed collections if you don't
        want to write the boilerplate code yourself.
        This allows you to separate interface from implementation in this case.
        It also simplifies passing the data around in your program.
        Finally, the collections themselves can be implemented using arrays so that
        you have an O(1) get/set just like an array.
        It will also simplify your documentation and make the users of the
        collections much happier.

        Oscar Papel.

        "Kurzweil" <raykurz_@hotma il.com> wrote in message
        news:1060674537 .412092@newsrea der2.wirehub.nl ...[color=blue]
        > I need to make a two dimensional array of objects. These objects are of[/color]
        type[color=blue]
        > Influence[].
        > How do I declare such an array?
        >
        > Now I use:
        > private object[,] influences;
        > influences[x, y] = GetInfluences() ; // GetInfluences returns type
        > Influence[]
        >
        > But when I need to use something from te array I have to typecast from
        > object to Influence[].
        > I would like to have a two dimensional array of type Influence[] in stead[/color]
        of[color=blue]
        > type object.
        >
        > private Influence[][,] influences; // This does compile
        > influences[x, y] = GetInfluences() ; // Compile error : Wrong number of
        > indices inside [], expected '1'
        >
        > Does anyone have an idea?
        >
        > Thanks in advance,
        > Kurzweil
        >
        >[/color]


        Comment

        Working...