How do I randomize an array in Visual Basic 2010? - arrays

I have any array of questions like this:
Dim Questions(25) As TheQuestions
Function loadQuestions()
Questions(0).Question = "Which of these words are an adjective?"
Questions(0).option1 = "Dog"
Questions(0).option2 = "Beautiful"
Questions(0).option3 = "Steven"
Questions(0).option4 = "Bird"
Questions(0).Answer = "B"
Questions(1).Question = "What's the adjective in this sentence:" & vbCrLf & "'Kelly handled the breakable glasses very carefully'"
Questions(1).option1 = "Kelly"
Questions(1).option2 = "Handled"
Questions(1).option3 = "Carefully"
Questions(1).option4 = "Breakable"
Questions(1).Answer = "D"
Questions(2).Question = "What's the adjective in this sentence: 'Karen is a graceful dancer'"
Questions(2).option1 = "Is"
Questions(2).option2 = "Graceful"
Questions(2).option3 = "Dancer"
Questions(2).option4 = "Tanya"
Questions(2).Answer = "B"
...
I have found a way of randomizing the question successfully, but could I make sure that the correct, four potential answers are displayed along with the question being displayed?
Below is the code for calling the Function which gets the question and then displays it in a label (lblQuestion) and where the code I am looking for needs to go, I am guessing:
Function GetQuestion(ByVal intQuestion As Integer)
tmrOne.Start()
If questionNumber < 25 Then
lblQuestionNumber.Text = "Question" & " " & questionNumber
lblQuestion.Text = Questions(intQuestion).Question
btnAnswerA.Text = Questions(intQuestion).option1
btnAnswerB.Text = Questions(intQuestion).option2
btnAnswerC.Text = Questions(intQuestion).option3
btnAnswerD.Text = Questions(intQuestion).option4
strAnswer = Questions(intQuestion).Answer
questionNumber = questionNumber + 1
btnAnswerA.BackColor = Color.White
btnAnswerB.BackColor = Color.White
btnAnswerC.BackColor = Color.White
btnAnswerD.BackColor = Color.White
btnAnswerA.Enabled = True
btnAnswerB.Enabled = True
btnAnswerC.Enabled = True
btnAnswerD.Enabled = True
Return intQuestion
Else
MsgBox("You have finished")
End
End If
End Function
I used this:
lblQuestion.Text = Questions(random.Next(25)).Question
It randomizes the question, but how do I get it so that the four possible answers are shown with the correct question, like in the array above where there are FOUR options.
Many thanks!

Instead of:
lblQuestion.Text = Questions(random.Next(25)).Question
I think you just need to save the random number and do it like this:
dim questionChosen as int
questionChosen = random.Next(25)
lblQuestion.Text = Questions(questionChosen).Question
Then you update the rest of the fields using Questions(questionChosen).WhateverYouNeed

Related

Biopython for Loop IndexError

I get "IndexError: list is out of range" when I input this code. Also, the retmax is set at 614 because that's the total number of results when I make the request. Is there a way to make the retmode equal to the number of results using a variable that changes depending on the search results?
#!/usr/bin/env python
from Bio import Entrez
Entrez.email = "something#gmail.com"
handle1 = Entrez.esearch(db = "nucleotide", term = "dengue full genome", retmax = 614)
record = Entrez.read(handle1)
IdNums = [int(i) for i in record['IdList']]
while i >= 0 and i <= len(IdNums):
handle2 = Entrez.esearch(db = "nucleotide", id = IdNums[i], type = "gb", retmode = "text")
record = Entrez.read(handle2)
print(record)
i += 1
Rather than using a while loop, you can use a for loop...
from Bio import Entrez
Entrez.email = 'youremailaddress'
handle1 = Entrez.esearch(db = 'nucleotide', term = 'dengue full genome', retmax = 614)
record = Entrez.read(handle1)
IdNums = [int(i) for i in record['IdList']]
for i in IdNums:
print(i)
handle2 = Entrez.esearch(db = 'nucleotide', term = 'dengue full genome', id = i, rettype = 'gb', retmode = 'text')
record = Entrez.read(handle2)
print(record)
I ran it on my computer and it seems to work. The for loop solved the out of bounds, and adding the term to handle2 solved the calling error.

Using two array's in one application with progressing the arrays

I have the below code I'm trying to put together and I'm running into a Run-time error '9' subscript out of range. This does work through the first run through then errors. I don't see why it won't allow for the string to go forward. From what I'm reading it should go through the application changing the X values with Y value 1 and when completed with that set to go to the next Y and start the whole process again until the end of Y. Any help would be appreciated.
Dim Cat(1 To 10) As String
Cat(1) = "010" 'SD
Cat(2) = "020" 'FD
Cat(3) = "050" 'WVID
Cat(4) = "040" 'VID
Cat(5) = "030" 'MEM
Cat(6) = "080" 'ACC
Cat(7) = "060" 'HDMI
Cat(8) = "070" 'SSD
Cat(9) = "090" 'POWER
Cat(10) = "990" 'ZRM
Dim Month(1 To 12) As String
Month(1) = "January"
Month(2) = "February"
Month(3) = "March"
Month(4) = "April"
Month(5) = "May"
Month(6) = "June"
Month(7) = "July"
Month(8) = "August"
Month(9) = "September"
Month(10) = "October"
Month(11) = "November"
Month(12) = "December"
For Y = 1 To UBound(Cat)
For X = 1 To UBound(Month)
Month(X) = Application.WorksheetFunction.SumIf(Sheets(Month(X)).Columns("AO"), Cat(Y), Sheets(Month(X)).Columns("AG"))
Next X
Cells(3 + Y, 41).Value = Application.WorksheetFunction.Sum(Month(1), Month(2), Month(3), Month(4), Month(5), Month(6), Month(7), Month(8), Month(9), Month(10), Month(11), Month(12))
Next Y
End Sub
On the first run through the loop indexed by X you are computing a conditional sum of data stored in Sheet "January". And then overwriting "January" with that value in Month(X). Let's say that that value is 42. On your next run through the loop 42 is in Month(X) so you are looking for Sheets(42), which probably isn't a valid worksheet. I would try this instead.
dim temp as double
For Y = 1 To UBound(Cat)
temp = 0# '0 as a double.
For X = 1 To UBound(Month)
temp = temp + Application.WorksheetFunction.SumIf(Sheets(Month(X)).Columns("AO"), Cat(Y), Sheets(Month(X)).Columns("AG"))
Next X
Cells(3 + Y, 41).Value = temp
Next Y
This way we don't need to store all of the sums from each sheet, since we only use them to add them all together.

Creating an array of strings

I'm having a few problems with the following:
Dim design(6) As String
design(0) = "DES1"
design(1) = "DES1_slot"
design(2) = "DES2"
design(3) = "DES2_slot"
design(4) = "DES3"
design(5) = "DES3_slot"
I get the error "Expected end of statement"
Similarly:
Dim design(0 To 5) As String
design(0) = "DES1"
design(1) = "DES1_slot"
design(2) = "DES2"
design(3) = "DES2_slot"
design(4) = "DES3"
design(5) = "DES3_slot"
says "Expecting ')' "
I don't often use VBA, but from the quick googling I did at least one of these should work?
Thats perfectly valid VBA however you will get those exact errors in VBScript which is what I presume your using.
As VBScript is typeless change the first line to just:
Dim design(5)

String or binary data would be truncated when updating datatable

I am trying to update customer info. I use this code to load 5 fields:
cust_cn.Open()
Dim cust_da As New SqlDataAdapter("SELECT * FROM [customers] where [custID]=" & txtCustPhone.Text, cust_cn)
cust_da.Fill(cust_datatable)
txtCustPhone.Text = cust_datatable.Rows(0).Item("custID")
txtCustFirstName.Text = cust_datatable.Rows(0).Item("first")
txtCustLastName.Text = cust_datatable.Rows(0).Item("last")
txtCustAddress.Text = cust_datatable.Rows(0).Item("address")
txtCustZip.Text = cust_datatable.Rows(0).Item("zip")
and this works fine. When I try to modify one of the fields (change zip code on an existing customer)
with this code:
If cust_datatable.Rows.Count <> 0 Then
cust_datatable.Rows(0).Item("custID") = txtCustPhone.Text
cust_datatable.Rows(0).Item("first") = txtCustFirstName.Text
cust_datatable.Rows(0).Item("last") = txtCustLastName
cust_datatable.Rows(0).Item("address") = txtCustAddress.Text
cust_datatable.Rows(0).Item("zip") = txtCustZip.Text
'cust_datatable.Rows(custrecord)("custID") = txtCustPhone.Text
'cust_datatable.Rows(custrecord)("first") = txtCustFirstName.Text
'cust_datatable.Rows(custrecord)("last") = txtCustLastName.Text
'cust_datatable.Rows(custrecord)("address") = txtCustAddress.Text
'cust_datatable.Rows(custrecord)("zip") = txtCustZip.Text
cust_DA.Update(cust_datatable)
End If
I get the error: "String or binary data would be truncated"
I originally tried to update using the commented section, but it was only modifying the first record in the database.
Any thoughts?

Check if the value of text box is inside the array

below is my array and code to check if the data in textbox is inside of the array. the problem is when i run the program the value of array is always " " or no data found.. what's wrong with my code? please help me.. thank you.
a = Split((a), vbTab)
devices = Array("iPhone5", "iPhone4", "iPhone3", "iPad", "iPod", "iPhone4s", "iPhone3G", "iPhone3gs", "gt-s5360", "gt-i9505", "n7100", "gt-n7100", "i9300", "gt-i9300", "gt-p3100", "s5300", "gt-s5300", "gt-s7562", _
"gt-i8190", "s100", "p5100", "gt-p5100", "gt-s6102", "gt-i9100", "gt-p3110", "gt-p6200", "n8000", "gt-n8000", "gt-i9082", "sm-t210", "gt-n7105", "n7000", "gt-n7000", "gt-n5100", "GT-S5570", "GT-S5830i", _
"GT-S5830", "GT-I8262", "GT-P1000", "Nexus 7", "GT-I8160", "H120", "ALCATEL ONE TOUCH 918N", "HuaweiG510-0200", "MyPhone A919 Duo", "MyPhone A848i Duo", "C6603", "ALCATEL ONE TOUCH 4030E", "LG-E400", _
"GT-P6800", "ICE 350e", "GT-I9070", "ALCATEL ONE TOUCH 5021E", "Cherry w500", "GT-I8150", "LT22i", "Spark TV", "I9500", "GT-I9500", "Burst S280", "W120", "GT-P7500", "MyPhone A888 Duo", "GT-S5301", "Thunder S220", "GT-S7500", _
"GT-I8552", "SM-T211", "GT-S5282", "A818 Duo", "LT26i", "GT-S6802", "GT-S5570I", "HuaweiY210-0100", "LT26w", "HTC One", "ST23i", "ST27i", "SHW-M250S", "Cruize W280", "Titan TV S320", "B1-A71", "GT-I9152", "W110", "7038", _
"LT18i", "GT-P3113", "GT-I9000", "Cherry Sonic", "GT-S5670", "SHW-M110S", "ST26i", "SonyEricssonMT25i", "Excite_352g", "LT25i", "Lenovo A390_ROW", "ST25i", "LG-E612", "GT-I9003")
urls = Array("youtube.com", "ytimg.com", "DoubleClick.net", "google.com", "fbcdn.net", "google -analytics.com", "yimg.com", "googlesyndication.com", "facebook.com", "gstatic.com", "mywebacceleration.com", "yahoo.com", "scorecardresearch.com", _
"google.com.ph", "adnxs.com", "redtubefiles.com", "rubiconproject.com", "wattpad.net", "www.com", "youjizz.com", "bing.net", "akamaihd.net", "xvideos.com", "tumblr.com", "twitter.com", "yieldmanager.com", "sharethis.com", "wikimedia.org", _
"y8.com", "sulitstatic.com", "globe.com.ph", "googleapis.com", "tagstat.com", "quantserve.com", "addthis.com", "blogspot.com", "king.com", "cloudfront.net", "ayosdito.com", "ask.com", "openx.net", "bigspeedpro.com", "gravatar.com", _
"amasvc.com", "bing.com", "cdn.com", "yldmgrimg.net", "cedexis.com")
For intX = 0 To UBound(a)
If Text11.Text = "" Then
a(intX) = UCase(a(intX))
Text11.Text = a(intX)
ElseIf Text12.Text = "" Then
a(intX) = UCase(a(intX))
Text12.Text = a(intX)
If Len(Text12.Text) <= 17 Then
Text12.Text = ""
Else
b = Split(Text12.Text, "/")
For i = 0 To UBound(b)
Text12.Text = b(2)
Next
Text12 = InStr(Text12, urls)
If Text12 = UCase(urls) Then
Text22 = Text12
Text25 = count + 1
Else
Text26 = Text12
Text27 = othercount + 1
End If
End If
You can use ForEach to make it more effective.
Or use this function... :)
Public Function IsContained(theArray() As Variant, strSearchPharse As String, Optional IsMatch As Boolean = False, Optional IsCaseSensitive As Boolean = True) As Boolean
On Error Resume Next
If Not UBound(theArray) 0 Then End
Dim strExploded As String
Dim gsChache As Boolean
gsChache = False 'set the default value
'Checking for every array thing
For Each strExploded In theArray
If IsMatch And Not (Len(strSearchPharse) = Len(strExploded)) Then 'if its matchable...
If IsCaseSensitive And strExploded = strSearchPharse Then gsChache = True
If Not IsCaseSensitive And LCase(strExploded) = LCase(strSearchPharse) Then gsChache = True
ElseIf Not IsMatch And Not IsCaseSensitive Then 'if its not matchable, and not case sensitive
If InStr(0, LCase(strExploded), LCase(strSearchPharse)) >= 1 Then gsChache = True
Else 'if its not matchable, and Case Sensitive
If InStr(0, strExploded, strSearchPharse) >= 1 Then gsChache = True
End If
DoEvents
Next strExploded
IsContained = gsChache 'finish
End Function
How to use it?
theArray is for array variable, strSearchPharse is the text you want to search (like keyword, e.g. apple or youtube), then isMatch is is the text you want to search is need to exactly same (not case sensitive, buat it will if you enable it.), IsCaseSensitive is the text you need to search on is need to case sensitive or not.
the default setting is, Not Matchable, and Case Sensitiveable.
Sample:
XYZ = Array("Satu", "Dua", "Tiga", "Empat", "Lima", "Enam", "Tujuh", "Delapan", "Sembilan", "Sepuluh")
If IsContained(XYZ, "o", , False) Then
MsgBox "The Array contains o alphabet."
Else
MsgBox "The Array have no o alphabet."
End If
The result will show "The Array contains o alphabet." MsgBox... :)

Resources