help VB

Search This thread

fujirio

Senior Member
Feb 10, 2011
190
23
can some one tell me whats wrong with this code

Code:
Private Sub btnDisplay_Click( . . . )  Handles btnDisplay. Click
‘Toggle switch from on to off and from off to on.
Dim switchOn As Boolean
switchOn = CBool( InputBox( " Enter True or False. " ,  " The switch is on. " ) )
If switchOn Then
switchOn = False
End If
If Not switchOn Then
switchOn = True
End If
txtOutput. Text = CStr( switchOn)
End Sub

and also

Code:
Private Sub btnDisplay_Click( . . . )  Handles btnDisplay. Click
‘Display twice the length of a word.
Dim word As String
word = InputBox( " Enter your favorite word " ) 
txtOutput.Text = “When the word is written twice, “ &
                             Twice (word) & “  letters are used.” 
End Sub
Function Twice (w As String) As Integer
‘Compute twice the length of a string.
Dim len As Integer
               Return len = 2 * w.Length
End Function
 

pencil42

Senior Member
Feb 20, 2014
67
2
Kraków
Is that java? I haven't programmed in a while, if that's java then you need ; at the end of line

Edit: oh that's vb, disregard my post

Sent from my Xperia L using Tapatalk
 
Last edited:

Archer

Inactive Recognized Developer / Retired Senior Mod
Jul 9, 2008
14,002
4,003
Manchester
Google Pixel 8 Pro
can some one tell me whats wrong with this code

The first one is wrong where you do this...

Code:
If switchOn Then
switchOn = False
End If
If Not switchOn Then
switchOn = True
End If

You need an else or you're setting it to false and then saying "if it's false make it true" immediately after....

Code:
If switchOn Then
switchOn = False
Else
switchOn = True
End If

Also, I'd recommend looking at using MessageBox as you can show a message and specify what buttons to have - MUCH better than asking someone to type true or false (and then have to worry about people typing other things, or spelling mistakes or upper/lower case characters).

The second one is wrong in the Twice function. You are returning the value of setting the variable len, not the actual value of it. It's easier to get rid of the variable in this case as it serves no purpose...

Code:
Function Twice (w As String) As Integer
‘Compute twice the length of a string.
               Return 2 * w.Length
End Function