Author Topic: Code explanation  (Read 4881 times)

0 Members and 1 Guest are viewing this topic.

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Code explanation
« on: December 02, 2012, 04:37:17 pm »
Ok, maybe it will sound wierd to ask this since I wrote the code down, but, I don't understand how it work. I am coding a testing application (for learning only) and the application ask several question one user, he asnwer in textbox1 by clicking one button. There is listbox1 that store those informations. Here is the image of application and the code:
Code: (vb) [Select]
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x As String = Microsoft.VisualBasic.Left(Label1.Text, 2)
        Dim username As String = TextBox1.Text
        Dim age As String = TextBox1.Text
        Dim job As String = TextBox1.Text
        If x = "1." Then
            ListBox1.Items.Add("Username: " & username)
            Label1.Text = "2. How old are you?"
        ElseIf x = "2." Then
            ListBox1.Items.Add("Age: " & age)
            Label1.Text = "3. Do you have job?"
        ElseIf x = "3." Then
            ListBox1.Items.Add("Job: " & job)
            Label1.Text = "Great job."
        End If
    End Sub
End Class

As you can see, the code will work ok, I know that allready. But, If I just don't change the label1.text into another number, the listbox1 will store same value again. Or , if you don't understand what I wanted to say, can someone give me more simple example of application I did here. So I can learn better way to do programming. Thanks.
« Last Edit: December 02, 2012, 07:46:00 pm by Code.Illusionist »
Vae Victis - suffering to the conquered

Offline ande

  • Owner
  • Titan
  • *
  • Posts: 2664
  • Cookies: 256
    • View Profile
Re: Code explanation
« Reply #1 on: December 02, 2012, 07:08:31 pm »
Images dosent work. Use some other uploading service.
If you have code, paste it into code tags here, dont take images of it.
if($statement) { unless(!$statement) { // Very sure } }
https://evilzone.org/?hack=true

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Re: Code explanation
« Reply #2 on: December 02, 2012, 07:47:00 pm »
Ok, post edited.
Vae Victis - suffering to the conquered

Offline ande

  • Owner
  • Titan
  • *
  • Posts: 2664
  • Cookies: 256
    • View Profile
Re: Code explanation
« Reply #3 on: December 02, 2012, 08:05:30 pm »
Okay, I see the problem or rather the.. Way you did it.

Basically you want to use the same button and textbox for various questions. And you use the label1's first two characters to determine what level/question you are on.

Now, you wanted an alternative way of doing this. Do it internally and skip the label all together. Do something like this:

Code: (vb) [Select]
Public Class Form1

       Dim qLevel as integer = 0
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim answer As String = TextBox1.Text

        If qLevel = 1 Then
            ListBox1.Items.Add("Username: " & answer)
            Label1.Text = "How old are you?"
            qLevel += 1
        ElseIf qLevel = 2 Then
            ListBox1.Items.Add("Age: " & answer)
            Label1.Text = "Do you have job?"
            qLevel += 1
        ElseIf qLevel = 3 Then
            ListBox1.Items.Add("Job: " & answer)
            Label1.Text = "Great job."
            qLevel += 1
        Else
            ' Msg box here? End of code?
        End If
    End Sub

End Class

I also changed the way you get your answers, you dont need one variable for each of the answers when the input is the same. Simply use one variable/input, i called it answer. You might want to look into switch() as well, it works very well in these cases.
if($statement) { unless(!$statement) { // Very sure } }
https://evilzone.org/?hack=true

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Re: Code explanation
« Reply #4 on: December 02, 2012, 08:11:41 pm »
Finaly I understand this. Thank you so much Ande. Yea, the idea to use several variables for same input is, I admit, stupid. Good you told me where i make errors (not like it wont work, just too much code for one simple thing). Thanks again.


EDIT: Just to remind you Ande that the starting point of qLevel should be 0, not 1, if it's 1, it can't start.
« Last Edit: December 02, 2012, 08:35:25 pm by Code.Illusionist »
Vae Victis - suffering to the conquered

Offline ande

  • Owner
  • Titan
  • *
  • Posts: 2664
  • Cookies: 256
    • View Profile
Re: Code explanation
« Reply #5 on: December 02, 2012, 10:18:33 pm »
EDIT: Just to remind you Ande that the starting point of qLevel should be 0, not 1, if it's 1, it can't start.

Hehe yeah, you are entirely right ;)
if($statement) { unless(!$statement) { // Very sure } }
https://evilzone.org/?hack=true