[Mono-dev] Arrays in a loop?

SigmaX scottclansman at cwazy.co.uk
Mon Oct 3 15:21:31 EDT 2005


Martin Hinks wrote:
>Hi Eric,
>
>First off, why are you enclosing the array name in curly braces?
>MyArray[i, 0] should do the trick...
>  
Heh; I meant to imply that that was just an example array name.  I 
should have just left them out of the example, as they were unnecessary.
>Secondly, how is your array defined? Have you checked that the
>exception thrown isn't an "Out of range exception"? If it IS an
>out-of-range exception you need to declare your array differently, if
>not, post the declaration as well and i'll try to help more.
>
>Thanks,
>
>Martin Hinks
>  
Thanx for your time.

It seems the problem was not, as I first supposed, directly related to 
the array.  Actually the example I gave compiles fine... but it wasn't 
exactly what I was doing :-P.

The actual loop is as follows:

    foreach ( DataTable table in loaded_chart.Tables )
                {
                    // Repeat for each row in the table.
                    int i = 0;

                    foreach ( DataRow row in table.Rows )
                        {                   
                            lms_table[i, 0] =
    Convert.ToSingle(row[keytext]);
                            lms_table[i, 1] = Convert.ToSingle(row["L"]);
                            lms_table[i, 2] = Convert.ToSingle(row["M"]);
                            lms_table[i, 3] = Convert.ToSingle(row["S"]);
    Console.WriteLine("Round {0}, Key = {1}, L = {2}, M = {3}, S = {4}",
    i, lms_table[i, 0], lms_table[i, 1], lms_table[i, 2], lms_table[i, 3]);
                           
                            i ++;
                       }
                }


The exception I get is:

    Unhandled Exception: System.InvalidCastException: Cannot cast from
    source type to destination type.
    in <0x0001d> System.DBNull:System.IConvertible.ToSingle
    (IFormatProvider provider)
    in <0x00086> System.Convert:ToSingle (System.Object value,
    IFormatProvider provider)
    in <0x00016> System.Convert:ToSingle (System.Object value)
    in <0x00277> CDurry.coredurry:lms_load (System.String chart, Int32
    gender)
    in <0x0024a> consdurry:Main ()

    The weird thing is that it executes once flawlessly, giving me the 
first row of the dataset printed on the screen; the exception is thrown 
the second time around.
    The array is declared at the first line of the class as:

    static float[,] lms_table = new float[1000, 4];

    Any help?
             Thanx again,
               SigmaX

>On 10/3/05, Eric Scott <scottishbug at cwazy.co.uk> wrote:
>  
>>    I have a newbie C# question that I've been having difficulties
>>finding an answer to.  I have a global array, and am trying to assign
>>data to it from as follows:
>>
>>                int i = 0;
>>
>>                foreach ( DataRow row in table.Rows )
>>                    {
>>                        {MyArray}[i, 0] = a;
>>                        {MyArray}[i, 1] = x;
>>                        {MyArray}[i, 2] = y;
>>                        {MyArray}[i, 3] = z;
>>
>>                        i ++;
>>                   }
>>
>>    All the code executes properly once, but then stalls the second time
>>it attempts to go through the loop.  I've gathered there's something
>>quarky about using arrays in a loop like this, but I haven't found any
>>down-to-earth explanations that let me know how to fix my code.
>>    Thanx for any help you can offer,
>>             SigmaX
>>
>>--
>>My home page: www.sigmax.cjb.net
>>
>>"Education is what remains after one has forgotten everything he learned in school"
>>        -Albert Einstein
>>
>>
>>_______________________________________________
>>Mono-devel-list mailing list
>>Mono-devel-list at lists.ximian.com
>>http://lists.ximian.com/mailman/listinfo/mono-devel-list
>>
>>    
>
>
>--
>Martin Hinks
>http://www.m-s-d.net
>
>  


-- 
My home page: www.sigmax.cjb.net

"Education is what remains after one has forgotten everything he learned in school"
	-Albert Einstein





More information about the Mono-devel-list mailing list