Why does not 'int' work? (Type casting)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: Why does not 'int' work? (Type casting)

Hybrid View

  1. #1
    Join Date
    Apr 2010
    Posts
    8

    Red face Why does not 'int' work? (Type casting)

    Hi!

    I'm new here, so forgive me if this is in the wrong place...

    heres the 'problem' (not really a 'problem', its just something i dont understand even though i got it working)

    Basically, I have a field in the database and its a tinyint, i have a simple value there as a '0' or '1' (without the quotes)

    (Note: I am using winforms via Visual Studio 2008)

    then in the on LOAD code I write:

    int for_chkbox=0;
    if (this.Settings_ShowSplash_checkBox.Checked)
    { for_chkbox = 1; }
    else { for_chkbox = 0; }

    but it then throws up this error:
    cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?)


    i then changed the above code to:
    byte for_chkbox=0;
    if (this.Settings_ShowSplash_checkBox.Checked)
    { for_chkbox = 1; }
    else { for_chkbox = 0; }


    and everything works...

    Can someone explain to me... why?

    Thanks in advance!

  2. #2
    Join Date
    Aug 2008
    Posts
    902

    Re: Why does not 'int' work? (Type casting)

    You have to give us more. Somewhere, for_chkbox (kind of a confusing variable name) is being used in a statement that expects a byte type.

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Why does not 'int' work? (Type casting)

    Quote Originally Posted by Chris_F View Post
    You have to give us more. Somewhere, for_chkbox (kind of a confusing variable name) is being used in a statement that expects a byte type.
    If you look at the code it is declared as an int, but somehow I am not buying it...
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  4. #4
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Why does not 'int' work? (Type casting)

    Quote Originally Posted by BigEd781 View Post
    If you look at the code it is declared as an int, but somehow I am not buying it...
    Uhm, yeah ... And that's why the compiler comes up with an error if it's used somewhere that expects a byte
    It's not a bug, it's a feature!

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Why does not 'int' work? (Type casting)

    Quote Originally Posted by foamy View Post
    Uhm, yeah ... And that's why the compiler comes up with an error if it's used somewhere that expects a byte
    Yes, but that code is not up there, so it is anyone's guess.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

  6. #6
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Why does not 'int' work? (Type casting)

    Quote Originally Posted by BigEd781 View Post
    Yes, but that code is not up there, so it is anyone's guess.
    Yep, without more of the code it's impossible to tell.
    It's not a bug, it's a feature!

  7. #7
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    422

    Re: Why does not 'int' work? (Type casting)

    hmm...

  8. #8
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Why does not 'int' work? (Type casting)

    It should works fine. You have find a curiosity or a bug in parser. 0 or 1 are integer literals. I'm very interested in solution or reason.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  9. #9
    Join Date
    Apr 2010
    Posts
    2

    Re: Why does not 'int' work? (Type casting)

    Past some code, around the code you pasted above. Also try searching for that variable in the code files and see if it have any scope level problem.

  10. #10
    Join Date
    Apr 2010
    Posts
    8

    Re: Why does not 'int' work? (Type casting)

    Ok, hold on, will post some more details

  11. #11
    Join Date
    Apr 2010
    Posts
    8

    Cool Re: Why does not 'int' work? (Type casting)

    Here you go:
    First my db table:

    http://img28.imageshack.us/i/62745765.png/

    more db:
    http://img517.imageshack.us/i/31661690.png/

    My form, and checkbox selected, i took a large screen capture so you can see my connections etc at the bottom and the checkbox properties on the side as well:
    http://img340.imageshack.us/i/55444160.png/

    and finally, the code:
    http://img133.imageshack.us/i/76597391.png/

    If I left out any particular piece (dont think i have) just reply and tell me which part and I'll up it.

    Cheers!
    R

  12. #12
    Join Date
    Apr 2010
    Posts
    8

    Re: Why does not 'int' work? (Type casting)

    oops,
    looks like the last pic didnt come out too well, heres that snapshot again:
    http://img682.imageshack.us/img682/166/37698623.png

    If any of the snapshots are hard to read or too small, just click it for the original large size as the image host resizes it sometimes to show their ads.

    I couldnt find the edit button so am posting below... i'm guessing the edit button has been disabled?

  13. #13
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Why does not 'int' work? (Type casting)

    It must be something weird. I've just tried both of your variants and both compile well, regardless for_chkbox is declared as byte or int.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  14. #14
    Join Date
    Apr 2010
    Posts
    8

    Re: Why does not 'int' work? (Type casting)

    That IS strange, because this is what i get when i change it to int
    http://img52.imageshack.us/img52/3331/90341105.png

    Shared with ImageShack http://stream.imageshack.us/redir.ph...1/90341105.png

  15. #15
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Why does not 'int' work? (Type casting)

    That's probably because SplashDisabled is a byte, not an int.
    It's not a bug, it's a feature!

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center