System.TypedReference.InternalToObject Error

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

    #1

    System.TypedReference.InternalToObject Error

    Hello Group,

    This line of c# dot net 2.0 code:
    MyStruct thisAd = (MyStruct)myArr ay.GetValue(i);

    Generates this error.
    mscorlib
    at System.TypedRef erence.Internal ToObject(Void* value)
    at System.Array.Ge tValue(Int32 index)

    I am unable to reproduce in my test environement. I would really appreciate
    your thoughts.

    The line of code in question is a big block of code, so I've distilled it
    down to it's essence. Unless necessary please refrain from asking "why you
    did something this way". I'm still learning how to code with best practices,
    so if there's a better way please describe and I'll be happy to learn it.


    using System;

    namespace ConsoleForTesti ng
    {
    [Serializable()]
    public struct MyStruct
    {
    public string str1;
    public string str2;
    public int int1;
    public int int2;
    public int int3;
    public string string3;
    public int int4;
    public int int5;
    public int int6;
    public int int7;
    public Array array1;
    public int int8;
    public int int9;
    }

    class test
    {
    static void Main(string[] args)
    {
    MyStruct[] myArray = new MyStruct[1];
    MyStruct myStruct = new MyStruct();
    myArray[0] = myStruct;

    // there are numerous loops like this one in the same method
    for (int i = 0; i < myArray.Length; i++)
    {
    MyStruct thisAd = (MyStruct)myArr ay.GetValue(i);
    // when I put in 1 it generates the expected "Index was
    outside the bounds of the array.",
    // but not the mscorlib at
    System.TypedRef erence.Internal ToObject(Void* value)at
    System.Array.Ge tValue(Int32 index)
    }

    Console.WriteLi ne("Press any key to exit.");
    Console.ReadLin e();
    }
    }
    }



  • Mark S.

    #2
    Re: System.TypedRef erence.Internal ToObject Error

    Hi,

    I can answer my own question.

    I changed public struct to public class because it was containing more than
    the recommeded 16K worth of data.

    I rewrote the code to use generics. public class myClass<T>. This resulted
    in a 23% increase in performance in a very busy web app.

    I also found some unhandled errors, so I wrapped them in try statements and
    debugged them.

    As of this writing it's working great again.

    Hope this helps someone else.

    M


    Comment

    • G.Doten

      #3
      Re: System.TypedRef erence.Internal ToObject Error

      Mark S. wrote:
      I changed public struct to public class because it was containing more than
      the recommeded 16K worth of data.
      Did you mean to say 16 bytes worth of data?

      --
      -glenn-

      Comment

      Working...