Integer - Double

XOJO 2021r1.1 / Windows 11
Using following code in the Paint event of a Canvas
g.DrawRoundRectangle(0.0, 0.0, Me.Width, Me.Height, 10.0, 10.0)
Gives a conversion warning when analyzing the project.
How can I prevent that warning ? (I do not mean to not check for the conversion warnings.)
I am looking for the correct code to prevent this warning.

i.e. Me.Width and Me.Height are Integer values.
And the drawing expect a Double.


Did you noticed the example does not use doubles ?

This is what the documentation say
Graphics.DrawRoundRectangle(x As Double, y As Double, Width As Double, Height As Double, arcWidth As Double, arcHeight As Double)

As Widht I use Me.Width but this is a Integer.
How can I make Me.Width a Double ?

Here’s the full paragraph from the online documentation:
DrawRoundRectangle(X As Double, Y As Double, Width As Double, Height As Double, ArcWidth As Double, ArcHeight As Double)

Draws the outline of a rounded rectangle in the current color. The current color is set with the DrawingColor property.

X and Y are the coordinates of the top-left corner. Width and Height specify the size of the round rectangle. ArcWidth and ArcHeight control the shape of the corners in the horizontal and vertical axes, respectively. They are the distance (in pixels) from the corner at which the arc begins. Setting them to zero results in a rectangle with sharp corners.

This example draws two squares with a 5-pixel red border. The left square sets the arcs to 10 and the right one to 25.

g.DrawingColor = &cff0000 g.PenSize = 5 g.DrawRoundRectangle(10, 10, 100, 100, 10, 10) g.DrawRoundRectangle(130, 10, 100, 100, 25, 25)

Either add 0.0 to the Integer value (verify this claim)=
theWidth = Me.Width + 0.0

Or Declare a Double set it to Me.Width

Var meWidth As Double

MeWidth = Me.Width

Something like:

Dim me_height as double = me.height
Dim me_width as double = me.width
g.drawroundrectangle(0.0, 0.0, me_width, me_height, 10.0, 10.0)


Or extend integer to be able to do Me.Width.ToDouble (if not available yet)

Edit: I took too long to write this and I see a couple of answers after I started.

I have two suggestions:

  1. Cast the integer to double using CType, like ... CType( Me.Width, Double ), ...; or
  2. Turn off that warning.

I recommend the second.

1 Like

Or extend integer to be able to do Me.Width.ToDouble (if not available yet)
Need to look for that later.

None of the other solutions are preventing the warning.
Converting from integer to Double causes a possible loss of precision, which can lead to unexpected results.

No worry it is only a warning.
Everything works fine.
Not checking the conversion items in the Analysis Warnings stops these warnings.
But I was wondering how this warning could be solved.

It’s a weird warning. I get its purpose. If you were to do Var Value As Integer = 2.5 that warning helps you catch the mistake.

But damn… avoiding the warning makes the code real annoying.

1 Like

C’mon… this should not throw a warning. It complains “Converting from Integer to Int32 causes a possible loss of precision.” This is how you get people to turn the warning off.

I believe what’s happening is the array accessor is defined as Int32 instead of Integer, thus the warning on 64-bit systems. But LastIndex is Integer, so :person_shrugging:.

This one might be the most egregious.

Want to guess the warning? “Converting from Integer to Double causes a possible loss of precision.”

They are the same type!!!

you should use g.width and g.height

1 Like
g.drawroundrectangle(0.0, 0.0, Ctype(me.width, Double), Ctype(me.height, Double), 10.0, 10.0)

I hate such syntax, but it is as it is. I would like Xojo to implement some syntactic sugar for a fast conversion as me.width.ToDouble doing some fast inline conversion behind the scenes instead of calling a method.

Any integer.ToDouble and Double.ToInteger should assume the real intention of promoting to Double and truncating to Integer, and doing it in the best available machine code for the context, without conversion warnings.

1 Like

you should use g.width and g.height
That works well.