简体   繁体   English

InvalidCastException:指定的强制转换无效

[英]InvalidCastException: Specified cast is not valid

This part of code is responsible to capture user input from keyboard and use it. 这部分代码负责从键盘捕获用户输入并使用它。 When i press some button (ex. C) on keyboard variable TAG receives this as object (byte) value 3 . 当我按下键盘上的某个按钮(例如C)时,变量TAG将其作为对象(字节)值3接收 i cannot find out why debugger returns the following error: System.InvalidCastException. 我无法找出调试器返回以下错误的原因: System.InvalidCastException。 Specified cast is not valid . 指定演员表无效 num and tag declared as integer value. numtag声明为整数值。 what is wrong? 怎么了? In this line int? 在这行int? tag = (int?) this.pnlAnswers.Controls[num.Value].Tag; tag =(int?)this.pnlAnswers.Controls [num.Value] .Tag; - debugger points to .Tag at the end of line as error. - 调试器指向。行末尾的.Tag作为错误。

    private void Question_KeyDown(object sender, KeyEventArgs e)
    {
        int? num = null;
        this.qta.get_answer_number_by_key(new int?(e.KeyValue), ref num);
        if (!num.HasValue)
        {
            this.SwitchQuestion(e.KeyValue);
        }
        else
        {
            num -= 1;
            bool? nullable2 = false;
            bool? end = false;
            if (this.pnlAnswers.Controls.Count >= (num + 1))
            {
                Valid valid;
                int? tag = (int?) this.pnlAnswers.Controls[num.Value].Tag;
                this.qta.test_answer(this.q, tag, ref nullable2, ref end, ref this.pass);
                this.e = end.Value;
                if (nullable2.Value)
                {
                    valid = new Valid(MessageType.Valid);
                }
                else
                {
                    valid = new Valid(MessageType.Invalid);
                }
                valid.ShowDialog();
                base.Close();
            }
        }
    }

i`ve tried to change 我试图改变

int? tag = (int?) this.pnlAnswers.Controls[num.Value].Tag;

to

byte? tag = (byte?) this.pnlAnswers.Controls[num.Value].Tag;

and error gone, however i have issues with post-processing of receiving this values. 和错误消失,但我有接收此值的后处理问题。

You need to cast the object referenced by Tag property to it's actual type which is byte . 您需要将Tag属性引用的对象Tag为它的实际类型,即byte Then you can do further conversion against the byte object : 然后你可以对byte对象做进一步的转换:

byte tagByte = (byte)this.pnlAnswers.Controls[num.Value].Tag);
int? tag = (int?) tagByte;
//or in short :
//int? tag = (byte)this.pnlAnswers.Controls[num.Value].Tag;

Simple test I did to confirm this behavior : 我做的简单测试来确认这种行为:

byte initialValue = 3;
object TAG = initialValue;
int? tagSuccess = (int?)((byte)TAG); //successfully convert TAG to type int?
int? tagFails = (int?)TAG; //throw InvalidCastException

The accepted answer does not always work. 接受的答案并不总是有效。

When a Tag is set to 1 in the property editor of your Visual Studio forms designer, it will be assigned string type, that is "1". 在Visual Studio窗体设计器的属性编辑器中将标记设置为1时,将为其分配字符串类型,即“1”。 you can see that in the debugger when the exception occurs. 您可以在发生异常时在调试器中看到它。 There are quotes. 有报价。

A string cannot be cast to any numeric type directly. 字符串不能直接转换为任何数字类型。 In this case, (byte) will yield the same exception. 在这种情况下,(byte)将产生相同的异常。

A general but dirty solution for tags of type string OR integer is this.. 对于字符串或整数类型的标签,一般但很脏的解决方案是这个..

   private int TagValue(object cTag)
    {
        int iTag = 0;
        try { iTag = (int)cTag; } catch { iTag = int.Parse((string)cTag); }
        return iTag;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 InvalidCastException:指定的强制转换在 Unity 中无效 - InvalidCastException: Specified cast is not valid in Unity InvalidCastException: 指定的强制转换无效 || 统一 - InvalidCastException: Specified cast is not valid || Unity 指定的转换无效-System.InvalidCastException - Specified cast is not valid - System.InvalidCastException System.InvalidCastException:指定的强制转换无效 - System.InvalidCastException: Specified cast is not valid InvalidCastException:指定的强制转换在LINQ查询中无效 - InvalidCastException: Specified cast is not valid in LINQ query “ System.InvalidCastException:指定的转换无效”-DateTime - “System.InvalidCastException: Specified cast is not valid” - DateTime System.InvalidCastException:指定的强制转换在.ExecuteScalar上无效 - System.InvalidCastException: Specified cast is not valid at .ExecuteScalar Unity Mirror Networking InvalidCastException:指定的演员表无效 - Unity Mirror Networking InvalidCastException: Specified cast is not valid System.InvalidCastException:'指定的强制转换无效。 - System.InvalidCastException: 'Specified cast is not valid.' C#InvalidCastException:指定的强制转换对JSON.Net无效 - C# InvalidCastException: Specified cast is not valid for JSON.Net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM