|
-
May 14th, 2013, 09:08 PM
#1
Thread Starter
Lively Member
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.
-
May 14th, 2013, 09:26 PM
#2
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?
-
May 14th, 2013, 11:44 PM
#3
Thread Starter
Lively Member
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.
-
May 15th, 2013, 01:05 AM
#4
Thread Starter
Lively Member
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.
-
May 15th, 2013, 01:45 AM
#5
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.
-
May 15th, 2013, 01:56 AM
#6
Thread Starter
Lively Member
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.
 Originally Posted by jmcihinney
What are PickOne, PickTwo, etc?
 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)) & "!")
 Originally Posted by jmcihinney
Can their values change?
 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.
-
May 15th, 2013, 02:12 AM
#7
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
-
May 15th, 2013, 02:17 AM
#8
Thread Starter
Lively Member
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.
-
May 15th, 2013, 02:23 AM
#9
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".
-
May 15th, 2013, 03:56 AM
#10
Re: Looking for an easier way to nestle multiple If statements
 Originally Posted by Taem
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:
Public Class MainForm Private Sub PickElementValue() Dim nineInEight As String = "4" ' Example... Dim dic As New Dictionary(Of String, String) From { {"1", "PickOne"}, {"2", "PickTwo"}, {"3", "PickThree"}, {"4", "PickFour"}, {"5", "PickFive"} } Dim picked = (From d In dic Where d.Key = nineInEight Select d.Value).FirstOrDefault Me.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", picked) End Sub End Class
-
May 15th, 2013, 05:11 AM
#11
Re: Looking for an easier way to nestle multiple If statements
 Originally Posted by ident
vb Code:
Public Class MainForm Private Sub PickElementValue() Dim nineInEight As String = "4" ' Example... Dim dic As New Dictionary(Of String, String) From { {"1", "PickOne"}, {"2", "PickTwo"}, {"3", "PickThree"}, {"4", "PickFour"}, {"5", "PickFive"} } Dim picked = (From d In dic Where d.Key = nineInEight Select d.Value).FirstOrDefault Me.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", picked) End Sub 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.
-
May 15th, 2013, 05:26 AM
#12
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:
Public Class Form1 Private Sub PickElementValue() Dim nineInEight As String = "4" ' Example... Dim dic As New Dictionary(Of String, String) From { {"1", "PickOne"}, {"2", "PickTwo"}, {"3", "PickThree"}, {"4", "PickFour"}, {"5", "PickFive"} } Me.WebBrowser1.Document.GetElementById("promptInput_41188").SetAttribute("value", dic(nineInEight)) End Sub 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|