I recently coded a program on VB.NET that shows Input1 Mod Input2 for example. Here i use function :
Private Function remainder(intno1 As Integer, intno2 As Integer) As Integer
Dim intresult As Integer
intresult = intno1 Mod intno2
remainder = intresult
End Function
Then i called it on Button_Click event :
Dim intm As Integer, intn As Integer
Dim intmod As Integer
intm = Val(TextBox1.Text)
intn = Val(TextBox2.Text)
If TextBox1.Text >= TextBox2.Text Then
MsgBox("Error while computing proccess ! Please try Again.", vbOKOnly + vbCritical, "Error!")
**Stop statement**
End If
intmod = remainder(intm, intn)
RichTextBox1.Text = "The result is " & Str(intmod)
as you can see on this code, i use if statements when txt1 is bigger or equals txt2 then message box shows. before End If clause i want to use statement that stop running the code. i mean stop doing those process on Function.
I use Stop and End statements but after i debug the program, it stop responding or even exit the program.
What should i do exactly here ?
You would use an Exit Sub call to stop executing that routine right there. If it had been a function, Exit Function
BTW, you should use CInt rather than Val to force to integer and your message should be more helpful (i.e., what was the error while computing?). Something along the lines of "First integer must be less than the second" would be more useful to a user.
Try one of these to exit a method
Exit Sub
Or
Return
Simple use :
Dim intm As Integer, intn As Integer
Dim intmod As Integer
intm = Val(TextBox1.Text)
intn = Val(TextBox2.Text)
If TextBox1.Text >= TextBox2.Text Then
MsgBox("Error while computing proccess ! Please try Again.", vbOKOnly + vbCritical, "Error!")
Exit Sub
End If
intmod = remainder(intm, intn)
RichTextBox1.Text = "The result is " & Str(intmod)
when you want exit from a function without return any value, It shows the software failure , and must not give the impression of 'business as usual' to the responsible caller code.
for this you need Throw an exception:
Throw New Exception("Error while computing proccess ! Please try Again.")
in this case the exception its not a suprise, it is likely command will be executed inadvertentlyexecuted inadvertently when the input text-boxes not correspond to expectations.
In this case there is no reason to throw an error, but has prevented the execute function already in the code caller, not in the function body.
Related
I have a form in where users need to input integers to save to an xml when they press a button
the issue lies that when the user does not put in valid input, it displays the error but still attempts to declare the variables for my function and then crashes - how do i stop this?
here's my code:
If IsNumeric(txtLevel.Text) And IsNumeric(txtHealth.Text) Then
MsgBox("choose a location to save in")
ElseIf String.IsNullOrWhiteSpace(txtHealth.Text) Or String.IsNullOrWhiteSpace(txtLevel.Text) Then
MsgBox("you have not filled in all fields")
Me.Close()
Else MsgBox("please input number form")
End If
Dim strName As String = txtCharacter.Text
Dim intLevel As Integer = txtLevel.Text
Dim intHealth As Integer = txtHealth.Text
Dim strStat As String = cmbStat.Text
You are using quite a few legacy features and you also have Option Strict turned off.
IsNumeric should be replaced with Integer.TryParse: https://learn.microsoft.com/en-us/dotnet/api/system.int32.tryparse
MsgBox should be replaced with MessageBox: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.messagebox
For more information on option strict, I suggest you check out any of these resources:
https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/option-strict-statement
https://www.vblessons.com/lessons.html#/1/2
http://vb.net-informations.com/language/vb.net_option_strict.htm
With that being said, your code could look like this:
Dim strName As String = txtCharacter.Text
Dim strStat As String = cmbStat.Text
Dim intLevel, intHealth As Integer
' if either condition is true, close the form prematurely
If (String.IsNullOrWhitespace(txtLevel.Text) OrElse String.IsNullOrWhitespace(txtHealth.Text)) Then
MessageBox.Show("You have not filled in all fields.", "Invalid Form")
Me.Close()
ElseIf (Not Integer.TryParse(txtLevel.Text, intLevel) OrElse Not Integer.TryParse(txtHealth.Text, intHealth)) Then
MessageBox.Show("Please input number form.", "Invalid Form")
Me.Close()
End If
' continue on with the code
Move your variable declaration inside your "If IsNumeric(txtLevel.Text) And IsNumeric(txtHealth.Text) Then" so its only going to try to assign them the value if both txtLevel and txtHealth are integers.
I would suggest you to change the code like so:
'Checks if the textboxes are not empty
If Not String.IsNullOrWhiteSpace(txtHealth.Text) Or String.IsNullOrWhiteSpace(txtLevel.Text) Then
'They are not empty, so the program checks if they are integers
If IsNumeric(txtLevel.Text) And IsNumeric(txtHealth.Text) Then
'The input is integer
Dim strName As String = txtCharacter.Text
Dim intLevel As Integer = txtLevel.Text
Dim intHealth As Integer = txtHealth.Text
Dim strStat As String = cmbStat.Text
MsgBox("choose a location to save in")
Else
'The input is not integer
MsgBox("Value is not an integer")
End If
Else
'A textbox is empty
MsgBox("you have not filled in all fields")
Me.Close()
End If
you can try with TryParse to be sure of integer value without error :
Dim intLevel As Integer
Dim intHealth As Integer
If (Integer.TryParse(txtLevel.Text, intLevel)) And (Integer.TryParse(txtHealth.Text, intHealth)) Then
MsgBox("choose a location to save in")
ElseIf String.IsNullOrWhiteSpace(txtHealth.Text) Or String.IsNullOrWhiteSpace(txtLevel.Text) Then
MsgBox("you have not filled in all fields")
Me.Close()
Else MsgBox("please input number form")
End If
Dim strName As String = txtCharacter.Text
Dim strStat As String = cmbStat.Text
find more about it on : Int32.TryParse Method
private Sub Command1_Click()
a = InputBox("What is the Number ?", "Input Example"," ")
If a = "-1" Then
End If
End Sub
the whole question is: Enter some numbers until a negative value is entered to stop the program(stop running the what is your number thing). then find the average of the entered numbers.
What I want to do, for now, I want to know how can I make my "a" variable accept any negative value like in the code above it stops when I enter -1 but I want to stop when I enter -3, -10, or any negative value.
There are some helpful answers down below
If you are expecting the usual input to be text then you can use the Double.TryParse method to check if a number was entered. If it was, then you can check if that number is negative, and exit the application if so:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim userMsg As String
userMsg = Microsoft.VisualBasic.InputBox("What is your message?", "Message Entry Form", "Enter your message here", 500, 700)
If userMsg <> "" Then
Dim x As Double
If Double.TryParse(userMsg, x) AndAlso x < 0 Then
Application.Exit()
End If
MessageBox.Show(userMsg)
Else
MessageBox.Show("No Message")
End If
End Sub
The AndAlso operator only looks at the second argument if the first evaluated to True.
If you would like to repeat some portion of code till specific condition is met, you need to use:
While...End While Statement (Visual Basic)
or
Do...Loop Statement (Visual Basic)
It's also possible to write conditional loop using For... Next statement
Dim myNumber As Integer = 0
Dim mySum As Integer = 0
Dim myCounter As Integer = 0
Do
Dim answer As Object = InputBox("Enter integer value", "Getting average of integer values...", "")
'check if user clicked "Cancel" button
If answer <> "" Then
'is it a number?
If Int32.TryParse(answer, myNumber)
If myNumber >=0 Then
mySum += myNumber
myCounter += 1
Else
Exit Do
End If
End If
End If
Loop
Dim average As Double = mySum/myCounter
'you can display average now
Tip: Do not use InputBox, because this "control" is VisualBasic specific. Use custom Form instead. There's tons of examples on Google!
I'm a student trying to learn Console App .Net Framework and I want to code a random number between 0000 and 9999 (as a pin that you need to guess). Thus far I've had to set it as a random number from 1000 to 9999 as the system wont let me do 0000. Furthermore, I want to save the amount of tries the user has as a text file e.g. if the user tries 50 times, I'd like it to say
Username Tries
I've tried Randomise() Rnd(*9999) and X = EasyNum.Next(1000, 9999) but then I can't compare unless I convert that to an integer.
Module Module1
Dim Tries As String = 0
Dim EasyNum As New Random
Dim HardNum As New Random
Dim Attempt As String
Sub Main()
Dim Difficulty As String
Console.WriteLine("Welcome to MasterMind")
Console.WriteLine("Choose between Easy and Hard difficulty")
Difficulty = Strings.LCase(Console.ReadLine)
While Difficulty <> "easy" And Difficulty <> "hard"
Console.WriteLine("That's not a correct mode")
Difficulty = Strings.LCase(Console.ReadLine)
End While
If Difficulty = "easy" Then
Easy()
ElseIf Difficulty = "hard" Then
Hard()
End If
End Sub
Dim EasyGuess1 As Integer
Dim EasyGuess2 As Integer
Dim X As String
Dim Y As Integer
Sub Easy()
Console.WriteLine("You have chosen the easy difficulty")
Console.WriteLine("You have to guess a 4 Digit number between 1000 and 9999")
Console.WriteLine("Enter your guess")
X = EasyNum.Next(1000, 9999)
Console.WriteLine(X)
EasyGuess1 = Console.ReadLine
Tries = +1
If Mid(CStr(EasyGuess1), 1, 1) = Mid(CStr(X), 1, 1) Then
Console.WriteLine("You have 1 number correct, try again?")
Attempt = Strings.LCase(Console.ReadLine)
While Attempt <> "yes" And Attempt <> "no"
Console.WriteLine("Enter either yes or no")
Attempt = Strings.LCase(Console.ReadLine)
End While
ElseIf Mid(CStr(EasyGuess1), 2, 1) = Mid(CStr(X), 2, 1) Then
Console.WriteLine("You have 1 number correct, try again?")
Attempt = Strings.LCase(Console.ReadLine)
While Attempt <> "yes" And Attempt <> "no"
Console.WriteLine("Enter either yes or no")
Attempt = Strings.LCase(Console.ReadLine)
End While
End If
If Attempt = "yes" Then
EasyYes()
ElseIf Attempt = "no" Then
EasyNo()
End If
Console.WriteLine("incorrect")
Console.ReadKey()
End Sub
Sub EasyYes()
Console.WriteLine("Enter a new guess")
EasyGuess1 = Console.ReadLine
End Sub
Sub EasyNo()
Dim Save As String
Dim File As System.IO.File
Console.WriteLine("Do you want to save your tries? Enter Yes or no")
Save = Strings.LCase(Console.ReadLine)
If Save = "yes" Then
System.IO.File.Create(Tries, "C:\X\Code\VB\Challenges\Challenge 1\MasterMind Test")
End If
End Sub
Sub Hard()
Console.WriteLine("You have chosen the hard difficulty")
End Sub
Sub HardYes()
End Sub
Sub HardNo()
End Sub
End Module
When I try to save the tries, I get this:
System.InvalidCastException: 'Conversion from string "C:\X\Code\VB\Challenges\Cha" to type 'Integer' is not valid.'
InnerException
FormatException: Input string was not in a correct format.
Which I don't understand myself.
Comments in line. Please read all comments. This program is still not working too well but I will leave it to you to tidy up.
Module Module1
Dim Tries As Integer = 0
'Use only a single instance of Random
Dim EasyNum As New Random
'Dim HardNum As New Random
Dim Attempt As String
Sub Main()
Dim Difficulty As String
Console.WriteLine("Welcome to MasterMind")
Console.WriteLine("Choose between Easy and Hard difficulty")
Difficulty = Strings.LCase(Console.ReadLine)
'AndAlso prevents the second condition from executing if the first condition is true
While Difficulty <> "easy" AndAlso Difficulty <> "hard"
Console.WriteLine("That's not a correct mode")
Difficulty = Strings.LCase(Console.ReadLine)
End While
If Difficulty = "easy" Then
Easy()
'ElseIf Difficulty = "hard" Then
' Hard() 'Not implemented
End If
End Sub
'Dim EasyGuess2 As Integer
'Dim X As String
'Dim Y As Integer
Sub Easy()
Dim EasyGuess1 As Integer
Console.WriteLine("You have chosen the easy difficulty")
Console.WriteLine("You have to guess a 4 Digit number between 1000 and 9999")
Console.WriteLine("Enter your guess")
'X = EasyNum.Next(1000, 9999)
'The next method returns a non-negative integer not a string
'To get 0 to 9999 with leading zeros do the following
'Returns a non-negative random integer that is less than the specified maximum.
Dim X = EasyNum.Next(10000).ToString("0000")
Console.WriteLine(X)
'Console.ReadLine returns a String. You should Not assign a String to an Integer
'EasyGuess1 = Console.ReadLine
'Never trust a user :-) Use .TryParse to set the variable
Integer.TryParse(Console.ReadLine, EasyGuess1)
'This just assigns the value of 1 to Tries
'Tries = +1
'If you want to increment Tries
Tries += 1
'Let's compare apples and apples
'If you just convert EasyGuess1 To a string and EasyGuess1 is 54
'the string is "54" You want a 4 character string
'If Mid(EasyGuess1.ToString("0000"), 1, 1) = Mid(CStr(X), 1, 1) Then
' 'but you only compared the first character, what about the rest?
' 'Mid is aroung for backward compatibility. It should not be used for new code.
' 'The position Integer is one based which is not at all sympatico with .net
' Console.WriteLine("You have 1 number correct, try again?")
' Attempt = Strings.LCase(Console.ReadLine)
' While Attempt <> "yes" And Attempt <> "no"
' Console.WriteLine("Enter either yes or no")
' Attempt = Strings.LCase(Console.ReadLine)
' End While
'ElseIf Mid(CStr(EasyGuess1), 2, 1) = Mid(CStr(X), 2, 1) Then
' Console.WriteLine("You have 1 number correct, try again?")
' Attempt = Strings.LCase(Console.ReadLine)
' While Attempt <> "yes" And Attempt <> "no"
' Console.WriteLine("Enter either yes or no")
' Attempt = Strings.LCase(Console.ReadLine)
' End While
'End If
Dim CorrectCharacters = TestInput(EasyGuess1, X)
Console.WriteLine($"You have guessed {CorrectCharacters} correctly. Try again?")
'Use the .net way. The framework is available to all languages in .net
'so it will be easier to learn other languages.
Attempt = (Console.ReadLine).ToLower
If Attempt = "yes" Then
EasyYes()
ElseIf Attempt = "no" Then
EasyNo()
End If
Console.WriteLine("incorrect")
Console.ReadKey()
End Sub
Sub EasyYes()
Dim guess As Integer
Console.WriteLine("Enter a new guess")
'You are doing this twice ???
'EasyGuess1 = Console.ReadLine
Integer.TryParse(Console.ReadLine, guess)
'EasyGuess1 will be 0 if the entry is other than an integer
'Very nice bu this Sub doesn't do anything
End Sub
Sub EasyNo()
Dim Save As String
'Unused local variable and if you needed this it is a poor choice for a variable name
'Dim File As System.IO.File
Console.WriteLine("Do you want to save your tries? Enter Yes or no")
Save = Console.ReadLine.ToLower
If Save = "yes" Then
'Give the file name an extension.
'When you pass (String, Integer) to the Create method, the Integer is the number of bytes
'buffered for reads And writes to the file.
'Not exactly what you were expecting.
'System.IO.File.Create("C:\X\Code\VB\Challenges\Challenge 1\MasterMind Test.txt", Tries)
'Imports System.IO at top of file
File.WriteAllText("C:\X\Code\VB\Challenges\Challenge 1\MasterMind Test.txt", Tries.ToString)
End If
End Sub
Sub Hard()
Console.WriteLine("You have chosen the hard difficulty")
End Sub
Private Function TestInput(Guessed As Integer, RandString As String) As Integer
Dim Correct As Integer
Dim Guess As String = Guessed.ToString("0000")
'A String can be a Char array
'Here we check each character in the 2 strings for equality
'and if true then increment Correct
For i = 0 To 3
If Guess(i) = RandString(i) Then
Correct += 1
End If
Next
Return Correct
End Function
End Module
There are a lot of issues, but I don't want to spoil it for you.
I definitely recommend to put "Option Strict On" in the project settings, so you immediately see where there are conversion errors (a string assigned to a integer etc).
To save the file, it should be something like
If Save = "yes" Then
File.WriteAllText("C:\X\Code\VB\Challenges\Challenge 1\MasterMind Test", Tries.ToString())
End If
(there are also File.Append... functions).
The Random class is a bit tricky, this is an object that provides random values and is not yet the random value itself. Always use the same random object for all different numbers, otherwise you might get the same number:
Private Randomizer As New Random(Environment.TickCount)
Private EasyNum As Int32 = Randomizer.Next(0, 10000) '0 to 9999
Private HardNum As Int32 = Randomizer.Next(0, 100000) '0 to 99999
The "from" value of the randomizer's Next method is always inclusive, the "to" value exclusive, for whatever reasons.
To increment a variable, the syntax is
Tries += 1
instead of "Tries = +1"
To write a number with leading digits, use might use
Console.Out.WriteLine($"The correct solution would have been: {EasyNum:0000}")
or
EasyNum.ToString("0000")
I have a task to make a calculation of selling price and cost value.
I don't know have any idea to make a good coding for exception.
Private Sub ButtonCalculate_Click(sender As Object, e As EventArgs) Handles ButtonCalculate.Click
Dim SellingPrice As Double
Dim CostValue As Double
Dim Commission As Double
Dim CommissionRate As Double = 0.2
If Me.TextBoxSellingPrice.Text <> "" Or Me.TextBoxCostValue.Text <> "" Then
Try
SellingPrice = Double.Parse(TextBoxSellingPrice.Text)
CostValue = Double.Parse(TextBoxCostValue.Text)
'Calculation
Commission = CommissionRate * (SellingPrice - CostValue)
'Display
Me.TextBoxDisplay.Text = Me.TextBoxName.Text
Me.TextBoxCommission.Text = "RM" + Commission.ToString("F2")
Catch
MessageBox.Show("Wrong Input", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Else
MessageBox.Show("Please fill your data completely", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Output:
Figure 1
Using exception handling to check that data is correct isn't really the best practice. For a start, the performance of Try..Catch blocks is pretty poor compared to just using If..Then..Endif as there is a fair amount of stuff happening in the background with exceptions. In your case it wont make a major difference, but if the Try..Catch..End Try is in a loop, your program can take quite a performance hit.
There is little else to do other than to write some If..End If statements to validate your input data .. As other commenters had stated, using .TryParse is an exellent way to start things off ..
If Not Double.TryParse(TextBoxSellingPrice.Text, SellingPrice) Or Not Double.TryParse(TextBoxCostValue.Text, CostValue) Then
MessageBox.Show("Invalid input. Please enter positive numbers only")
Exit Sub
End If
If SellingPrice < CostValue Then
MessageBox.Show("Selling Price must be greater than cost")
Exit Sub
End If
If Not SellingPrice > 0 Or Not CostValue > 0 Then
MessageBox.Show("Selling price and cost must be >0")
Exit Sub
End If
'Calculation
Commission = CommissionRate * (SellingPrice - CostValue)
'Display
Me.TextBoxDisplay.Text = Me.TextBoxName.Text
Me.TextBoxCommission.Text = "RM" + Commission.ToString("F2")
You could also have a look at the ErrorProvider component
As (hopefully) all developers, I've developed over the years and am now at the point that OOP and functional programming is a daily costs. Recently I've bumped into and "old" (2 years) DLL of my that reads in a TCP stream, drops the incoming string (which end with a line feed) into a ConcurrentQueue(Of String). Then a second Backgroundworker loops over the ConcurrentQueue(Of String) and dequeue's the string and parses it.
Depending on the request command i send to the TCP server i gat get one line or several hunders of line. Also when nothing is asked the TCP server send's nothing. It is completely event driven.
With the new and efficient Technics of PLINQ, Async/Await and TPL i am determine to make this faster/more efficient. Currently i'm brainstorming on how to do this and am asking you to think with me or give some good advice.
So let's dig deeper into what i have now:
It al starts with two Backgroundworkers, One for Reading and One for Parsing.
The Reading Backgroundworker:
This backgroundworker job is to read the incomming data and make sure to keep reading until a Linefeed is passed. Then it cuts into a string and drops it into a ConcurrentQueue(Of String). The code looks like this:
Private Sub bgwReceive_DoWork(sender As Object, e As DoWorkEventArgs) Handles bgwTCP_Receive.DoWork
Dim strRXData As String = String.Empty
Dim intLfpos As Integer = 0
Try
Do Until bgwTCP_Receive.CancellationPending
'while the client is connected, continue to wait for and read data
While _Client.Connected AndAlso _ClientStream.CanRead
Dim buffer(_Client.ReceiveBufferSize - 1) As Byte
Dim read As Integer = _ClientStream.Read(buffer, 0, buffer.Length)
'Put the new data in the queue
If read > 0 Then
'Add incomming data to the buffer string
strRXData &= Text.Encoding.ASCII.GetString(buffer, 0, read)
'Replace "0 padding"
strRXData.Replace(Chr(0), String.Empty)
'Now check if we have a Linefeed in our buffer string
While strRXData <> String.Empty
'Get the position of the first linefeed
intLfpos = strRXData.IndexOf(vbLf)
'Now check if we find something.
If intLfpos < 0 Then Exit While
'There is a line feed, it is time to add it to the incomming data queue
IncommingData.Enqueue(strRXData.Substring(0, intLfpos - 1))
'Now remove the command from the buffer string
strRXData = strRXData.Substring(intLfpos + 1, strRXData.Length - intLfpos - 1)
End While
End If
'Check if we have to stop
If bgwTCP_Receive.CancellationPending Then e.Cancel = True : Exit Do
Threading.Thread.Sleep(1)
End While
Threading.Thread.Sleep(100)
Loop
e.Cancel = True
Catch op As OperationCanceledException
Throw New Exception("TCP Reading cancelled")
Catch se As SocketException
Throw New Exception("Socket unknown error. Native error code: " & se.NativeErrorCode)
Catch IOex As IOException
Throw New Exception("Socket timeout while receiving data from [" & TCP_ServerIP.ToString & "]")
End Try
End Sub
The Parsing Backgroundworker: This backgroundworker job is to parse the strings in the ConcurrentQueue(Of String). In this backgroundworker there is a big Select case that looks at the first word in the string. This determines what to do.
The received protocol is very simple but unfortunately it hasn't a sollid structure like JSON. A string would look like this: IND PARM1:"Hello world!" PARM2:1.4.8 \CrLf
As you can see it is very simple and plain. The IND indicate the Command, like VER is for VERSION string. IND is always 3 chars long. Then comes the parameters. The parameter name is always 4 chars long. If it is between double quotes it is a string, else its something like a double/integer/IP address (X.X.X.X). Keep in mind that original it is a string. In my parser i parse it to the object properties. The code looks like this:
Private Sub bgwProcessQueue_DoWork(sender As Object, e As DoWorkEventArgs) Handles bgwProcessQueue.DoWork
Dim strNewLine As String = String.Empty
Dim strSubLine As String = String.Empty
Try
'Loop until program has stopped
Do Until bgwProcessQueue.CancellationPending
'Only process when something is in the queue
If TCP_Connection.IncommingData.TryDequeue(strNewLine) Then
'If Backgroundworker has to cancel than cancel
If bgwProcessQueue.CancellationPending Then Exit Do
Select Case True
Case strNewLine.StartsWith("VER") 'Example: VER PRM1:1.1 PRM2:"Hi there" PRM3:1.1.1 PRM4:8/0 PRM5:8
'First check if all the needed values are there, if not resend the command
If strNewLine.Contains("PRM1:") AndAlso strNewLine.Contains("PRM2:") AndAlso strNewLine.Contains("PRM3:") AndAlso strNewLine.Contains("PRM4:") AndAlso strNewLine.Contains("PRM5:") Then
'Get versions and devicename
Me.Param1 = GetSubstring(strNewLine, "PRM1:", " ")
Me.Param2 = GetSubstring(strNewLine, "PRM2:", " ")
Me.Param3 = GetSubstring(strNewLine, "PRM3:", " ")
Me.Param4 = GetSubstring(strNewLine, "PRM4:", " ")
Me.Param5 = GetSubstring(strNewLine, "PRM5:", " ")
Else
'Commando was invalid, resend the command
Log.Message(LogLevel.Warning, "VER Messages was incompleet, resending VER command")
SendData("VER")
End If
Case strNewLine.StartsWith("ERROR")
Log.Message(LogLevel.Warning, strNewLine)
End Select
End If
'Clear any old data
strNewLine = String.Empty
'Sleep
Threading.Thread.Sleep(1)
Loop
e.Cancel = True
Catch ex As Exception
Log.Exception(ex)
End Try
End Sub
Private Function GetSubstring(text As String, StartValue As String, EndValue As String) As String
Try
'If we can't find the Start value we can't do anything
If Not text.Contains(StartValue) Then Return String.Empty
'Find the index of the Start and End value
intStartValueIndex = text.IndexOf(StartValue) + StartValue.Length
intEndValueIndex = text.IndexOf(EndValue, intStartValueIndex)
'If no Endvalue index was found then get the end of the string
If intEndValueIndex < intStartValueIndex Then intEndValueIndex = text.Length
'Return the substring en remove quetes
Return text.Substring(intStartValueIndex, intEndValueIndex - intStartValueIndex).Replace("""", "")
Catch ex As Exception
Log.Exception(ex)
Return String.Empty
End Try
End Function
Hopefully all above is understandable. I was thinking of looping through the ConcurrentQueue(Of String) with a Parrallel.ForEach Wich passes the Dequeued string into a sub that does the parsing. At the reading side, my inspiration is empty at the moment...