C# Specified cast is not valid
-
You cannot cast an
Object
to anint
; it makes no sense. You really need to understand the basics of C#, before trying a project such as this. -
You can if it's a boxed int though, and some ints were assigned to tags so it looks like that was the intention
-
The
Tag
property returns anObject
, so it can (as you imply) be cast to anIntxx
type, but not to a simpleint
. -
Why do I get error when I click on button with image specified cast is not valid here: this.pokusaj[0] = (int)button.Tag;
switch (this.brojKlikova)
{
case 1:
{
//this.trenutneKontrole[0].Image = pictureBox.Image;
//this.pokusaj[0] = (int)pictureBox.Tag;this.trenutneKontrole\[0\].Image = button.Image; this.pokusaj\[0\] = (int)button.Tag; } break; case 2: { //this.trenutneKontrole\[1\].Image = pictureBox.Image; //this.pokusaj\[1\] = (int)pictureBox.Tag; this.trenutneKontrole\[1\].Image = button.Image; this.pokusaj\[1\] = (int)button.Tag; } break; case 3: { //this.trenutneKontrole\[2\].Image = pictureBox.Image; //this.pokusaj\[2\] = (int)pictureBox.Tag; this.trenutneKontrole\[2\].Image = button.Image; this.pokusaj\[2\] = (int)button.Tag; } break; case 4: { //this.trenutneKontrole\[3\].Image = pictureBox.Image; //this.pokusaj\[3\] = (int)pictureBox.Tag; this.trenutneKontrole\[3\].Image = button.Image; this.pokusaj\[3\] = (int)button.Tag; } break;
This is full code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;using System.Threading;
namespace Slagalica
{
public partial class Skocko : Form
{
private GroupBox groupBox1;private PictureBox pictureBox6; private PictureBox pictureBox5; private PictureBox pictureBox4; private PictureBox pictureBox3; private PictureBox pictureBox2; private PictureBox pictureBox1; private GroupBox groupBox8; private PictureBox pictureBox55; private PictureBox pictureBox56; private PictureBox pictureBox57; private PictureBox pictureBo
You cannot convert an object to int. Do some more research before asking for help
-
You cannot convert an object to int. Do some more research before asking for help
-
Why do I get error when I click on button with image specified cast is not valid here: this.pokusaj[0] = (int)button.Tag;
switch (this.brojKlikova)
{
case 1:
{
//this.trenutneKontrole[0].Image = pictureBox.Image;
//this.pokusaj[0] = (int)pictureBox.Tag;this.trenutneKontrole\[0\].Image = button.Image; this.pokusaj\[0\] = (int)button.Tag; } break; case 2: { //this.trenutneKontrole\[1\].Image = pictureBox.Image; //this.pokusaj\[1\] = (int)pictureBox.Tag; this.trenutneKontrole\[1\].Image = button.Image; this.pokusaj\[1\] = (int)button.Tag; } break; case 3: { //this.trenutneKontrole\[2\].Image = pictureBox.Image; //this.pokusaj\[2\] = (int)pictureBox.Tag; this.trenutneKontrole\[2\].Image = button.Image; this.pokusaj\[2\] = (int)button.Tag; } break; case 4: { //this.trenutneKontrole\[3\].Image = pictureBox.Image; //this.pokusaj\[3\] = (int)pictureBox.Tag; this.trenutneKontrole\[3\].Image = button.Image; this.pokusaj\[3\] = (int)button.Tag; } break;
This is full code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;using System.Threading;
namespace Slagalica
{
public partial class Skocko : Form
{
private GroupBox groupBox1;private PictureBox pictureBox6; private PictureBox pictureBox5; private PictureBox pictureBox4; private PictureBox pictureBox3; private PictureBox pictureBox2; private PictureBox pictureBox1; private GroupBox groupBox8; private PictureBox pictureBox55; private PictureBox pictureBox56; private PictureBox pictureBox57; private PictureBox pictureBo
There are a couple of things I notice about your code. The first thing I see is that you use a switch statement unnecessarily. As each array item you drop into is 1 away from the value in brojKlikova, you can use a simple subtraction to allocate the correct value. The second issue you have is that you have no idea what's in Tag. It may, or may not, be an int. If I were you, I would use something that is suited to test and convert a value into an int. Specifically, I would use int.TryParse to see if I could actually convert from whatever is in Tag to an int.
int tagValue;
this.trenutneKontrole[brojKlikova-1] = button.Image;
if (!int.TryParse(button.Tag, out tagValue))
{
pokusaj[brojKlikova-1] = tagValue;
}
else
{
System.Diagnostics.Debug.WriteLine("The value in the Tag for {0} is not an integer", brojKlikova);
}Run the application and watch the Output window to see which one of your Tag values triggers this. It's that simple.
This space for rent
-
You cannot convert an object to int. Do some more research before asking for help
Depends on what the
object
contains. If you have a method:private void MyMethod(object o)
{
int i = (int) o;
...
}And you use it thus:
int x = 666;
MyMethod(x);It will work fine. If you do this:
MyMethod(null);
Or
MyMethod(new Button());
Then it will fail.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Why do I get error when I click on button with image specified cast is not valid here: this.pokusaj[0] = (int)button.Tag;
switch (this.brojKlikova)
{
case 1:
{
//this.trenutneKontrole[0].Image = pictureBox.Image;
//this.pokusaj[0] = (int)pictureBox.Tag;this.trenutneKontrole\[0\].Image = button.Image; this.pokusaj\[0\] = (int)button.Tag; } break; case 2: { //this.trenutneKontrole\[1\].Image = pictureBox.Image; //this.pokusaj\[1\] = (int)pictureBox.Tag; this.trenutneKontrole\[1\].Image = button.Image; this.pokusaj\[1\] = (int)button.Tag; } break; case 3: { //this.trenutneKontrole\[2\].Image = pictureBox.Image; //this.pokusaj\[2\] = (int)pictureBox.Tag; this.trenutneKontrole\[2\].Image = button.Image; this.pokusaj\[2\] = (int)button.Tag; } break; case 4: { //this.trenutneKontrole\[3\].Image = pictureBox.Image; //this.pokusaj\[3\] = (int)pictureBox.Tag; this.trenutneKontrole\[3\].Image = button.Image; this.pokusaj\[3\] = (int)button.Tag; } break;
This is full code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;using System.Threading;
namespace Slagalica
{
public partial class Skocko : Form
{
private GroupBox groupBox1;private PictureBox pictureBox6; private PictureBox pictureBox5; private PictureBox pictureBox4; private PictureBox pictureBox3; private PictureBox pictureBox2; private PictureBox pictureBox1; private GroupBox groupBox8; private PictureBox pictureBox55; private PictureBox pictureBox56; private PictureBox pictureBox57; private PictureBox pictureBo