问题描述:

Hi

New to C# Programming.

I've been trying to cast in that way:

m_GameBoard = new Board((short)i_array[1]); // (i_array is defined as object[] i_array)

The compiler don't say anything, but in run time I encounter the following crash:

System.InvalidCastException was unhandled

Message=Association specified is invalid.

Source=B11 Ex02

StackTrace:

ב- B11_Ex02.Game..ctor(Object[] i_array) ב- ..\B11 Ex02\Game.cs: line 32

ב- B11_Ex02.Program.Main() ב- \B11 Ex02\Program.cs:line 56

ב- System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)

ב- System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)

ב- Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

ב- System.Threading.ThreadHelper.ThreadStart_Context(Object state)

ב- System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)

ב- System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

ב- System.Threading.ThreadHelper.ThreadStart()

InnerException:

Would very appreciate your help

Thanks in advance

网友答案:

The error message is telling you that whatever is being returned by i_array[i] cannot actually be cast to a short. So, my advice would be to inspect i_array to see what it actually contains at index i when it fails.

EDIT: Per Ben Voigts insight in my comments:

This is unboxing, so even if the value stored at i_array[i] could be cast to short, the expression (short)i_array[i] only works if i_array[i] is exactly a short (I think the runtime makes an exception for unsigned short vs short, but neither widening nor narrowing conversions can take place).

I only post this here because the OP would not be able to see my deleted post. The problem is that you are likely initializing elements in the collection with int's, but you will need to explicitly add only shorts to the collection if you need to unbox them (DO you actually need to unbox them? Why not use a List<short> instead?)

网友答案:

This works for me:

Object Data_array (is filled with excel data):
object[,] Data_array = (object[,])excelRange.get_Value(XlRangeValueDataType.xlRangeValueDefault);

Then, convert to short:

short data = (short)Convert.ToInt32(Data_array[x, y]);
网友答案:

You're trying to cast something to short that can't be cast. As a general rule you want to use one of the Parse methods:

m_GameBoard = new Board(short.Parse(i_array[1].ToString()));

I suggest you refactor the code a little and use short.TryParse() so you can handle the case where the parse fails.

short i_short;

if (short.TryParse(i_array[1].ToString(), i_short))
{
   m_GameBoard = new Board(i_short);
}
else
{
   throw new ArgumentException("Some joker filled that array with something that wasn't an int16");
}
网友答案:

This exception tells you that your object can not be cast to short. If it's not a short (maybe a string or some other type) the type needs to define a cast operator to short or you will have to convert it yourself.

There is no compiler warning because at compile time there is no way to tell if object will be castable to type short.

相关阅读:
Top