|
5'ed, I think that's correct but without looking it up I can't be sure. That makes the most sense.
|
|
|
|
|
The meaning of + operator depends on the types added together.
"A char can be implicitly converted to ushort, int, uint, long, ulong, float, double, or decimal. However, there are no implicit conversions from other types to the char type"
For chars the + is adding the integers together that are used for representing a single char. So in your first line the result is an int.
The second line contains automatically assignment so the result type cannot change (since you don't have an intermediate result) thus the operation results to a char. In the line having an error you must do casting explicitely to the intermediate result.
|
|
|
|
|
|
|
A colleague of mine is learning C#, as am I. I am ahead of him, so he asked me to help him figure out why his tiny test program isn't working. For the life of me I can't figure it out. Here's the code:
private void button1_Click(object sender, EventArgs e)
{
int tf = int.Parse(textBox1.Text);
int tc = 5 / 9 * (tf - 32);
label1.Text = tc.ToString();
}
This is supposed to convert the temperature in Fahrenheit (tf) to the temperature in celcius (tc). As far as I can tell it should work, but the value tc always comes out as 0. What am I overlooking?
|
|
|
|
|
Lodeclaw wrote: int tc = 5 / 9 * (tf - 32);
Integer devision, a common mistake for beginners, it happens to all of us.
5/9 is .5555 which is 0. Convert to doubles and try it then, should be fine after that.
|
|
|
|
|
|
No it isn't... I used same variable names...
double tf = double.Parse(textBox1.Text);
double tc = (5.0 / 9.0) * (tf - 32);
label1.Text = tc.ToString();
|
|
|
|
|
Oh, I see the problem. We're not entering the values in the calculation as doubles. (5.0 rather than 5) Thanks, Eliott.
|
|
|
|
|
|
Remember to change your int.Parse and datatype to double and double.Parse otherwise entering 10.0 in that textbox and parsing it to an int might either through an exception or truncate the double value (I don't know which one) in either case, duplicating the original error!!
|
|
|
|
|
double tf = double.Parse(textBox1.Text);
double tc = 5 / 9 * (tf - 32);
label1.Text = tc.ToString();
If that doesn't work, something's really wrong here.
Kristian Sixhoej
"You can't undo the past... but you can certainly not repeat it." - Bruce Willis
|
|
|
|
|
Kristian Sixhoej wrote: If that doesn't work, something's really wrong here.
Untrue, I think it will still do integer devision on 5 / 9, you have to explicitly put .0 on either to do double devision. Weird, I know, I just noticed that behavior on a test.
|
|
|
|
|
EliottA wrote: you have to explicitly put .0 on either to do double devision.
Didn't knew that.
EliottA wrote: Weird, I know, I just noticed that behavior on a test.
IIRC I have never had that problem when doing double divisions.
But I probably don't recall correct.
Kristian Sixhoej
"You can't undo the past... but you can certainly not repeat it." - Bruce Willis
|
|
|
|
|
I didn't know it either, and I never ran into a problem like that before, I never had to implicitly add a .0 unless it was in java.. even then I vaguely recall something about automatic upgrading datatype or something, I don't know. I completely forget, just remember seeing integer devision smack me in the face a few times in my first programming course.
|
|
|
|
|
EliottA wrote: I never ran into a problem like that before
That's probably because it's not that usual to divide two literal values. Usually one of the operands is a double variable, then the compiler will cast the other operand to double also.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Being a bit anal about this, I'd do this:
double tf = double.Parse(textBox1.Text);
double tc = 5d / 9d * (tf - 32d);
label1.Text = tc.ToString();
Regards,
Rob Philpott.
|
|
|
|
|
Oh, that's fascinating! Placing a 'd' after a whole number will really make it a double?
|
|
|
|
|
Yep, although I never really have ever used it. I'd rather put 5.0 or something of the sort.
|
|
|
|
|
will 5.0 def making it a double as opposed to float?
Im not trying to correct, seroius question
My opinion is... If someone has already posted an answer, dont post the SAME answer
|
|
|
|
|
From my recollection yes it will, the default is always double unless explicitly indicated otherwise.
|
|
|
|
|
My opinion is... If someone has already posted an answer, dont post the SAME answer
|
|
|
|
|
Hi All,
I have an application which calls a number of different child form for different processes.
These forms are mainly displayed with the ShowDialog() method as they require the user to enter data before doing anything else.
Now the problem is sometimes (not very often) when the child 'dialog' form is finished with, either by setting DialogResult to 'OK' or to 'Cancel' and then calling this.Hide(), the parent form will not be visible. The form is still present in the taskbar, so a quick minimise then maximise will show it once again, but clearly this is not acceptable.
If anyone has experienced this problem or can offer an explaination, I would be greatful.
But please remember its not a common occurances, and if you offer a simple solution such as try setting the parent form to visible everytime as safety measure, I cannot test so easily and would much prefer an explaination if possible.
Of course saying that, if someone can say they have had experience with the problem and have solved with no more issues without knowing a reason behind, I would be happy with that as a solution - atleast for now
Thanks
My opinion is... If someone has already posted an answer, dont post the SAME answer
|
|
|
|
|
You're calling this.Hide() to hide the dialog - not the parent form - if I understood correctly.
You should use ChildForm.Hide() since the this keyword
points back to the parent form.
Kristian Sixhoej
"You can't undo the past... but you can certainly not repeat it." - Bruce Willis
|
|
|
|
|
this.Hide() is inside the child form itself.
Basically there are 2 buttons (Confirm and Cancel) within the child form
Confirm has event code such as:
this.DialogResult = DialogResult.OK;
if(CheckValid())
this.Hide();
Cancel has event code such as:
this.DialogResult = DialogResult.Cancel;
this.Hide();
My opinion is... If someone has already posted an answer, dont post the SAME answer
|
|
|
|