Question Combine If And Also Statements

snitchdnb21

Member
Joined
Mar 21, 2013
Messages
5
Programming Experience
Beginner
Hi, Im pretty new to VB so excuse my inexperience with it :) as this may/is be a very silly question.

Im trying to write a little program similar to a sort of encryptor which converts characters in the password string to integers of a set value etc..

I want; If "a" is in string and "b" then msgbox the stored variables a + b and so forth for how many characters are in the string (3 "a" = a x 3) etc.. hope this makes sense!

Here's my code;

VB.NET:
        If textbox2.Text.Contains("a") Then

            MsgBox(a)


        ElseIf textbox2.Text.Contains("b") Then


            MsgBox(b)


        ElseIf textbox2.Text.Contains("a") AndAlso textbox2.Text.Contains("b") Then


            MsgBox(a + b)


        End If

Seems really simple and I can't seem to get it? They just cancel each other out?

Any help would be great.

Thanks!
 
Last edited:
If I am to understand what you are doing, you are giving each letter an interger value then summing them up?

There are a number of approaches you can take. I would stay away from using If...Then statements because it would make your code huge.

You could put the letters in an array and use the index as their "value". You could also just put all of the letters in a string and access them by character index. Below is an example of using a string and character index along with a Xor function for a simple hash. By changing the order of the characters in the string, you change their value. You can access their index or "value" by doing a string.indeof(char).

Private Const CODE AsString = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

Private Function bitXor(ByVal A AsChar, ByVal B AsChar) AsChar
'Find the Xor product of A and B then returns the CODE character at that index
Dim codeIndex AsInteger = (Asc(A) Xor Asc(B)) Mod CODE.Length
Return CODE.Chars(codeIndex)
End Function'bitXor(ByVal A As Char, ByVal B As Char) As Char
 
If I am to understand what you are doing, you are giving each letter an interger value then summing them up?

There are a number of approaches you can take. I would stay away from using If...Then statements because it would make your code huge.

You could put the letters in an array and use the index as their "value". You could also just put all of the letters in a string and access them by character index. Below is an example of using a string and character index along with a Xor function for a simple hash. By changing the order of the characters in the string, you change their value. You can access their index or "value" by doing a string.indeof(char).

Private Const CODE AsString = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

Private Function bitXor(ByVal A AsChar, ByVal B AsChar) AsChar
'Find the Xor product of A and B then returns the CODE character at that index
Dim codeIndex AsInteger = (Asc(A) Xor Asc(B)) Mod CODE.Length
Return CODE.Chars(codeIndex)
End Function'bitXor(ByVal A As Char, ByVal B As Char) As Char

That's correct, Thank you for the reply.

Im not familiar with Xor or many of these function but will look into them!

Thank you for your help!
 
That's correct, Thank you for the reply.

Im not familiar with Xor or many of these function but will look into them!

Thank you for your help!

That was meant to be an example of using a string and its index to look up characters. You don't need to use Xor or other things in that code.
 
The sequnce of conditions is important.

The problem with your If block is that the conditions you are evaluating are in the wrong order. If the first condition is True, then it will execute those instructions and not bother to evaluate the rest of the conditions, even if they also happen to be True. In your case, you want to test whether both "a" and "b" are included, but you put that down last. So if either one is True, it will execute that first and continue to the instruction after the End If. You need to rearrange your block as follows:

VB.NET:
     If textbox2.Text.Contains("a") AndAlso textbox2.Text.Contains("b") Then
            MsgBox(a + b)
        ElseIf textbox2.Text.Contains("a") Then
            MsgBox(a)
        ElseIf textbox2.Text.Contains("b")  Then
            MsgBox(b)
        End If
 
Thank you, I see that now :biggrin:

Im trying to create a moving button that once the button is clicked the timer slides the button to the new location.

Its current location on the x axis is 118 and i want to slide to x = 36.

Any idea?


VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Timer1.Enabled = True


    End Sub


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick




        Dim move As Integer


        move -= 1


        If Me.Button1.Location = New Point(Me.Button1.Location.X) > 36 Then


            Me.Button1.Location = New Point(move)


        ElseIf Me.Button1.Location = New Point(Me.Button1.Location.X) = 36 Then


            Timer1.Enabled = False


        End If
    End Sub

Again any help would greatly thankful!
 
Back
Top