Domanda

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. i cannot find out why debugger returns the following error: System.InvalidCastException. Specified cast is not valid. num and tag declared as integer value. what is wrong? In this line int? tag = (int?) this.pnlAnswers.Controls[num.Value].Tag; - debugger points to .Tag at the end of line as error.

    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.

È stato utile?

Soluzione

You need to cast the object referenced by Tag property to it's actual type which is byte. Then you can do further conversion against the byte object :

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

Altri suggerimenti

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". 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.

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;
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top