System.TypedReference.InternalToObject Error

M

Mark S.

Hello Group,

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

Generates this error.
mscorlib
at System.TypedReference.InternalToObject(Void* value)
at System.Array.GetValue(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 ConsoleForTesting
{
[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)myArray.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.TypedReference.InternalToObject(Void* value)at
System.Array.GetValue(Int32 index)
}

Console.WriteLine("Press any key to exit.");
Console.ReadLine();
}
}
}
 
M

Mark S.

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
 
G

G.Doten

Mark said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top