Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

help VB

OP fujirio

12th March 2014, 08:37 AM   |  #1  
OP Senior Member
Thanks Meter: 23
 
179 posts
Join Date:Joined: Feb 2011
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
12th March 2014, 09:04 AM   |  #2  
pencil42's Avatar
Member
Flag Kraków
Thanks Meter: 2
 
67 posts
Join Date:Joined: Feb 2014
Donate to Me
More
[s]Is that java? I haven't programmed in a while, if that's java then you need ; at the end of line [/s]

Edit: oh that's vb, disregard my post

Sent from my Xperia L using Tapatalk
Last edited by pencil42; 12th March 2014 at 10:08 AM.
12th March 2014, 12:39 PM   |  #3  
Archer's Avatar
Senior Moderator / Recognized Developer - (Un)Recognized Pole Dancer
Flag Northampton
Thanks Meter: 4,117
 
10
13,913 posts
Join Date:Joined: Jul 2008
More
Quote:
Originally Posted by fujirio

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
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes