Convert Integer to Single - vb.net

I am using the Graph Control made by a user named "Aeonhack" in my .NET application. When adding, let's say, a point with the size 9, it has to be a Single like 9.0F.
So I want a function that converts an Integer like 9 into a single like 9.0F.
The normal CType won't work, and I also tried:
Private Function IntToSingle(ByVal Number As Integer) As Single
Return CType(Number & ".0F", Single)
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
GraphConnections.AddValue(IntToSingle(7))
End Sub
This gives me this error:
Conversion from string "7.0F" to type 'Single' is not valid.
How can I fix this?

Use Convert.ToSingle:
Convert.ToSingle(Number)
Or just CType, without weird string concatenation:
CType(Number, Single)

I think the confusion is with 7.0F. That is not the same thing as the string "7.0F".
Per Microsoft
Appending the literal type character F to a literal forces it to the
single data type.
You could simply use the F to force a double literal to be a single data type.
GraphConnections.AddValue(7.0F)
If, however, you receive an integer variable, then you would need to convert or cast it to a single as suggested by #MarcinJuraszek.
dim point As Integer = 7
GraphConnections.AddValue(Convert.ToSingle(point))

You can use this to convert to a single: CSng(9), though I think this only works in VB.NET.

Related

Recognize two or more digits

How to make Visual Basic .NET recognize two or more digits instead of one?
When I execute it, it only validates correctly in case both numbers are one digit.
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim menorOigual As Boolean
menorOigual = numero1.Text <= numero2.Text
MsgBox(menorOigual)
End Sub
By comparing the value of the Text properties, you're comparing strings, which is why it only works with single digits (because as a string "10" comes before "3", because each character is compared in turn). You need to convert the strings to a numeric type like integer, e.g.
Convert.ToInt32(numero1.Text)
For production code, you'll probably also need to validate the conversion, so see also:
Integer.TryParse

Widening: Change in representation

Requesting your help to understand the concept of widening better!
I came across the following statement w.r.t 'Widening Conversion' in VB.Net. From the msdn documentation on the topic I found the following: Widening conversions preserve the source value but can change its representation. This occurs if you convert from an integral type to Decimal, or from Char to String. The link to the page is found below:
https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/data-types/widening-and-narrowing-conversions
My Question is as follows: I wrote the following code to understand the meaning of the statement "...preserve the source value but can change its representation". But I see no difference in the output when I print the integer or the decimal. Then what does the phrase mean....what is the meaning of "...can change its representation"?
Module Module1
Sub Main()
Dim i As Integer = 5
Dim d As Decimal = i 'widening
Console.WriteLine(i)
Console.WriteLine(d)
'Both prints "5"...no difference in representation
Console.ReadLine()
End Sub
End Module
Can someone also please give an example to demonstrate how the representation changes when we convert a char value to a string?
It means the internal presentation of the number (in your case). Try to convert, say, Single to Double:
Sub Main(args As String())
Dim sng As Single = 1.23E+32
Dim dbl As Double = sng
Console.WriteLine($"Single: {sng}")
Console.WriteLine($"Double: {dbl}")
End Sub
' Output:
' Single: 1,23E+32
' Double: 1,2300000079302825E+32

How to use CStr to convert generic parameter to String?

I wrote a generic Sub and need to convert the input values to String to get them into a TextBox(in this case a powerpoint textbox). This looks like:
Sub InsertValueIntoTextbox(Of t)(ByVal sldNr As Integer, ByVal tbName As String, ByVal valueToInsert As T)
_pptSld = _pptPre.Slides(sldNr)
_pptSld.Shapes(tbName).TextFrame.TextRange.Text = CStr(valueToInsert)
End Sub
My problem is that it gives me this compiler-error:
The value of t cannot be converted into string
I tried a CStr (as you can see in the snippet) but this doesnt help too. Could anyone help me get this to work and can tell me why I cannot convert T to CStr?
Every Object can ToString, so why dont you use that?
Dim text = If(valueToInsert Is Nothing, "", valueToInsert.ToString())
_pptSld.Shapes(tbName).TextFrame.TextRange.Text = text
You cannot use CStr on generics. The type of the parameter must be convertible to String which is not guaranteed in your case. That's why you get the compiler error. I wouldn't use those old VB functions anymore. There are always .NET alternatives.

Why does a negative SByte number have 16 bits in VB.Net?

I asked a question earlier about comparing numbers using the "And" comparison operator in If Statements and now I have been toying around with getting my head wrapped around bitwise operators. So I have written a very basic code that will allow me to see the conversion of any decimal number in binary format.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(ConvertToBinary(-1))
End Sub
Public Function ConvertToBinary(ByVal someInteger As SByte) As String
Dim converted As String = Convert.ToString(someInteger, 2) '.PadLeft(8, "a"c)
Return converted
End Function
Notice here that I am using SByte as the paramerter - which should only contain 8 bits right? However, the message box that appears has 16 bits assigned to negative numbers. Positive numbers have the correct 8.
There is no Convert.ToString overload that takes an SByte, so the SByte is being implicitly converted to a Short.

Left value of an integer

I have a value in variable, say it is dim a as integer = 145.98
I tried to take the Left(a,2)
but it returned an error instead of returning 14
I also tried left(a.Tostring,2)
but error is the same.
Please help me solve it.
Thanks
Furqan
First off, you say that you’re using an integer but the number is actually a floating-point number, not an integer.
Secondly, the action “take the left of a number” isn’t a meaningful operation. Left is a substring operation, it’s only defined on strings.
You can turn the number into a string and then extract a substring of the decimal representation. This should work. What’s the error?
Finally, some general advice:
Put Option Strict On at the very top of ever vb file, or better yet, make this option the default in your settings. Otherwise, you’ve got a veritable hullaballoo waiting to happen because VB is very … “lenient” when it comes to questionable or downright incorrect code. This option fixes this and will flag a lot more errors. For example, the compiler would (rightfully) have complained about your assignment,
Dim a As Integer = 145.98
because as I said, you’re trying to assign a floating-point number to an integer.
First of all, 145.98 is not an integer. 145 is an integer. You might want to try Double. Second, you can only take the left of a string. You were on the right track when you added ToString, but you forgot the ()s at the end.
Dim a as Integer = 145
Dim b as Double = 145.98
Then you can do this:
Left(a.ToString(), 2)
Left(b.ToString(), 2)
Try Left(a.ToString(), 2) instead.
If your "integer" is a string, try this:
Dim a As String = "145.98"
Dim b As Int32 = 0
Int32.TryParse(a.Substring(0, 2), b)
As Konrad has said with option strict this would not compile. If you don't have it on it will perform a conversion and store only the integer part. At this point you can call tostring and then performe any operation on it as a string you would like.
With option strict
Option Strict On
Module Module1
Sub Main()
Dim I As Integer = CType(142.3, Integer)
Dim s As String = I.ToString
Console.WriteLine(Left(s, 2))
End Sub
End Module
With out optiion strict
Module Module1
Sub Main()
Dim I As Integer = 142.3
Dim s As String = I
Console.WriteLine(Left(s, 2))
End Sub
End Module
As you can see from the two example option strict will attempt to perform many conversions for you but does so at the risk of unexpected results.