Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# Specified cast is not valid

C# Specified cast is not valid

Scheduled Pinned Locked Moved C#
csharplinqgraphicshelp
16 Posts 5 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    You cannot cast an Object to an int; it makes no sense. You really need to understand the basics of C#, before trying a project such as this.

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #7

    You can if it's a boxed int though, and some ints were assigned to tags so it looks like that was the intention

    L 1 Reply Last reply
    0
    • L Lost User

      You can if it's a boxed int though, and some ints were assigned to tags so it looks like that was the intention

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #8

      The Tag property returns an Object, so it can (as you imply) be cast to an Intxx type, but not to a simple int.

      L 1 Reply Last reply
      0
      • L Lost User

        The Tag property returns an Object, so it can (as you imply) be cast to an Intxx type, but not to a simple int.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #9

        int is the same type as Int32. This works[^]: object obj = 1; int x = (int)obj;

        L 1 Reply Last reply
        0
        • P Pavlex4

          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
          
          N Offline
          N Offline
          Nelson Costa Inacio
          wrote on last edited by
          #10

          You cannot convert an object to int. Do some more research before asking for help

          L OriginalGriffO 2 Replies Last reply
          0
          • N Nelson Costa Inacio

            You cannot convert an object to int. Do some more research before asking for help

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #11

            You can though, so right back at you.

            1 Reply Last reply
            0
            • L Lost User

              int is the same type as Int32. This works[^]: object obj = 1; int x = (int)obj;

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #12

              My bad; I should always run my own tests rather than accepting what OPs post. :doh:

              1 Reply Last reply
              0
              • P Pavlex4

                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
                
                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #13

                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

                1 Reply Last reply
                0
                • N Nelson Costa Inacio

                  You cannot convert an object to int. Do some more research before asking for help

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #14

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

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  1 Reply Last reply
                  0
                  • P Pavlex4

                    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
                    
                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #15

                    It's like you're running your "code" through an obfuscator before posting it here.

                    L 1 Reply Last reply
                    0
                    • L Lost User

                      It's like you're running your "code" through an obfuscator before posting it here.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #16

                      Take a look below at some of OP's other posts.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups