Re: Help with declaring and populating an array

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

    Re: Help with declaring and populating an array

    On Thu, 14 Aug 2008 18:56:00 -0700, Phill
    <Phill@discussi ons.microsoft.c omwrote:
    I have a table that contains 7 row of data with 8 columns. The columns
    contain decimal data. Looks like this:
    1 .5 .5 0 0 0 0 0 0
    2 .25 .25 .25 0 0 0 0 0
    etc.
    The first column tells me how many points and I will use this as my
    index in
    the array. I want to load this table into an array to be used in some
    calculations and i don't want to have to read the database everytime they
    change the number of points to calculate. I am new to c# and am having
    a lot
    of trouble declaring my array and populating it. This is what I have so
    far
    but it won't compile because the array is defined wrong..I think???
    For future reference, if you are asking for help with an error (compile or
    execution), you really should post the complete text of the error and be
    specific about when and where it happens.

    That said, in your code it's clear what's wrong:
    double[,] arrULD=new int[8];
    If you declare a multi-dimensional array, you have to allocate one. And
    you can't allocate just one dimension. A proper allocation would look
    like "new int[rows, 8]" (if "rows" contains the number of rows you want)
    or "new int[7, 8]" (if you know ahead of time you want 7 rows), or
    whatever.

    Now, that said, based on the code you posted, it seems as though maybe you
    don't really want a multidimensiona l array anyway. In your loop, you are
    setting an element of a single-dimension array to an instance of another
    single-dimensional array. For that, your array initialization would look
    more like this:

    double[][] arrULD = new double[][7]; // 7 rows of data

    Then the code in your loop would work fine.

    Finally, don't forget that C# arrays are 0-based. :)

    Pete
  • =?Utf-8?B?UGhpbGw=?=

    #2
    Re: Help with declaring and populating an array

    Thanks for the response. This line is still giving an error:
    while (rdr.Read())
    {
    arrULD[(int)rdr["NumberOfPoints "]] = {rdr[0],rdr[1]};

    //{rdr[0],rdr[1]};
    }
    The error is ";" expected in this part {rdr[0],rdr[1]};

    "Peter Duniho" wrote:
    On Thu, 14 Aug 2008 18:56:00 -0700, Phill
    <Phill@discussi ons.microsoft.c omwrote:
    >
    I have a table that contains 7 row of data with 8 columns. The columns
    contain decimal data. Looks like this:
    1 .5 .5 0 0 0 0 0 0
    2 .25 .25 .25 0 0 0 0 0
    etc.
    The first column tells me how many points and I will use this as my
    index in
    the array. I want to load this table into an array to be used in some
    calculations and i don't want to have to read the database everytime they
    change the number of points to calculate. I am new to c# and am having
    a lot
    of trouble declaring my array and populating it. This is what I have so
    far
    but it won't compile because the array is defined wrong..I think???
    >
    For future reference, if you are asking for help with an error (compile or
    execution), you really should post the complete text of the error and be
    specific about when and where it happens.
    >
    That said, in your code it's clear what's wrong:
    >
    double[,] arrULD=new int[8];
    >
    If you declare a multi-dimensional array, you have to allocate one. And
    you can't allocate just one dimension. A proper allocation would look
    like "new int[rows, 8]" (if "rows" contains the number of rows you want)
    or "new int[7, 8]" (if you know ahead of time you want 7 rows), or
    whatever.
    >
    Now, that said, based on the code you posted, it seems as though maybe you
    don't really want a multidimensiona l array anyway. In your loop, you are
    setting an element of a single-dimension array to an instance of another
    single-dimensional array. For that, your array initialization would look
    more like this:
    >
    double[][] arrULD = new double[][7]; // 7 rows of data
    >
    Then the code in your loop would work fine.
    >
    Finally, don't forget that C# arrays are 0-based. :)
    >
    Pete
    >

    Comment

    • Peter Duniho

      #3
      Re: Help with declaring and populating an array

      On Thu, 14 Aug 2008 21:42:03 -0700, Phill
      <Phill@discussi ons.microsoft.c omwrote:
      Thanks for the response. This line is still giving an error:
      while (rdr.Read())
      {
      arrULD[(int)rdr["NumberOfPoints "]] = {rdr[0],rdr[1]};
      >
      //{rdr[0],rdr[1]};
      }
      The error is ";" expected in this part {rdr[0],rdr[1]};
      Sorry, yes...the short-hand collection notation is allowed only in
      initializers. Change the right-hand-side to "new double[] { rdr[0],
      rdr[1] }" and it should work.

      For future reference, you should become familiar with the C# language
      reference on MSDN. It has lots of information about basic syntax for
      expressions, assignments, declarations, etc.

      Pete

      Comment

      • =?Utf-8?B?UGhpbGw=?=

        #4
        Re: Help with declaring and populating an array

        Thanks, I have looked at many sample, none are helping me though. Now I get
        error saying cannot convert type double[] to double on this line:
        arrULD[(int)rdr["NumberOfPoints "]] = new double[] {
        (double)rdr[0], (double)rdr[1], (double)rdr[2], (double)rdr[3],
        (double)rdr[4], (double)rdr[5], (double)rdr[6] };

        This should not be rocket science....urrr r

        "Peter Duniho" wrote:
        On Thu, 14 Aug 2008 21:42:03 -0700, Phill
        <Phill@discussi ons.microsoft.c omwrote:
        >
        Thanks for the response. This line is still giving an error:
        while (rdr.Read())
        {
        arrULD[(int)rdr["NumberOfPoints "]] = {rdr[0],rdr[1]};

        //{rdr[0],rdr[1]};
        }
        The error is ";" expected in this part {rdr[0],rdr[1]};
        >
        Sorry, yes...the short-hand collection notation is allowed only in
        initializers. Change the right-hand-side to "new double[] { rdr[0],
        rdr[1] }" and it should work.
        >
        For future reference, you should become familiar with the C# language
        reference on MSDN. It has lots of information about basic syntax for
        expressions, assignments, declarations, etc.
        >
        Pete
        >

        Comment

        • Peter Duniho

          #5
          Re: Help with declaring and populating an array

          On Fri, 15 Aug 2008 19:22:11 -0700, Phill
          <Phill@discussi ons.microsoft.c omwrote:
          Thanks, I have looked at many sample, none are helping me though. Now I
          get
          error saying cannot convert type double[] to double on this line:
          arrULD[(int)rdr["NumberOfPoints "]] = new double[] {
          (double)rdr[0], (double)rdr[1], (double)rdr[2], (double)rdr[3],
          (double)rdr[4], (double)rdr[5], (double)rdr[6] };
          That suggests that "arrULD" is declared as "double[]" rather than
          "double[][]". Unfortunately, you haven't shown all of the code, so
          there's no way for us to say exactly what's wrong for sure.

          I'm also a little confused, since your first post said you have 7 rows and
          8 columns of data, but you seem to be defining here a row of data with 7
          columns. But that wouldn't be the compiler error issue, so maybe I
          shouldn't concern myself with that.

          Pete

          Comment

          Working...