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!
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.
Re: Why does not 'int' work? (Type casting)
Quote:
Originally Posted by
Chris_F
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...
Re: Why does not 'int' work? (Type casting)
Re: Why does not 'int' work? (Type casting)
Quote:
Originally Posted by
BigEd781
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 ;)
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.
Re: Why does not 'int' work? (Type casting)
Quote:
Originally Posted by
foamy
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.
Re: Why does not 'int' work? (Type casting)
Quote:
Originally Posted by
BigEd781
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.
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.
Re: Why does not 'int' work? (Type casting)
Ok, hold on, will post some more details ;)
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
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?
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.
Re: Why does not 'int' work? (Type casting)
Re: Why does not 'int' work? (Type casting)
That's probably because SplashDisabled is a byte, not an int.
Re: Why does not 'int' work? (Type casting)
Quote:
Originally Posted by
foamy
That's probably because SplashDisabled is a byte, not an int.
Nope, already established that SplashDisabled is a tinyint:
http://img517.imageshack.us/img517/724/31661690.png
Edit:
Looks like my edit button has been enabled! Yay!
Re: Why does not 'int' work? (Type casting)
Quote:
Originally Posted by
MusicGuy
TBH, I didn't look at those images. AFAIK tinyint should be used as a byte in C#.
Re: Why does not 'int' work? (Type casting)
Well, I'm new to C#, I'm more used to PHP/MySql and usually when using php/mysql if i have a "on/off" value and am using something small as a 0/1 value i use a tiny int.
It usually gives me more flexibility than declaring it as a boolean as i can extend it later to take more than just 2 values but a LOT less than an int.
Are you suggesting I dont use tinyint anymore in C#? I'm not being sarcastic or anything, like i said i am new here (just started C# a few days back) and would genuinely like to know the answer from you guys who use this on a daily basis.
Re: Why does not 'int' work? (Type casting)
Quote:
Originally Posted by
MusicGuy
Well, I'm new to C#, I'm more used to PHP/MySql and usually when using php/mysql if i have a "on/off" value and am using something small as a 0/1 value i use a tiny int.
It usually gives me more flexibility than declaring it as a boolean as i can extend it later to take more than just 2 values but a LOT less than an int.
Are you suggesting I dont use tinyint anymore in C#? I'm not being sarcastic or anything, like i said i am new here (just started C# a few days back) and would genuinely like to know the answer from you guys who use this on a daily basis.
For something named "SplashDisabled", do you really think you will need "more flexibility" in the future? Sounds like a true or false value to me, anything more would just be confusing. Also, if you need more values you should use an enum, not a byte.
And yes, use an int. You are not saving anything in a practical sense by using a byte.
Re: Why does not 'int' work? (Type casting)
Quote:
Originally Posted by
BigEd781
For something named "SplashDisabled", do you really think you will need "more flexibility" in the future? Sounds like a true or false value to me, anything more would just be confusing. Also, if you need more values you should use an enum, not a byte.
And yes, use an int. You are not saving anything in a practical sense by using a byte.
Hey,
Thanks for replying!
I agree, in this case "more flexibility" will probably never be needed, but its a habit I built up after working for some morons who kept changing their minds about what the data should do, and what values the DB's fields should hold.
As an example I can actually name you times I actually had to change the field names to suit the designers! (We were using SMARTY so we could all play along nicely.. but still it wouldnt do)
If I was still working for that company, SplashDisabled could possibly have been changed to SplashStatus (0-off, 1-on,2-default,3-on (but run google ads), 4-on but run house ads... etc)
Thats just it, i have never used a byte anywhere, only int and tinyint, but it still wont let me get the present value from the db and put it into an int... it insists on byte for some reason that is beyond me.. so i asked here. Looks like a bug on my machine for some reason as others here cant reproduce the same result even after looking at my code. :(
Re: Why does not 'int' work? (Type casting)
Since you're new to C# I'll explain once more. TinyInt is a byte in C#. A byte is not the same as an int. Therefore this will not compile:
Code:
namespace intbyte
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int for_chkbox = 0;
byte SplashDisabled = 1;
SplashDisabled = for_chkbox;
}
}
}
However, this will:
Code:
namespace intbyte
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int for_chkbox = 0;
byte SplashDisabled = 1;
SplashDisabled = (byte)for_chkbox;
}
}
}
As the error message is telling you, you need to cast the int as a byte.