VB.NET Code Snippets: Number, String, and Palindrome Checks

VB.NET Code Snippets

Here are several VB.NET code snippets demonstrating various functionalities:


4) Parity Check Function

    
Private Function paroimpar(ByVal x As Integer) As Integer
    Dim y As Integer

    y = Val(TextBox1.Text)

    If y Mod 2 = 0 Then
        If y = 0 Then
            MsgBox("es NeUtrO")
            Exit Function
        End If
        MsgBox("es par")
    Else
        If y = 0 Then
            MsgBox("es NeUtrO")
        End If
        MsgBox("es impar")
    End If
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    paroimpar(Val(TextBox1.Text))
End Sub
    
  

This function checks if a number from TextBox1 is even or odd.


5) String Reversal Function

    
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Label1.Text = reverse(TextBox1.Text)
End Sub

Private Function reverse(ByVal x As String) As String
    x = StrReverse(TextBox1.Text)
    Return x
End Function
    
  

This function reverses the string entered in TextBox1 and displays it in Label1.


6) Substring Count Function

    
Private Function lol(ByVal a As String, ByVal b As String) As String
    Dim len, C As Integer
    a = TextBox1.Text
    b = TextBox2.Text

    For len = 1 To a.Length
        If Mid(TextBox1.Text, len, b.Length) = b Then
            C = C + 1
        End If
    Next len
    Return C
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Label1.Text = lol(TextBox1.Text, TextBox2.Text)
End Sub
    
  

This function counts how many times the string from TextBox2 appears as a substring within the string from TextBox1.


7) Palindrome Check Function

    
Private Function pali(ByVal a As String) As String
    Dim s As String
    Dim len, c As Integer

    s = Replace(TextBox1.Text, " ", "")
    Label2.Text = StrReverse(s)
    For len = 1 To s.Length
        If Mid(TextBox1.Text, len, 1) = " " Then
            c = c + 1
        End If
    Next

    If s = Label2.Text Then
        MsgBox("Es palindroma")
    Else
        MsgBox("No es palindroma")
    End If
    Return s
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Label1.Text = pali(TextBox1.Text)
End Sub
    
  

This function checks if the string from TextBox1 is a palindrome (reads the same forwards and backward), ignoring spaces.


8) Prime Number Check and List Function

    
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Label1.Text = pc(Val(TextBox1.Text))
    Label2.Text = ps(Val(TextBox1.Text), Val(TextBox2.Text))
End Sub

Private Function pc(ByVal x As Integer) As Boolean
    Dim primo As Boolean = True
    Dim i As Integer

    For i = 2 To x - 1
        If x Mod i = 0 Then
            primo = False
        End If
    Next i
    Return primo
End Function

Private Function ps(ByVal desde As Integer, ByVal hasta As Integer) As String
    Dim con As String
    Dim x As Integer
    con = ""
    For x = desde To hasta
        If pc(x) Then
            con += " - " + Str(x)
        End If
    Next
    Return con
End Function
    
  

This function checks if a number from TextBox1 is prime and lists prime numbers between the values from TextBox1 and TextBox2.