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.
Related
In VB.net (Visual Studio 2015) how can I get the nth string (or number) in a comma-separated list?Say I have a comma-separated list of numbers like so:13,1,6,7,2,12,9,3,5,11,4,8,10How can I get, say, the 5th value in this string, in this case 12?I've looked at the Split function, but it converts a string into an array. I guess I could do that and then get the 5th element of that array, but that seems like a lot to go through just to get the 5th element. Is there a more direct way to do this, or am I pretty much limited to the Split function?
In case you are looking for an alternative method, which is more basic, you can try this:
Module Module1
Sub Main()
Dim a As String = "13,1,6,7,2,12,9,3,5,11,4,8,10"
Dim counter As Integer = 5 'the number you want (in this case, 5th one)
Dim movingcounter As Integer = 0 'how many times we have moved
Dim startofnumber, endofnumber, i As Integer
Dim numberthatIwant As String
Do Until movingcounter = counter
startofnumber = InStr(i + 1, a, ",")
i = startofnumber
movingcounter = movingcounter + 1
Loop
endofnumber = InStr(startofnumber + 1, a, ",")
numberthatIwant = (Mid(a, startofnumber + 1, endofnumber - startofnumber - 1))
Console.WriteLine("The number that I want: " + numberthatIwant)
Console.ReadLine()
End Sub
End Module
Edit: You can make this into a procedure or function if you wish to use it in a larger program, but this code run in console mode will give the output of 12.
The solution provided by Plutonix as a comment to my question is straightforward and exactly what I was looking for, to wit:result = csv.Split(","c)(5)In my case I was incrementing a variable each time my program ran and needed to get the nth character or string after the incremented value. That is, if my program had incremented the variable 5 times, then I needed the string after the 4th comma, which of course, is the 5th string. So my solution was something like this:result = WholeString.Split(","c)(IncrementedVariable)Note that this is a zero-based variable.Thanks, Plutonix.
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
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
like my title already explained, I want to get a substring of a string (who contains a address) and I would like to have only the street..
It's not possible to only take the text (non-numeric) chars, because then the box will remain.
It's not possible to take substring till first space, because the streetname can contain a space..
For example 'developerstreet 123a' -> would like to have 'developerstreet'
The 'a' is a box number of the house, which I'm not interested in..
How can I do this in VB.NET?
Parsing addresses is notoriously difficult, so I caution you to make sure that you a very deliberate about the choices you make. I would strongly recommend reviewing the documentation provided by the postal service. If these are US addresses, you should start by looking at the USPS Publication 28.
However, to answer your specific question, you can find the index of the first numeric character in a string by using the Char.IsDigit method. You may also want to take a look at the Char.IsNumber method, but that's probably more inclusive than what you really want. For instance, this will get the index of the first numeric character in the input string:
Dim index As Integer = -1
For i As Integer = 0 to input.Length - 1
If Char.IsDigit(input(i)) Then
index = i
Exit For
End If
Next
However, for complex string parsing, like this, I would suggest learning Regular Expressions. Getting the non-numeric portion at the beginning of a string becomes trivial with RegEx:
Dim m As Match = Regex.Match(input, "^\D+")
If m.Success Then
Dim nonNumericPart As String = m.Value
End If
Here is the meaning of the regular expression in the above example:
^ - The matching string must start at the beginning of the line
\D - Any non-numeric character
+ - One or more times
try this:
Private Sub MyFormLoad(sender As Object, e As EventArgs) Handles Me.Load
Dim str As String = "developerstreet 123a"
Dim index As Integer = GetIndexOfNumber(str)
Dim substr As String = str.Substring(0, index)
MsgBox(substr)
End Sub
Public Function GetIndexOfNumber(ByVal str As String)
For n = 0 To str.Length - 1
If IsNumeric(str.Substring(n, 1)) Then
Return n
End If
Next
Return -1
End Function
output will be: developerstreet
text.Substring(0, text.IndexOfAny("0123456789"))
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.