Results 1 to 12 of 12

Thread: Looking for an easier way to nestle multiple If statements

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Posts
    123

    Looking for an easier way to nestle multiple If statements

    Here is my the relevant section of my code:

    Code:
     If NineInEight = "1" Then
                        If PickIt = "1" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickOne)
                        If PickIt = "2" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickTwo)
                        If PickIt = "3" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickThree)
                        If PickIt = "4" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickFour)
                        If PickIt = "5" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickFive)
                        If PickIt = "6" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickSix)
                        If PickIt = "7" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickSeven)
                        If PickIt = "8" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickEight)
                        If PickIt = "9" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickNine)
                        If PickIt = "10" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickTen)
                        If PickIt = "11" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickEleven)
                        If PickIt = "12" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickTwelve)
                        If PickIt = "13" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickThirteen)
    
                    ElseIf NineInEight = "2" Then
    
                    End If
    Basically I just want to shorten it. I'm pretty sure I can use: For i = 1 to 13 / Next and set the If Pickit = i Then.... ("value", Pick......) <- but this is where I'm stumped. Not sure how to proceed to make this simpler on myself using the value "Pick"... Ideas?
    Last edited by Taem; May 14th, 2013 at 09:14 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Looking for an easier way to nestle multiple If statements

    What are PickOne, PickTwo, etc? Can their values change? Are you going to have something similar to what's in the If block in the ElseIf block?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Posts
    123

    Re: Looking for an easier way to nestle multiple If statements

    Code:
    Dim TYPE1 As String() = {"like", "love", "lust", "enjoy", "relish", "savor", "taste", "devour", "get off on", "revel in", "indulge in", "gratify myself with"}
    Dim PickOne As String = ("I really " & TYPE1(rand.Next(0, 12)) & " your " & TYPE4(rand.Next(0, 12)) & " " & TYPE5(rand.Next(0, 12)) & "!")
    I'm testing out a random sentence generator. That dimmed sentence worked when I tested it as TextBox1.Text =, but I have not yet tested it in this fashion. So yes, every Pick has a separate value (as does TYPE). I suppose I could dim them as Pick1, Pick2, etc., and then what I suggested with the For/Next loop might just work. I'll give it a shot and report back here what happens, unless someone has a better idea or reason why I'll fail.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Posts
    123

    Re: Looking for an easier way to nestle multiple If statements

    Well, this did not work :

    Code:
     For i = 1 To 13
                    Dim TheValue As String = "Pick" & i.ToString
                    If i = PickIt Then TextBox1.Text = TheValue
    Kept getting Pick + random numbers, but not the dimmed statement. Darn, thought I had it for sure that time. The "If i" statement works because I changed the = to PickOne and it worked fine, so... I just need to figure out how to dim a string as the value of an already dimmed string and not just text.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Looking for an easier way to nestle multiple If statements

    I see three question marks in post #2, which would imply that I asked three questions. I can only see an answer to one of them. You're about the fifth person in the last two or three days who has not bothered to answer the questions I've asked and I'm heartily sick of asking for information over and over when someone else has asked for help. I'm not going to post anything more to this thread so, by not answering my questions you have reduced your chance of getting the help you want. Think about that next time someone asks you for more information in order to be able to help you and please don't waste their time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Posts
    123

    Re: Looking for an easier way to nestle multiple If statements

    I suppose there is a misunderstanding, but if you don't wish to help, then I understand.

    Quote Originally Posted by jmcihinney
    What are PickOne, PickTwo, etc?
    Quote Originally Posted by From post 3
    I'm testing out a random sentence generator.
    Code:
    Dim TYPE1 As String() = {"like", "love", "lust", "enjoy", "relish", "savor", "taste", "devour", "get off on", "revel in", "indulge in", "gratify myself with"}
    Dim PickOne As String = ("I really " & TYPE1(rand.Next(0, 12)) & " your " & TYPE4(rand.Next(0, 12)) & " " & TYPE5(rand.Next(0, 12)) & "!")
    Quote Originally Posted by jmcihinney
    Can their values change?
    Quote Originally Posted by Also from post 3
    So yes, every Pick has a separate value (as does TYPE).
    Maybe you were asking something else? I thought I had those two covered. Sorry if I let you down in someway.

    Are you going to have something similar to what's in the If block in the ElseIf block?
    I failed to notice this and for that, I can only apologize at this point. In case someone else wants to take a crack, the answer to this question is yes.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Looking for an easier way to nestle multiple If statements

    OK, everyone deserves a second chance.

    The fact that you're testing out a random sequence generator is not an answer to my first question. Your code answers that question, i.e. they are Strings and their values are constructed as shown.

    The fact that each Pick variable has a separate value is not an answer to my second question. Your code shows a value being assigned to one of the variables where it's declared but it doesn't indicate whether that will be the value of that variable for its entire life or whether a new value will or may be assigned at a later stage.

    Anyway, I'm thinking that the way to go would be to use a Dictionary, e.g.
    Code:
    Dim myDictionary As New Dictionary(Of String, String) From {{"1", "Hello World"},
                                                                {"2", "Goodbye Cruel World"}}
    Your original code would then reduce to:
    Code:
    If NineInEight = "1" Then
        WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickIt)
    ElseIf NineInEight = "2" Then
        '...
    End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Posts
    123

    Re: Looking for an easier way to nestle multiple If statements

    I appreciate your reply and will be more comprehensive with my answers in the future. I've never tried a "dictionary" so I will have to do some research and try it out before I post back the results, but it looks like a viable option for my problem. Thank you again for your suggestion.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Looking for an easier way to nestle multiple If statements

    You might not even need a Dictionary. I was just using the same numerical Strings that you did as keys but, given that they are sequential numbers, you could probably just use an array. You can then get the appropriate String via the numerical indexes 0 to 12 instead of the textual keys "1" to "13".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Looking for an easier way to nestle multiple If statements

    Quote Originally Posted by Taem View Post
    Here is my the relevant section of my code:

    Code:
     If NineInEight = "1" Then
                        If PickIt = "1" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickOne)
                        If PickIt = "2" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickTwo)
                        If PickIt = "3" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickThree)
                        If PickIt = "4" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickFour)
                        If PickIt = "5" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickFive)
                        If PickIt = "6" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickSix)
                        If PickIt = "7" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickSeven)
                        If PickIt = "8" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickEight)
                        If PickIt = "9" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickNine)
                        If PickIt = "10" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickTen)
                        If PickIt = "11" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickEleven)
                        If PickIt = "12" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickTwelve)
                        If PickIt = "13" Then WebWindow.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", PickThirteen)
    
                    ElseIf NineInEight = "2" Then
    
                    End If
    Basically I just want to shorten it. I'm pretty sure I can use: For i = 1 to 13 / Next and set the If Pickit = i Then.... ("value", Pick......) <- but this is where I'm stumped. Not sure how to proceed to make this simpler on myself using the value "Pick"... Ideas?
    vb Code:
    1. Public Class MainForm
    2.  
    3.     Private Sub PickElementValue()
    4.         Dim nineInEight As String = "4" ' Example...
    5.         Dim dic As New Dictionary(Of String, String) From
    6.                                                                   {
    7.                                                                       {"1", "PickOne"},
    8.                                                                       {"2", "PickTwo"},
    9.                                                                       {"3", "PickThree"},
    10.                                                                       {"4", "PickFour"},
    11.                                                                       {"5", "PickFive"}
    12.                                                                   }
    13.  
    14.         Dim picked = (From d In dic Where d.Key = nineInEight Select d.Value).FirstOrDefault
    15.         Me.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", picked)
    16.     End Sub
    17. End Class

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Looking for an easier way to nestle multiple If statements

    Quote Originally Posted by ident View Post
    vb Code:
    1. Public Class MainForm
    2.  
    3.     Private Sub PickElementValue()
    4.         Dim nineInEight As String = "4" ' Example...
    5.         Dim dic As New Dictionary(Of String, String) From
    6.                                                                   {
    7.                                                                       {"1", "PickOne"},
    8.                                                                       {"2", "PickTwo"},
    9.                                                                       {"3", "PickThree"},
    10.                                                                       {"4", "PickFour"},
    11.                                                                       {"5", "PickFive"}
    12.                                                                   }
    13.  
    14.         Dim picked = (From d In dic Where d.Key = nineInEight Select d.Value).FirstOrDefault
    15.         Me.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", picked)
    16.     End Sub
    17. End Class
    That's the wrong way to use a Dictionary. The whole point of a Dictionary is that you give it a key and it gives you the value.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Looking for an easier way to nestle multiple If statements

    Never needed any real use for the Dictionary class. Made the example from the intellisense. Not often i dont consult MSDN with unknown classes. Lesson learned rush posting John. Also had the tab open for a while before you posted. http://msdn.microsoft.com/en-us/libr...code-snippet-1

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub PickElementValue()
    4.         Dim nineInEight As String = "4" ' Example...
    5.         Dim dic As New Dictionary(Of String, String) From
    6.                                                                   {
    7.                                                                       {"1", "PickOne"},
    8.                                                                       {"2", "PickTwo"},
    9.                                                                       {"3", "PickThree"},
    10.                                                                       {"4", "PickFour"},
    11.                                                                       {"5", "PickFive"}
    12.                                                                   }
    13.  
    14.         Me.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", dic(nineInEight))
    15.     End Sub
    16.  
    17. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width