How can I add an Integer to a string and get a string answer? - vb.net

I am using Visual Basic 2010.
I have tried several ways to try and add an integer to a string and then get the results as a string. Nothing I have tried seems to work. Below is the code I have at present time. Result is a label that I need to show the results after adding a 1 to the label result to get a new total. Can anyone help? Thanks!
Result.Text = CStr(CInt(Result.Text + 1))

You can not add numbers with a string. You need to first convert the string into an integer. You can do this like this:
Dim resultInteger = CInt(Result.Text)
You can then add resultInteger to 1 and set Result.text to the answer like this:
Result.Text = (resultInteger + 1).ToString
It, however, is possible to do this all on one line:
Result.Text = (CInt(Result.Text) + 1).ToString
Note that if Result.Text does not contain a number then it will crash you application. You could use a try statement like this:
Try
Result.Text = (CInt(Result.Text) + 1).ToString
Catch ex As Exception
msgbox("Result does not contain an integer!")
End Try

Result.Text = CStr(CInt(Result.Text) + 1)

Dim intResult As Integer = 0
If Integer.TryParse(Result.Text, intResult) Then
intResult += 1
Result.Text = intResult.ToString()
End If

Related

How to subtract from a number with a variable amount of other numbers?

Sorry for the edits, im tired and im a moron, so heres my problem=
Lets say this is my code :
Dim ogquantity,subtractedquantity as double
Private Sub CalculateMe
ogquantity = numericupdown1.value
subtractedquantity = ogquantity - numericupdown2.value
subtractedquantity = ogquantity - numericupdown3.value
subtractedquantity = ogquantity - numericupdown4.value
subtractedquantity = ogquantity - numericupdown5.value
label1.text = subtractedquantity
End Sub
And i set "CalculateMe" as code for 1-5 numericupdowns_valuechanged
This wont output a changed value..
And the reason i have it set up like this is because in the actual code numericupdown might change subtractedquantity1 or subtractedquantity28, it is decided with a dropdown menu for each numericupdown.
something like this:
Dim item1stock, item1originalstock As Double
If itemselector1name.Text = itemdefiner1name.text Then
item1stock = item1originalstock - itemselector1quantity.value
itemselector1label.text = item1stock
End If
If itemselector2name.Text = itemdefiner1name.text Then
item1stock = item1originalstock - itemselector2quantity.value
itemselector2label.text = item1stock
End If
and i would repeat check for every item like this but if two different 'itemselectorquantity' is supposed to subtract from the same original stock quantity that i dont know how to set up.. Im trying my best here but im new to coding and bad at explanation. Thank you!
Your question is rather muddled but, as an example of the sort of thing you might do if you want to use zero, one or more of a set of controls based on another set of controls, consider this example that uses CheckBoxes to specify which NumericUpDowns to subtract from an initial quantity:
Private Function SubtractCheckedValues(value As Decimal) As Decimal
Dim checkBoxes = {CheckBox1, CheckBox2, CheckBox3, CheckBox4}
Dim numericUpDowns = {NumericUpDown1, NumericUpDown2, NumericUpDown3, NumericUpDown4}
For i = 0 To checkBoxes.GetUpperBound(0)
If checkBoxes(i).Checked Then
value -= numericUpDowns(i).Value
End If
Next
Return value
End Function

Multiple string replace at one time

I have this string abcd, and I want to replace the a with [a|b] and the b with [c|d]
I try many ways to do it, like
Dim varString As String = "abcd"
varString = varString.Replace("a", "[a|b]")
varString = varString.Replace("b", "[c|d]")
The result I get is
[a|[c|d]][c|d]cd
Instead I want it like this
[a|b][c|d]cd
The problem is every time I use the replace function it backs to change the values I already replaced before so I replaced a with [a|b] but then when I do my second command to replace the b it changes the b in [a|b] that I just changed and I don't want this.
I tried to use StringBuilder but it gives the same result.
Please advise me,
I solved the problem by making an array in this way
Dim NewCommand As String = "abcd"
For i = 0 To LikeCommand.Length - 1
If LikeCommand(i) = "a" Then
NewCommand += "[a|b]"
ElseIf LikeCommand(i) = "b" Then
NewCommand += "[c|d]"
Else
NewCommand += LikeCommand(i)
End If
Next
LikeCommand = NewCommand
Or just switch the logic up. But obviously I'm thinking you're using a basic example for a more complex question.
dim varString as string = "abcd"
varString = varString.Replace("b" ,"[c|d]")
varString = varString.Replace("a" ,"[a|b]")
That would get you the desired results.

Working with 1-Result-Linq with For Each? Any alternative?

If I got a Linq-Query where I am knowing that it'll return 1 result only, like this:
Dim result = From g In Foo.Bar
Where g.keyID = 1
Select g
...do I still have to use the For Each loop to get the values or is there anything else I could use to work with 1-result-queries?
For Each x In result
TextBox1.Text = x.field1
TextBox2.Text = x.field2
TextBox3.Text = x.field3
Next
You can use Enumerable.First/ Enumerable.FirstOrDefault or Enumerable.Single/ Enumerable.SingleOrDefault(if it was exceptional if there were more than one).
Dim firstResult = result.FirstOrDefault()
If firstResult IsNot Nothing Then
TextBox1.Text = firstResult.field1
TextBox2.Text = firstResult.field2
TextBox3.Text = firstResult.field3
End If
So use First if it's possible that there are more than one but you want the first
Use Single if you want the first but it was a bug if there were more than one
The methods without OrDefault in the name will throw an exception if none was found
The OrDefault methods will return the default value(Nothing for reference types).

Longest Common substring breaking issue

Hi I have a function that finds the longest common substring between two strings. It works great except it seems to break when it reaches any single quote mark: '
This causes it to not truly find the longest substring sometimes.
Could anyone help me adjust this function so it includes single quotes in the substring? I know it needs to be escaped someplace I'm just not sure where.
Example:
String 1: Hi there this is jeff's dog.
String 2: Hi there this is jeff's dog.
After running the function the longest common substring would be:
Hi there this is jeff
Edit: seems to also happen with "-" as well.
It will not count anything after the single quote as part of the substring.
Here's is the function:
Public Shared Function LongestCommonSubstring(str1 As String, str2 As String, ByRef subStr As String)
Try
subStr = String.Empty
If String.IsNullOrEmpty(str1) OrElse String.IsNullOrEmpty(str2) Then
Return 0
End If
Dim num As Integer(,) = New Integer(str1.Length - 1, str2.Length - 1) {}
Dim maxlen As Integer = 0
Dim lastSubsBegin As Integer = 0
Dim subStrBuilder As New StringBuilder()
For i As Integer = 0 To str1.Length - 1
For j As Integer = 0 To str2.Length - 1
If str1(i) <> str2(j) Then
num(i, j) = 0
Else
If (i = 0) OrElse (j = 0) Then
num(i, j) = 1
Else
num(i, j) = 1 + num(i - 1, j - 1)
End If
If num(i, j) > maxlen Then
maxlen = num(i, j)
Dim thisSubsBegin As Integer = i - num(i, j) + 1
If lastSubsBegin = thisSubsBegin Then
subStrBuilder.Append(str1(i))
Else
lastSubsBegin = thisSubsBegin
subStrBuilder.Length = 0
subStrBuilder.Append(str1.Substring(lastSubsBegin, (i + 1) - lastSubsBegin))
End If
End If
End If
Next
Next
subStr = subStrBuilder.ToString()
Return subStr
Catch e As Exception
Return ""
End Try
End Function
I tried it with dotnetfiddle and there it is working with your Code you posted. Please activate your warnings in your project. You have function with no return value and you return an integer or a string. This is not correct. How are you calling your function?
Here is my example I tested for you:
https://dotnetfiddle.net/mVBDQp
Your code works perfectly like Regex! As far as I can see, there is really nothing wrong with your code.
Here I even tested it under more severe case:
Public Sub Main()
Dim a As String = ""
Dim str1 As String = "Hi there this is jeff''s dog.-do you recognize this?? This__)=+ is m((a-#-&&*-ry$##! <>Hi:;? the[]{}re this|\ is jeff''s dog." 'Try to trick the logic!
Dim str2 As String = "Hi there this is jeff''s dog. ^^^^This__)=+ is m((a-#-&&*-ry$##! <>Hi:;? the[]{}re this|\ is jeff''s dog."
LongestCommonSubstring(str1, str2, a)
Console.WriteLine(a)
Console.ReadKey()
End Sub
Note that I put '-$#^_)=+&|\{}[]?!;:.<> all there. Plus I tried to trick your code by giving early result.
But the result is excellent!
You could probably put more actual samples on the inputs which give you problems. Else, you could possibly describe the environment that you use/deploy your code into. Maybe the problem lies elsewhere and not in the code.
The quickest way to solve this would be to use an escape code and replace all the ' with whatever escape code you use

Add Text Into Textbox's in Visual Basic

Ok, so im trying to make this program and i need this:
"Melternet Hello Melternet#gmail.com 5/7/2013" to be in different textbox's like this
"Textbox1 = Melternet"
"Textbox2 = Hello"
"Textbox3 = Melternet#gmail.com"
"Textbox4 = 5/7/2013"
So pretty much every space is a cut off line to add that text to a textbox and then it does the rest like the first one
How would i do something like that, thanks in advance.
Please Answer Back If Anyone Can Figure This Out Or Help Me, NEED THIS QUICK...
BTW: i'm using Visual Basic 2008.
Without more info...something like:
Dim data As String = "Melternet Hello Melternet#gmail.com 5/7/2013"
Dim values() As String = data.Split(" ")
If values.Length >= 4 Then
TextBox1.Text = values(0)
TextBox2.Text = values(1)
TextBox3.Text = values(2)
TextBox4.Text = values(3)
End If
It looks like you are simply splitting the entire input text string: "Melternet Hello Melternet#gmail.com 5/7/2013" wherever a space occurs, yes(?)
If your string has a variable number of words, then fill up textboxes programmatically, like, e.g.:
Dim mystr as String
mystr = "hello world I want to paste this to multiple textboxes"
Dim Buff() as String
Buff = Split(mystr," ")
For i As Integer = 0 to UBound(Buff)
Dim tb As New TextBox
str = Buff(i)
tb.Name = str
tb.Text = str
tb.Left = 50
tb.Top = 50 + 25 * i
tb.Width = 50
tb.TextAlign = HorizontalAlignment.Right
Me.Controls.Add(tb)
Next
In the above fashion, no matter what the value of mystr, or how many words are in the string (mystr), you can write them all to new textboxes that are created/placed dynamically on Form1, i.e. "Me".