Write a function that accepts a product code as an input string in the form XXX-
ID: 672900 • Letter: W
Question
Write a function that accepts a product code as an input string in the form XXX-YYY and validates it as follows: (1) The string length should be exactly 7 characters. (2) The first three characters (XXX) can be anything. (3) The fourth character should be a dash. (4) The last three characters (YYY) should be numeric digits. The output from this subroutine should be a Boolean value that is set to “True” if the product code is valid and “False” if the product code is not valid. VB Question.
Sub MySub(ByVal W As Integer, ByRef M As Integer)
M = W / 2
End Sub
Private Sub btn1_Click(…) Handles btn1.Click
Dim X As Integer = 12, Y As Integer = 3
Call MySub(X, Y)
lstOutput.Items.Add("The value of Y is " & Y)
End Sub
The value of Y is 6
Explanation / Answer
Public Function Validate(str As String)
If str.Length <> 7 Then
Return False
End If
If str(3) <> "-" Then
Return False
End If
For i As Integer = 4 To str.Length - 1
If str(i) > "9" Or str(i) < "0" Then
Return False
End If
Next
Return True
End Function
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.