|
I have a program for work that uses a formula to calculate the refurb on a unit (parts replaced on cableboxes that were damaged) divided by total units(cableboxes that went through refurb, but did not have any parts replaced). I looked up casting online, and the format for it is intvaluetoconvert = Convert.toint32; I'm doing that, but still get the error "Cannot implicitly convert type double to int. An explicit conversion exists (are you missing a cast?)" What am I doing wrong? Here's some of my code to look at:
private int GetRefurbRate()
{
string sql = "";
double Refurb_Rate;
int totalRefurb = 0;
int totalUnits = 0;
string error_msg = "";
sql = "SELECT COUNT(rp.repair_ord) " +
"FROM " + schema + ".repair_part rp " +
"WHERE rp.repair_ord = '" + repair_ord + "' ";
while (true)
{
if (!myDb.RunSql(sql, true))
{
error_msg = "DBError for getting Refurb Rate";
break;
}
if (myDb.dbRdr.HasRows)
{
if (myDb.dbRdr.Read())
{
try
{
Refurb_Rate = Convert.ToInt32( totalRefurb / totalUnits * 100);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
break;
}
myDb.dbRdr.Close();
if (error_msg != String.Empty)
{
MessageBox.Show(error_msg, "Get Refurb Rate",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
return Refurb_Rate;
}
This is not the age of reason, this is the age of flummery, and the day of the devious approach. Reason’s gone into the backrooms where it works to devise means by which people can be induced to emote in the desired direction.
|
|
|
|
|
Congrats on the PRE tags, however the percentage formula isn't quite OK... see my other msg.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Justiin1265 wrote: Refurb_Rate = Convert.ToInt32( totalRefurb / totalUnits * 100); //This is where I try to perform the cast.
this does not make sense, as all variables are declared integers, therefore the quotient will be integer too, and the Convert method does nothing for you.
Furthermore, the result will not be what you expect:
for totalRefurb=2, totalUnits=10, the formula looks like 2 / 10 * 100 , unfortunately in an integer world 2 / 10 is zero.
What you need to do is one of the following:
- multiply first, i.e. 100 * totalRefurb / totalUnits
- use real numbers (i.e. data types such as float or double ), this will avoid overflow (when your integers near the max value), and perform rounding.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
modified on Monday, April 11, 2011 3:25 PM
|
|
|
|
|
I deleted my crap responses. Yours is more... what is the word? Oh yes, "correct".
This is not the age of reason, this is the age of flummery, and the day of the devious approach. Reason’s gone into the backrooms where it works to devise means by which people can be induced to emote in the desired direction.
|
|
|
|
|
"More correct"? A purist might object, some characteristics really are binary.
I'm not saying it is correct now (I still don't get what the OP is after), I do think one more problem has been addressed though.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Hey now, neither float nor double can represent all reals. They can only represent (some) rationals (and -0, NaN, +/-Infinity).
|
|
|
|
|
David1987 wrote: neither float nor double can represent all reals
I agree (and I'll add nothing can represent just any real number's value); I also didn't say such thing.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
No.. but you said "real numbers" and "float" in the same sentence, it might give people the wrong idea. I've known plenty of people who were told by a teacher that doubles are "real numbers" (which is almost true if it's put like that - except for NaN and infinities and -0) and they were all confused and surprised when their calculations gave the "wrong" results because they expected all reals to be representable by doubles.
|
|
|
|
|
Yes, it would have been better to put float and real in code-style. I'll fix that.
I use "real numbers" for non-integer numbers (or better yet, numbers not restricted to integer values), and not for "exact numbers". Most programmers do it that way; it may originate in the old days of Fortran, where they had type REAL (there wasn't a FLOAT time then).
And calculus (as well as programming) courses should teach people about number representations, errors, accumulation and propagation of errors, etc. Most programmers understand the square root of two isn't exact inside a computer, yet many have a hard time understanding why one third times three isn't always one.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Give this a try this should fix your problem;
return (int)Refurb_Rate;
|
|
|
|
|
Hi,
I want to cast the string output of the selected value from dropdown to an enum.
What is the best way to do it?
Thanks,
|
|
|
|
|
Enum.Parse(typeof(enumName), stringNameOfEnumValue);
or (.NET 4.0 and above only)
Enum.TryParse<enumName>(stringNameOfEnumValue, out placeToPutValue);
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Manfred R. Bihy: "Looks as if OP is learning resistant."
|
|
|
|
|
I tried,
Enum.Parse(((System.Type)n1.n2.n3.enum1), stringval,true);
but gives error that enum1 is a type not valid in current context
where n1.n2.n3 is the namespace where public enum enum1 is declared.
|
|
|
|
|
Then try what was actually suggested instead
I may or may not be responsible for my own actions
|
|
|
|
|
As musefan said, try what I gave you, not an interpretation of it:
For example:
public enum DriveType : int
{
Unknown = 0,
NoRoot = 1,
Removable = 2,
Localdisk = 3,
Network = 4,
CD = 5,
RAMDrive = 6
}
...
DriveType dt = (DriveType) Enum.Parse(typeof(DriveType), "RAMDrive");
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Manfred R. Bihy: "Looks as if OP is learning resistant."
|
|
|
|
|
Good answer, but just thought I'd mention that you then have to cast the result back:
MyEnum value = (MyEnum)Enum.Parse(typeof(MyEnum), str);
Also, it will throw an exception (FormatException, I think?) if the value is invalid so if this is being run from user input where that is a possibility, it should be caught.
|
|
|
|
|
Those are two of the reasons I prefer the TryParse version. Unfortunately, it is only available in .NET 4.0 and above.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Manfred R. Bihy: "Looks as if OP is learning resistant."
|
|
|
|
|
hI,
This worked for me:
n1.n2.n3.enum = (n1.n2.n3.enum)Enum.Parse(typeof(n4.n5.n6.enum),stringval)
where n1..6 are name spaces where respective enums are defined.
Thanks for evryone's help.
|
|
|
|
|
You definitely shouldn't do that! If you have any differences between the two enums in the different namespaces, you will get bad results. The only reason it doesn't complain is that Enum.Parse returns an object, which can be cast to either type.
This is another reason why I prefer the TryParse method: It is strongly typed!
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Manfred R. Bihy: "Looks as if OP is learning resistant."
|
|
|
|
|
It's actually simple enough to create one and put it in a utility class (or even an extension method, if you are in 3.5).
T TryParseEnum<T>(string s, T defaultValue){
try { return (T)Enum.Parse(typeof(T), s); }
catch(Exception) { return defaultValue; }
}
|
|
|
|
|
If the item list of the combobox is fixed and contains valid enum values only, then it is ok to do a direct cast of the SelectedItem.
enum Fruit { Quince, Gooseberry, Damson }
public void InitialiseControls() {
FruitComboBox.DataSource = Enum.GetValues(typeof(Fruit));
}
private void EatFruitBtn_Click(object sender, EventArgs e) {
Fruit yummy = (Fruit)FruitComboBox.SelectedItem;
}
Alan.
|
|
|
|
|
Hello experts,
I would like to ask how will I sync the data of 2 C# projects I created. I created a windows form application which use sql server 2005 express as its database and I also create an asp.net website that uses sql server 2005 express(The asp website and its database is uploaded to a hosting site).
Any comments or suggestions are kindly appreciated.
Thanks,
DAN
|
|
|
|
|
You best bet would be to create a WCF service that resides on your websites server. Then both Windows application and Website can access the same database, or the windows app could use it's own database, and only update the main database on the Webserver occasionally.
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
|
|
|
|
|
Hello,
Thanks for the immediate reply, I would like to ask again. If I create a wcf service in my asp.net website do I still need my windows form application database?
Thanks,
DAN
|
|
|
|
|
That would depend on your requirements. Personally, I like to use a separate database for the Windows application, which would be a local data cache, which can periodically update the main database. This way, if there are any connectivity problems with the Windows application, the users can still carry on.
Hope this helps.
...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....
|
|
|
|
|