Visual Basic programming - vb.net

I am new to the programing world and I am stuck on the below problem please could you assist me
Write a Visual Basic.net function to calculate the sum of all the numbers in an input field. For example, if the input string is: "ICT2611", then the numbers included in this string are: 2, 6, 1, 1 and their sum is therefore, 2+6+1+1 = 10

The below should solve your problem, it uses Regex to find any matches in the provided string to the expression (numbers 1 - 9), and then iterates through them tallying them up as it goes.
Public Function SumOfString(str As String) As Integer
Dim total As Integer = 0
For Each i As Match In Regex.Matches(str, "[1-9]")
total += i.Value
Next
Return total
End Function
Alternatively the same thing could be achieved like this, this just iterates through each character in the string and then checks to see if it's a digit. If it is a digit then it'll tally it up.
Public Function SumOfString(str As String) As Integer
Dim total As Integer = 0
For Each i As Char In str
If Char.IsDigit(i) Then total += Integer.Parse(i)
Next
Return total
End Function

Related

How do I check if ticket number is formatted using a function?

I would like my function to follow some convention to check if my ticket number needs formatting or not.
If the convention is not met, then I would like to make some changes to the ticket number.
Ticket number 19K3072216 needs to be formatted to this 19-K3-07-002216 because it does not meet the following conditions.
My function should do the following.
Check if the 1st 2 digits has a value 0 - 9 (numeric)
Check if the 3rd digit has a value of A to Z
Check if the 4th digit has a value 0 - 9 (numeric)
Check if the 5th and 6th digits has a date value (e.g.2 digit year - 17, 90, 15 etc)
Check if the next 6 digits i.e. 7th - 12th digits are numeric.
Because ticket number 19K3072216 does not meet the above conditions, I would like my function to format it to look like this 19-K3-07-002216
The string strTicketNumber should return formatted ticket number 19-K3-07-002216
My vb.net function
Public Class Ticket_Code
Public Shared Sub main()
Dim strTicketNumber As String = FixTicketNumber("19K3072216")
End Sub
Public Shared Function FixCaseNumber(ByVal astrCaseNumber As String) As String
Dim strCaseNumber As String = Replace(astrCaseNumber, "-", "")
'Determine if ticket number is formatted
How do I do this?
'If ticket number is formatted add 2 zeros
'How do I do this?
'Else return unchanged
'If ticket number is already formatted, just returned the number (original number)
Return strCaseNumber
End Function
End Class
It will really depend on your input and how different from the example it can be.
For instance will invalid input always be in the same format 19K3072216or is there the chance it will be all digits, all letters, less/more than 10 characters long etc. All of these rules need to be considered and handled as necessary.
If the input will be from a user, never trust it and always assume it is the furthest from valid as possible. If the app can handle that case it can handle everything else
Something like this should get you started:
Public Sub Main()
Dim strTicketNumber As String = FixTicketNumber("19-K3-07-002216") ' or 19K3072216
Console.WriteLine(strTicketNumber)
Console.ReadKey()
End Sub
Private Function FixTicketNumber(p1 As String) As String
Dim fixed As String = ''
Dim valid As Boolean = checkTicketNumber(p1)
If valid Then
Return p1 ' Ticket number is valid, no transformation needed
Else
'Assume invalid input will always be 10 characters (e.g. 19K3072216)
'Split the input and Step through each rule one at a time
'returning the necessary result/format string as you go
'#1 Check if the 1st 2 digits has a value 0 - 9 (numeric)
Dim ruleOne As String = p1.Substring(0, 2)
'perform isNumeric, concatenate to fixed if everything is ok
'fixed += ruleOne+"-"
'#2 Check if the 3rd digit has a value of A to Z
Dim ruleTwo As String = p1.Substring(3, 1)
'check if its a letter, concatenate to fixed if everything is ok
'... same for all the rules
End If
End Function
Private Function checkTicketNumber(p1 As String) As Boolean
'See if the input matches the rules
'Check if the 1st 2 digits has a value 0 - 9 (numeric)
'Check if the 3rd digit has a value of A to Z
'Check if the 4th digit has a value 0 - 9 (numeric)
'Check if the 5th and 6th digits has a date value (e.g.2 digit year - 17, 90, 15 etc)
'Check if the next 6 digits i.e. 7th - 12th digits are numeric.
Dim pattern As String = "\d{2}-[A-Z]\d-\d{2}-\d{6}"
Dim match As Match = Regex.Match(p1, pattern)
Return match.Success
End Function
It is hard to produce a fully working solution as there are too many unknowns about the input as an outsider.

Get the nth character, string, or number after delimiter in Visual Basic

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.

VBA change string with decimal to number with decimal

I have a field in an access database as TEXT. the text has decimals such as 100.00000. need to run calcs on these with the decimals present. Ive been working with the SQL statement and CAST functions and ive had no luck. All I get is whole numbers with no decimals. I've resorted to VBA and I am still getting the same results. every result is whole numbers. I would prefer 5 (FIVE) decimal places.
such as 100.00000 and not "100".
I've tried the split function in VBA and re assembling the 2 strings into a number with the decimal present and still no luck.
The code below pulls in whole numbers only - I need decimal places!! thank you
Function numChange(input1 As String) As Integer
Dim output As Integer
If input1 = "" Then
input1 = 0
End If
output = Format(input1, Number)
numChange = output
End Function
after decimal if no zero will mean a thing, but if you have 100.00001 this will return the same.
Modified:
Function numChange(input1 As String) As Double
Dim output As Double
If input1 = "" Then
input1 = 0
End If
output = Format(input1, "0.00000")
numChange = output
End Function

how i can figuring the highest number in my array...visual basic

how i can figure the highest number in my array...below is the code...can someone help me to solve my problems...n i wan to show the result in the label from the other windows form....thank u... :
Public Class Frm2
Public Parties(9) As String
Public Votes(9) As String
Dim vote As Integer
Dim Party As String
Party = TParty.Text
vote = TVote.Text
For I As Integer = 0 To Parties.Length - 1
If Parties(I) = "" Then
Parties(I) = TParty.Text()
For J As Integer = 0 To Votes.Length - 1
If Votes(J) = "" Then
Votes(J) = TVote.Text()
MsgBox(TParty.Text & TVote.Text & " votes")
TParty.Clear()
TVote.Clear()
Exit Sub
End If
Next J
End If
Next
MsgBox("you can vote now")
If you want to use an algorithm to find the highest number into an array (let's say Votes), the classic is coming from the so-called Bubble Sort:
Dim max As Long 'change the type accordingly, for example if votes are 1-10 then Integer is better
max = Votes(0) 'set the first vote as the max
For j = 1 To Votes.Length - 1
If Votes(j) >= max Then max = Votes(j) 'if another element is larger, then it is the max
Next j
Now the variable max stores the highest value of the array Votes, that you can show anywhere as, for example, in MyForm.MyLabel.Text = max. More useful info here.
Please note that now you declare Public Votes(9) As String, which means they are strings so not usable as numbers. You might want to declare them with a different data type, or use the CInt() method to convert strings in integers as suggested by ja72.
I thought this would only work with a Variant array, but in quick testing it seems to work with an array of Longs as well:
Dim Votes(9) as Long
Dim Max As Long
Max=WorksheetFunction.Max(Votes)
Note that, as Matteo says, you should change Votes() to an array of numeric types. I'd use Long, as it's a native VBA type.
EDIT: As noted by Dee, the code in this question is actually VB.Net. I added that as a tag. In VBA the solution would be even simpler, as Max is an array property:
Max=Votes.Max
(I suppose it would be a good idea to change the variable name from "Max".)

Function to order numbers; find common difference and gap

I have a vb6 function that will take up to 7 numbers, order them, find a common difference. There will be a gap in this sequence of numbers. I also want to identify the missing number.
Example input is 19,17,20,and 16. The output should be an array 16,17,18,19,20 in this order. Any help? I may be able to interpret vb.net code to vb6, but vb6 is preferred to me.
Here’s an outline of what needs to be done:
Find the smallest and largest number
Create an array with enough entries to hold the full range from the smallest to largest
Fill the array with the numbers from lowest to highest.
Note that you don’t need to sort the numbers at all. You just need to find the extreme values.
Here’s a code outline in VB6:
Function RangeFrom(ParamArray Numbers() As Long) As Long()
Dim Lowest As Long
Dim Highest As Long
Lowest = Numbers(0)
Highest = Numbers(0)
Dim Number As Long
For Each Number In Numbers
If Number < Lowest Then Lowest = Number
If Number > Highest Then Highest = Number
Next
Dim Result(0 To Highest - Lowest) As Long
Dim I As Long
For I = 0 To Highest - Lowest
Result(I) = Lowest + I
Next
FromRange = Result
End Function
Since you have tagged VB.NET as well, this should be convertible to VB6(i've avoided Linq):
Public Shared Function FillGaps(input As Int32()) As Int32()
Dim output = New List(Of Int32)
Array.Sort(input)
' now we'll find the min/max-values at the first/last indices
For i As Int32 = input(0) To input(input.Length - 1)
output.Add(i)
Next
Return output.ToArray()
End Function
Use it in the following way :
Dim intArray = {19, 17, 20, 16}
intArray = FillGaps(intArray)
Note that this approach skips duplicates.