Why am I getting an InvalidCastException here? - string

I'm trying to write code to print out "Row (0 to 3): ".
Here's my code:
ReadOnly gridSize As Integer = 4
Dim s1 As String
s1 = "Row (0 to " & (gridSize - 1) & "): "
WriteLine(s1)
I'm getting an InvalidCastException with the following error at the last line, when the machine tries to print out the string:
Conversion from string "Row (0 to 3): " to type 'Integer' is not valid.

Integer is not a String.
How about using gridSize.ToString() ?

Related

How to convert a "fraction" character into a decimal?

I have a dataset where the format of data is like this:
10 ¾ AB 02/15/19
I'm trying to convert the ¾ into .75 so that the data looks like:
10.75 AB 02/15/19
I am thinking of trying to iterate through each character in the string, but once I do so how can I convert the ¾ into .75?
Simple set of string replacements:
Public Function numerify(s As String) As String
numerify = Replace(s, " " & ChrW(190), ".75")
numerify = Replace(numerify, " " & ChrW(188), ".25")
numerify = Replace(numerify, " " & ChrW(189), ".5")
End Function
NOTES:
We replace the space before the fraction as well as the fraction to get the desired result.There may be other uni-code "fractions" you may need to consider.

Conversion from string (x) to type 'Integer' is not valid

doing something for class and i'm completely stuck, even teacher seems unsure. any response appreciated. it's the first printline that is causing problems:
"A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Additional information: Conversion from string "1: Night F3, 10 tickets purchase" to type 'Integer' is not valid."
Dim filename As String
Dim fridaytickets As Integer
fridaytickets = 0
filename = "Z:\Computing Science\S5 (Higher)\Coursework Assessment\output.txt"
FileOpen(1, filename, OpenMode.Output)
For x = 1 To 300
If Mid(TicketID(x), 1, 1) = "F" Then
PrintLine((x) & ": Night " & TicketID(x) & ", " & TicketNo(x) & " tickets purchased. £" & (TicketNo(x) * 10) & " made.")
fridaytickets = fridaytickets + TicketNo(x)
End If
PrintLine(fridaytickets & " were purchased for Friday night.")
PrintLine("£" & (fridaytickets * 10) & " was made.")
This is the line:
PrintLine(fridaytickets & " were purchased for Friday night.")
Your fridaytickets is an integer, Strangely, it seems that & is taken as binary operator, so both side with integer. It should not be so, but maybe it is a bug or version dependent. Or just an overloading of the normal concatenation operator.
In any case, the correct way is to prepare the strings to be concatenated:
PrintLine(fridaytickets.toString & " were purchased for Friday night.")

VB.NET Get Number Position Of Char In String (Index Of)

im having a hard time getting a function working. I need to search message.text for each "," found, for each "," found I need to get the number position of where the "," is located in the string. For example: 23232,111,02020332,12 it would return 6/10/19 where the "," are located (index of). My code finds the first index of the first , but then just repeats 6 6 6 6 over, any help would be appreciated thanks.
Heres my code:
For Each i As Char In message.Text
If message.Text.Contains(",") Then
Dim data As String = message.Text
Dim index As Integer = System.Text.RegularExpressions.Regex.Match(data, ",").Index
commas.AppendText(index & " ")
End If
Next
You can try it this way; instantiate a Regex object and increment each time the position from which you start the matching (this possibility is not available with the static method Match).
Dim reg As New System.Text.RegularExpressions.Regex(",")
Dim Index As Integer = reg.Match(data).Index
Do While Index > 0
commas.AppendText(index & " ")
Index = reg.Match(data, Index + 1).Index
Loop
p.s the returned indices are zero-based.
Just use the Regex.Matches method
Dim message As String = "23232,111,02020332,12"
Dim result As String = ""
For Each m As Match In Regex.Matches(message, ",")
result &= m.Index + 1 & " "
Next
I should also add that indexes are 0 based (which is why +1 is added to m.Index). If you later need these values to point to the position of a particular comma, you may be off by 1 and could potentially try to access an index larger than the actual string.

Condition Inside string Concatenation VB.NET

In PHP you can have conditions (if/else) inside a string concatenation.
$string= 'X is' . ($x >0 1 ? ' > 10 ': ' < 10 ')';
Is this same thing possible in VB.NET?
You can use string inpterpolation and the If-operator:
Dim result = $"X is {If(x > 10, " > 10 ", " <= 10 ")}"
Which is syntactic sugar for String.Format:
Dim result = String.Format("X is {0}", If(x > 10, " > 10 ", " <= 10 "))
Yes, with string interpolation you can use expressions - Interpolated Strings
Dim text = $"IsPositive = {If(number > 0, "true", "false"}"
An interpolated string expression creates a string by replacing the
contained expressions with the ToString represenations of the
expressions’ results
The VB equivalent of the ternary operator is the If operator (as distinct from the If statement).
You can translate your code directly from php to VB:
Dim test = "X is" & If(x > 10, " > 10 ", " < 10 ")
(I made some corrections for apparent errors in the source, but I'm not familiar with php so I may have inadvertently introduced differences in behavior.)

Lpad with zero's in vbscript

I'm trying to pad a string with 0's to the left.The length of the output string should be 7.
Here's my code :
inputstr = "38"
in = string(7 - Len(inputStr),0) & inputStr
msgbox in
I'm getting error Expected Statement
Please help me
Thank You
The following code will run 5% faster:
inputStr = "38"
result = Right("0000000" & inputStr, 7)
msgbox result
This function will left-pad an input value to the given number of characters using the given padding character without truncating the input value:
Function LPad(s, l, c)
Dim n : n = 0
If l > Len(s) Then n = l - Len(s)
LPad = String(n, c) & s
End Function
Output:
>>> WScript.Echo LPad(12345, 7, "0")
0012345
>>> WScript.Echo LPad(12345, 3, "0")
12345
in is a reserved word so can't be used as a variable name and you must pass a string "0" not an integer 0, so:
inputStr = "38"
result = string(7 - Len(inputStr), "0") & inputStr
msgbox result
Function:
Private Function LPad (str, pad, length)
LPad = String(length - Len(str), pad) & str
End Function
Use:
LPad(12345, "0", 7)

Resources