I'm getting a cast error in design time and not sure how to fix this. I've attached a screenshot.
#1 images of code are the worst, this forum has [highlight=c#][/highlight] tags for a reason.
#2 you have x defined as an integer (by definition can't have decimal values) and you're multiplying it by 0.3 (a decimal value) so really all you need is to use a float or double (two types that can hold decimal values) and move the "* 0.3" part up to the end of the img.Width and it'll do the math storing it in the "x" variable in one swoop.
Currently using VS 2015 Enterprise on Win10 Enterprise x64.
That code doesn't really make sense. You have an int value and you multiply it by a double and then assign the result back to the same int variable. What is the likelihood that any integer multiplied by 0.3 will yield another integer? The result of 'x * 0.3' is going to be a double, so you're going to have to round that in some way (Math.Round, Math.Ceing, Math.Floor, Math.Truncate) and then convert the result to an int in order to assign back to an int variable, e.g.
Personally, I prefer the result of [highlight=csharp][/highlight] to [highlight=c#][/highlight], e.g.
Learn something new everyday, good to know there's a difference. Actually I didn't know there was a 'csharp' option on this forum for the highlight tag, all the other forums I use will use the visual studio code coloring when using 'c#' in a code, xcode, or highlight tag. I wonder why this forum doesn't.
Currently using VS 2015 Enterprise on Win10 Enterprise x64.
That code doesn't really make sense. You have an int value and you multiply it by a double and then assign the result back to the same int variable. What is the likelihood that any integer multiplied by 0.3 will yield another integer? The result of 'x * 0.3' is going to be a double, so you're going to have to round that in some way (Math.Round, Math.Ceing, Math.Floor, Math.Truncate) and then convert the result to an int in order to assign back to an int variable, e.g.
csharp Code:
x = Convert.ToInt32(Math.Round(x * 0.3));
It's worth noting that that code would produce the same result without the Math.Round call.
Math.Round:
Return Value
Type: System.Double
The integer nearest a. If the fractional component of a is halfway between two integers, one of which is even and the other odd, then the even number is returned. Note that the method returns a Double type rather than an integral type.
Convert.ToInt32:
Return Value
Type: System.Int32
value, rounded to the nearest 32-bit signed integer. If value is halfway between two whole numbers, the even number is returned; that is, 4.5 is converted to 4, and 5.5 is converted to 6.
It's only if you wanted to round using different rules that you'd need to add an extra call.
As I said, I'm learning C#...so thanks for the tip!
There's no difference between C# and VB here, if you had Option Strict On in VB. This code:
vb.net Code:
Dim x AsInteger = img.Width
x = x * 0.3
would give you an equivalent error message. An Int32 multiplied by a Double will produce a Double in both languages and you can't assign a Double value to an Int32 variable without a narrowing conversion. With Option Strict Off, VB would implicitly round and convert the Double, while with Option Strict On you would require the same explicit conversion and optional explicit rounding as in C#.
→ The Comprehensive Guide to Cloud Computing
A complete overview of Cloud Computing focused on what you need to know, from selecting a platform to choosing a cloud vendor.