Decimal - Double?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm new to C#, and I want to use a trackbar for the forms opacity
This is my code

decimal trans = trackBar1.Value / 5000
this.Opacity = trans

When I try to build it, I get this error

Cannot implicitly convert type 'decimal' to 'double

I tried making trans a double, but then the control doesn't work. This code worked fine for me in VB.NET. Any suggestions
 
Bill English said:
I'm new to C#, and I want to use a trackbar for the forms opacity.
This is my code:

decimal trans = trackBar1.Value / 5000;
this.Opacity = trans;

When I try to build it, I get this error.

Cannot implicitly convert type 'decimal' to 'double'

I tried making trans a double, but then the control doesn't work.

What do you mean by "doesn't work"? In what way doesn't it work?

You can always cast from decimal to double - in other words explicitly
converting rather than trying to implicitly convert.
This code worked fine for me in VB.NET. Any suggestions?

Did you have option strict on in VB.NET? If not, I suggest you have it
on in future, to give you more type safety. If so, I'm surprised it
worked. (Then again, I'm no VB.NET expert. Maybe it's happy to do
potentially dodgy conversions for you implicitly.)
 
How did you make it a double?

first thing I'd try is:

double trans = (double)trackBar1.Value / 5000.0;
this.Opacity = trans;

Math in C# (or maybe all c's) is kind weird....if you devide by an int you get
back an int.

Let me know if there are still problems.

I'm new to C#, and I want to use a trackbar for the forms opacity.
This is my code:

decimal trans = trackBar1.Value / 5000;
this.Opacity = trans;

When I try to build it, I get this error.

Cannot implicitly convert type 'decimal' to 'double'

I tried making trans a double, but then the control doesn't work. This code
worked fine for me in VB.NET. Any suggestions?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top