What is used for holding String values in vb.net - vb.net

hi I would just like to ask
if Val(Textbox1.Text) is for holding values of integers.
what should I put when holding values for String??

Textbox1.Text is a string, so a simple string variable will work:
Dim s As String = Textbox1.Text
But, note that Val doesnt "hold" a value but is a function to convert a string to a value. And it always returns a Double, not Integer.
To convert a string to Integer use Convert.ToInt32 or CInt. When working with TextBoxes though where the user may enter illegal data such as "123foo45", you should test the contents to avoid an error:
Dim n As Integer
If Integer.TryParse(TextBox1.Text, n) Then
' text can parse, n holds the value
Else
' tell the user they entered bad info
End If

Related

VB.net Issue with a string comparison failing

Below is the snippit of code that is having the trouble.
Private Const DOB_VALUE As Integer = 0
Private Const ADDRESS_VALUE As Integer = 1
Private Const NAME_VALUE As Integer = 2
For Each oRecord As KeyValuePair(Of Integer, String) In OriginalFileInfo
For Each nRecord As KeyValuePair(Of Integer, String) In WorkingFileInfo
Dim OriginalComparisonStringSubstrings As String() = oRecord.Value.ToString.ToLower.Split(":")
Dim WorkingComparisonStringSubstrings As String() = nRecord.Value.ToString.ToLower.Split(":")
' Are dates of birth the same?
If OriginalComparisonStringSubstrings(DOB_VALUE) Like WorkingComparisonStringSubstrings(DOB_VALUE) Then
' Are the address the same?
If OriginalComparisonStringSubstrings(ADDRESS_VALUE) Like WorkingComparisonStringSubstrings(ADDRESS_VALUE) Then
' Dob and address are the same, means we have a valid match. Lets check if the names match
If OriginalComparisonStringSubstrings(NAME_VALUE) Like WorkingComparisonStringSubstrings(NAME_VALUE) Then
' dob, address and name matches
Else
' Dob and address matches, name does not
End If
End If
End If
Next
Next
The issue is, when I get to the address value comparison, it is always failing. I have had my watch window active and the values are identical yet they do not compare. You can see my Watch window output below.
- WorkingComparisonStringSubstrings {Length=3} String()
(0) "4323" String
(1) "123 somewhere lane" String
(2) "j ii" String
- OriginalComparisonStringSubstrings {Length=3} String()
(0) "4323" String
(1) "123 somewhere lane" String
(2) "j ii j. .johnson" String
OriginalComparisonStringSubstrings(DOB_VALUE) Like WorkingComparisonStringSubstrings(DOB_VALUE) True Boolean
OriginalComparisonStringSubstrings(ADDRESS_VALUE) Like WorkingComparisonStringSubstrings(ADDRESS_VALUE) False Boolean
OriginalComparisonStringSubstrings(NAME_VALUE) Like WorkingComparisonStringSubstrings(NAME_VALUE) False Boolean
The comparison for the two addresses, which would be ("123 somewhere lane" Like "123 somewhere lane") should be true but is returning false. My question is why are these values failing in comparison when they are so apparently equal? In my code I am using "Like" comparisons but I have also tried String.Compare, StrComp, .Equals, =, and every other variation of comparison. In addition, the values are always a string from the time they are inserted into the FileInfo variable to the time they are split and compared.
Anyone have an idea of why they wont compare?
My colleague pointed out the issue. In the previous version of the program, we were dynamically changing the value of ADDRESS_VALUE. In this most recent version is when we changed the the values to a constant integer.
It appears in this version, before we made ADDRESS_VALUE constant, it was still being assigned a new value, so it was actually comparing the NAME_VALUE rather than comparing the ADDRESS_VALUE which would in fact fail.
Thank you everyone for your comments. They were really helpful.

Convert Textbox value to Integer

I have the following Integer variables
Dim sMaxAmount As Integer
Dim sMinAmount As Integer
I am trying to compare them with a TextBox field.
If (Convert.ToInt32(txtTransactionAmount) < sMinAmount And Convert.ToInt32(txtTransactionAmount) > sMaxAmount) Then
Although I am converting it to Integer I get exception
Unable to cast object of type 'System.Web.UI.WebControls.TextBox' to
type 'System.IConvertible'.
What am I doing wrong?
Typo: use txtTransactionAmount.Text instead of txtTransactionAmount
If (Convert.ToInt32(txtTransactionAmount.Text) < sMinAmount AndAlso Convert.ToInt32(txtTransactionAmount.Text) > sMaxAmount)
Val(TextBox.Text) will convert the value of a TextBox to an Integer.
txtTotal.text= Val(txtPrice.text) * Val(txtQuantity.text)```
In addition to not using the Text property to get the string contained in the TextBox, there are two other problems with your code.
You are not validating the contents of the TextBox. If the user enters something that can't be converted to an integer, an exception will be thrown.
The test you are doing doesn't make sense given the names of the variables. The value in the TextBox can't be both less than the minimum and more than the maximum.
The following code uses Integer.TryParse to validate the contents of the TextBox and convert it to an Integer. It also checks that the value in greater than or equal to sMinAmount and less than or equal to sMaxAmount.
Dim amount As Integer
If Integer.TryParse(txtTransactionAmount.Text, amount) _
AndAlso amount >= sMinAmount AndAlso aamount <= sMaxAmount Then
'The Integer called "amount" now contains a value between sMinAmount and sMinAmount
End If

How to verify an Integer? VB.Net

How do verify a value entered in an input box is an integer? If it's not I want the input box to show up again no harm done. Thanks for any help in advance
Dim int As Integer
If Integer.TryParse("12345", int) Then
'use int variable it holds the converted value
End If
as #OneFineDay said you have to use Integer.TryParse, now to repeat the question untill it is an integer you can do this:
dim ans = As String
dim int As Integer
dim isInteger As Boolean = False
do While Not isInteger
ans = InputBox("Give me an Integer")
isInteger = Integer.TryParse(ans, int)
End do
''Here int holds an integer
It wouldn't be an input validation question without a Regex answer, so if you want to make it more complicated than it needs to be then you can use something like
Dim expression As New Regex("^-?\d+$")
If Not expression.IsMatch(textBox1.Text) Then
textBox1.Text = String.Empty
End If
The Regex pattern will look at all entered text, and match iff there is zero or one minus signs followed by at least one digit.
12345 represents text that you can either enter manually in quotes or can grab from an input box like this:
Integer.TryParse(InputBox("Enter integer here"),myInt) ,such that the input from the inputbox is the one that will be converted into the integer variable,this saves you memory no need to declare another variable.
The loop in #bto.rdz 's answer is quite handy though,especially if you want the user to enter an integer no matter what

how to i create a Username shortener?

I have an AD username called "lastname-132" in Textbox1, this string is 12 long, so i want to add the username in to Textbox2, but shortened, in the textbox2 i only have a string length of only 10 available due to other tools this program is using, so i don't want to convert it all the time manually and want to just convert it automatically with a onleave event.
Anyone any idea how to write this?
So the End Result should look like this.
'String length can be 20 max.
Textbox1.Text = "lastname-123"
'some code to convert it to this:
'String length 10 max. Numbers and the "-" should stay the same, but remove letters if necessary.
Textbox2.Text = "lastna-123"
Here's the concept:
Split string based on '-' into 2 strings
In the example above: 'lastname' and '123'.
Check the length of the first string and cut if it is too long
the program checks 'lastname' and finds that it is too long, then
cuts it into 'lastna'
Combine 'lastna' and '123' back into a string
I hope this helps
Without more information, this will assume that there can be multiple hyphens, the number can be of variable length, and you can change the maximum length of the string by changing one variable.
Dim username As String = "lastname-123"
Dim max As Integer = 10
Dim lindex As Integer = username.LastIndexOf("-")
Dim numberLength As Integer = username.Length - lindex
Dim number As String = username.Substring(lindex)
Dim justName As String = username.Substring(0, lindex)
If justName.Length + numberLength >= max Then
username = justName.Substring(0, max - numberLength) & number
End If
If you are concentrating only on the restriction of length of characters to be accepted then you can use
Maxlength
property of the Textbox.
Ex: Maxlength="10"
restricts the Textbox to accept only 10 characters.
Try to make it fit with for example substring manipulation. See http://msdn.microsoft.com/en-us/library/dd789093.aspx for more info.

String.ToCharArray() stored in StringCollection conversion to numbers and back in VB.net

I have a string which was converted to a char array and stored in a stringcollection, how do I return the numerical value for the character in each string? How can I reverse this process, getting the character from the numerical value?
I know I could code a Select Case statement, but that would take a very long time as I need to cover every character a person could want to conceivably use in the English language, including punctuation.
Is there already a method built into vb.net for doing this?
Thanks for the help!
The Convert class has methods that can convert between characters and integers:
Dim c As Char = "A"C
Dim i As Integer = Convert.ToInt32(c)
Dim c2 As Char = Convert.ToChar(i)
To loop the values converted from the characters in a string into an array of integers:
Dim codes(theString.Length - 1) As Integer
For i As Integer = 0 to theString.Length - 1
codes(i) = Convert.ToInt32(theString.Chars(i))
Next
Try something like this:
For Each c As Char In yourString
Dim i As Int32 = DirectCast(c, Int32)
Next
Remeber that System.String implements IEnumerable<Char> so it is legal to For Each over it. And converting between a character and a number is as simple as casting between System.Char and System.Int32 (here I have shown how to get the numeric value for each character in the string).