1

So this is an error I've been trying to figure out but can not seem to fix. . This is in one function.

File.Copy(item.FileName, mcAD [VersionText.Tag], true);

private void Version_2_0_Click(object sender, EventArgs e)
{
    string Version_2_0_Selected = VersionText.Text = "Version 2.0";
    VersionText.Tag = 2;
}

But VersionText.Tag in the first part always gives me this error.

enter image description here

I heard something with int.TryParse, but I can not figure out how to implement it into my code.

I hope I explained it enough.

3
  • 3
    What is this supposed to do: string Version_2_0_Selected = VersionText.Text = "Version 2.0"; Commented Jul 31, 2013 at 4:37
  • 1
    @DimitarDimitrov That gives one value to both strings Commented Jul 31, 2013 at 4:37
  • 1
    @LeeTaylor Woah ! Honestly, I didn't even know that's valid syntax. Awesome. Commented Jul 31, 2013 at 4:39

1 Answer 1

7

My assumption of the problem is on the line

File.Copy(item.FileName, mcAD [VersionText.Tag], true);

specifically mcAD [VersionText.Tag].

.Tag returns type object, but the array indexer expects int.

If you cast it, it should hopefully get rid of the compile error at least.

File.Copy(item.FileName, mcAD [(int)VersionText.Tag], true);

If VersionText.Tag doesn't contain an integer, you'll get a runtime error, however.

Sign up to request clarification or add additional context in comments.

15 Comments

+1: With all the other poor answers, this is the only correct one.
agreed with @leppie )
That did it! I'll test now! :D @Matthew prntscr.com/1iirtr this is what happened
In order to avoid possible runtime error you can use int.TryParse method. Something like this: int result = -1; Int32.TryParse(VersionText.Tag, out result); if (result != -1) {//your code}
@MatthewH in that case, it's best to show us the format of VersionText in your question. I.e. how the variables are declared.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.