Const gconintRows1st As Integer = 15
Const gconintRows4th As Integer = 20
I am trying to convert String to Integer
by using:
intNumber = CInt(strNumberSelected(intFirst5Balls))
Professor's
intTemperature = CInt(strTemperatures(intMonth))
But some how this does not work.
The professor's version worked fine but I cannot figure out why the intNumber does not.
Yes, this is my first time doing vb
It looks like strNumberSelected is a string array, correct? And you're asking for the string in the array at position intFirst5Balls?
Take this for an example:
Dim strArray As String() = Split("Hi|there|everybody", "|")
The Split() function will split our long string at each occurrence of a pipe ("|").
The resulting string array will have the following 3 elements:
"Hi"
"there"
"everybody"
And you'd reference these elements by their indexes:
strArray(0) = "Hi"
strArray(1) = "there"
strArray(2) = "everybody"
If you're getting the error 'Char' values cannot be converted to 'Integer' then your array is more likely an array of type Char, and the CInt() function cannot convert it to an integer.
You can use
Integer.TryParse()
to try to get a valid Integer from your array, but it sounds a bit like you're unsure what sort of data actually lives in the array.
Related
is there any way to remove the element in char array I converted from a String?
Dim myString As String = "Hello"
Dim charArray As Char() = myString.ToCharArray
Your help would be much appreciated.. Thanks
Arrays are fixed-length in .NET. You can set an element to Nothing but you cannot remove that element. You can use a collection instead of an array and then you can add, insert and remove items at will, but your example isn't necessarily the best case for that sort of thing, e.g.
Dim str = "Hello"
Dim chars = New List(Of Char)(str)
You can then call Remove or RemoveAt on that List to remove a Char. You can then create a new String if desired, e.g.
chars.RemoveAt(2)
str = New String(chars.ToArray())
Console.WriteLine(str)
That will display "Helo".
I have a byte array need to convert to integer, and this array only have one value. I tried Bitconverter, convert.ToInt32 both are not working for me. my code as follows:
Dim a As new Byte() ={&H1C} ' the value range is {&H01} to {&HFF}
Dim key As integer = BitConverter.ToInt32(a,1)
I need the result with key = 28, which convert function I should use?
Thank you very much.
BitConverter.ToInt32 needs 4 bytes to work with, so you just need to put your one byte value into a 4 byte array. Allowing for the endianness, something like this:
Dim a() As Byte = { &H1C }
Dim b(3) As Byte
If BitConverter.IsLittleEndian Then
b(0) = a(0)
Else
b(3) = a(0)
End If
Dim key As Integer = BitConverter.ToInt32(b, 0)
You are not converting an array of values, but rather a single array element.
That said, there is no need to call a conversion function to convert a single Byte to an Integer. Just assign the value.
Dim key As Integer = a(0)
this is how to convert decimal to Ascii, but i have decimal in defferent format, and i want ot convert it to ascii.
Public Shared Function DecimalToASCII(dec As String) As String
Dim ascii As String = String.Empty
For i As Integer = 0 To dec.Length - 1 Step 3
ascii += CChar(ChrW(Convert.ToByte(dec.Substring(i, 3))))
Next
Return ascii
End Function
EX:
This is in Decimal and i want ot input it in textbox1.text:
"912,697,583,1065,261"
and i want to do operation to each group of numbers between comma and then convert it to Ascii???
To split the string into groups, you can just use the Split command. Your code is a long winded way of doing this - although it will fall over when it tries to deal with the four digit number.
Create a string array with no predefined number of elements like this -
Dim myArray()
Populate it with this code
myArray = Split (dec,",")
So now, using your input example, your ascii string array contains this data
myArray (0) = "912"
myArray (1) = "697"
myArray (2) = "583"
myArray (3) = "1065"
myArray (4) = "261"
If you want to have numbers that you can use in arithmetic operations, use this code instead. The function assumes that you're using integers, but if you want to use another type, just change all the occurrences of Integer to the type you want to handle, and change the CInt function to CDbl or cSng.
Public Shared Function DecimalToASCII(dec As String) As Integer()
'create an array of strings
Dim ascii() As String
'split each group into the array
ascii = Split(dec, ",")
'declare numbers array that is the same size as the ascii array
Dim numbers(ascii.GetUpperBound(0)) As Integer
'convert the array of strings to an array of numbers
For i As Integer = 0 To ascii.GetUpperBound(0) - 1
numbers(i) = CInt(ascii(i))
Next
'return an array of numbers containing each group
Return numbers
End Function
Use it like this
Dim dec As String = "912,697,583,1065,261"
Dim MyNumbersArray() As Integer = DecimalToASCII(dec)
With this code, you will have an array of Integers like this
MyNumbersArray(0) = 912
MyNumbersArray(1) = 697
MyNumbersArray(2) = 583
MyNumbersArray(3) = 1065
MyNumbersArray(4) = 261
Now you can perform whatever math you want using the array elements.
i got this far ... my data string, "num_str" contains a set of ~10 numbers, each separated by a comma. the last part of the string is a blank entry, so i use '.Trim' to avoid an error
Dim i As Integer
Dim m_data() As String
m_data = num_str.Split(",")
For i = 0 To UBound(m_data)
If m_data(i).Trim.Length > 0 Then
MsgBox(Convert.ToInt32(m_data(i).Trim))
End If
Next i
as you can see from the Msgbox, each of the numbers successfully pass through the loop.
where i am stuck is how to place all of the 'Convert.ToInt32(m_data(i).Trim)' numbers, which are now presumably integers, into an array.
how do i build an array of integers inside the For / Next loop so i can find MAX and MIN and LAST
TIA
You just need to initialize the array with the zero-based indexer. You can deduce it's initial size from the size of the string():
Dim m_data = num_str.Split({","c}, StringSplitOptions.RemoveEmptyEntries)
Dim intArray(m_data.Length) As Int32
For i = 0 To m_data.Length - 1
intArray(i) = Int32.Parse(m_data(i).Trim())
Next i
Note that i've also used the overload of String.Split which removes empty strings.
This way is more concise using the Select LINQ Operator.
Dim arrayOfInts = num_str.
Split({","c},
StringSplitOptions.RemoveEmptyEntries).
Select(Function(v) Int32.Parse(v.Trim()))
Dim minInt = arrayOfInts.Min()
Dim maxint = arrayOfInts.Max()
I have a ComboBox which I assign to a variable:
Dim var as String = ComboBox1.SelectedValue
Dim name As String = var.Split(",")
This gives me the error
Value of type '1-dimensional array of string' cannot be converted to String
Any ideas as to where I'm going wrong?
Split returns an array of strings. Your variable needs to be changed to an array, not just a single string.
My VB is a bit rusty, but I think you have to make name an array:
Dim name() As String = var.Split(",")
name needs to be declared as an array.
dim name() as string = var.split(",")
The split() method will break up the string based on the given character and put each newly created string into an array and return it.
This is what your error message is telling you:
Value of type '1-dimensional array of string' cannot be converted to String
The method returns an array of string, but your trying to put it into just a string!
EDIT: In response to your answer...
So far you've managed to split the string yourself with the split method. To output this to your message box, you need to concatenate the two elements in the proper order:
msgbox(name(1) & " " & name(0))
Notice I indexed the array twice! Element 1 is the first name, element 0 is the last name. Remember you got this name in lname,fname format. Passing the array itself doesn't make sense! Remember, a datatype is not equal to an array of that type, they are two different things. Therefore a string is not compatible with a string array. However, each individual element of the array is a string, and so each of those are compatible with the string type (because they're the same thing)!
Dim var As String = ComboBox1.SelectedValue
Dim temp() As String = Split(var, ",", -1, CompareMethod.Binary)
Dim name As String = temp(0)
Or maybe "name" isn't an array and the goal is to populate "name" with everything up until the first comma, in which case the fix would be:
Dim name as String = var.Split(",")(0)
Note: assumes that var is not Nothing.