Matthew H
Matthew H

Reputation: 39

C# int.TryParse Maybe?

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.

Upvotes: 1

Views: 251

Answers (1)

Matthew
Matthew

Reputation: 25773

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.

Upvotes: 7

Related Questions