How can I stop an array from repeating in Visual Basic 2010? - arrays

I have randomized an array, so questions are shown in a different order every time that the quiz is played, but would prefer it if the questions weren't able to repeat.
Here is how my array is set up:
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"
...
This is the function that calls the questions when the quiz begins.
Function GetQuestion(ByVal intQuestion As Integer)
tmrOne.Start()
If questionNumber < 11 Then
lblQuestionNumber.Text = "Question" & " " & questionNumber
Dim questionChosen As Integer
questionChosen = random.Next(25)
lblQuestion.Text = Questions(questionChosen).Question
btnAnswerA.Text = Questions(questionChosen).option1
btnAnswerB.Text = Questions(questionChosen).option2
btnAnswerC.Text = Questions(questionChosen).option3
btnAnswerD.Text = Questions(questionChosen).option4
strAnswer = Questions(questionChosen).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 have tried to find something on the internet to help with this, but haven't been successful or understood it as I am new to this. I have found ArrayList.RemoveAt but not sure that is the correct syntax to use with my array?
So, how do I stop my array from repeating a question once it has already been asked? Do I put them into another array?
Any help is greatly appreciated!

As I understand from your question, you are using an ArrayList. In that case, yes, the RemoveAt option sounds as the best alternative. Bear in mind that pure arrays (e.g., Dim Questions() As String) are the most efficient type of Collection; the main advantage of Lists/ArrayLists is precisely how easy is adding/removing elements, so if you use an ArrayList, better maximising one of its defining features.
The additional advantage in your specific case is that every time you remove an item, its position is filled (and the total number of items decreased). Thus, you have just to update the maximum value of your random number generator to the current number of indices (not elements, because the first index is zero).
In summary, the following changes in your code will deliver what you want:
If(Questions.Count > 0) Then
questionChosen = random.Next(0, Questions.Count - 1)
End If
And once you are done with it:
If(questionChosen >= 0 And questionChosen <= Questions.Count - 1) Then
Questions.RemoveAt(questionChosen)
End If

Related

Sorting array with starter inside the array

sorry I had issues formulating my question.
to explain it better :
I have a = ["alice", "jean", "bob"]
Now i want to let the user choose who will start the game.
If its jean, the new array should be like this
a = ["jean", "bob", "alice"]
So far, this is working :
def sort_array_players(array_player, starter)
sort_array_player = []
array_player.map do |name|
if name == starter && name == array_player[0]
sort_array_player = [array_player[0], array_player[1], array_player[2]]
elsif name == starter && name == array_player[1]
sort_array_player = [array_player[1], array_player[2], array_player[0]]
elsif name == starter && name == array_player[2]
sort_array_player = [array_player[2], array_player[0], array_player[1]]
end
end
puts sort_array_player
end
I want to refractor this code but i'm a bit new to ruby, I've spend 2 hours trying to figure out this thing. My guess is that you need to use each.with_index and then create the new array starting by the first one and the following element would be the one with the index of the starter + 1..
Thanks for helping guys
As #rubish commented, you can use Array#rotate method.
You can use positive integer as count parameter to rotate items counter clockwise or negative integers to rotate them clockwise.
a = ["alice", "jean", "bob"]
starter = "jean"
count = a.index(starter) # => 1
a.rotate(count) # => ["jean", "bob", "alice"]

How to retrieve a VB Control instance, given its Name and Index?

If I have a string "cboEnoughMoney(0)", is it possible to retrieve the control from a control array that I have in my form, cboEnoughMoney?
In my code, I try to refer to the control in this way:
frm.Controls(sControlName).ListIndex = -1
sControlName in this case is "cboEnoughMoney(0)" and I get an error that the control was not found.
EDIT: One of the issue's i'm having is trying to as above set the .listindex to -1, another one is where I actually try to get and set value to the combobox. In a function - i pass form name as parameter called frm.
In the code what has been done is this....
dim ctlData as string
If Frm.Controls(RS!CTRLname).ListIndex > 0 Then
CtlData = Frm.Controls(RS!CTRLname).Value
Else
CtlData = ""
End If
Any idea how I'd be able to do that with cboEnoughMoney(0)....
I assume I could follow your example and check
if instr(1,RS!CTRLname, "(")
but if there is, how would I refer to that particular control in frm
It is just enough to look in the VB Properties Window: if you search "cboEnoughMoney(0)" you will find "cboEnoughMoney"(0) (without double quotes). Still the name is the same as a non-array control.
So, it is perfectly legal to refer to a VB Control with Name and Index, no need for a controls loop here:
If isControlsArray Then
Set ctrl = Me.Controls(Name)(Index) '// array
Else
Set ctrl = Me.Controls(Name) '// non array
End If
You cannot use the (index) part in a lookup via .Controls.
You can loop them manually & given that you can safely assume any control name with a ( is an array member and thus will have an Index property match on that:
Private Function GetControl(ByVal name As String) As VB.Control
Dim pos As Long: pos = InStr(name, "(")
If pos = 0 Then
Set GetControl = Me.Controls(name) '// non array
Else
Dim index As Long
index = Val(Mid$(name, pos + 1)) '// get index #
name = Left$(name, pos - 1) '// get base name
For Each GetControl In Me.Controls
If (GetControl.name = name) Then
If (GetControl.index = index) Then Exit Function
End If
Next
Set GetControl = Nothing
End If
End Function
And then:
GetControl("cboEnoughMoney(1)").ListIndex = -1

How to loop through document and find words from predefined array and replace each word with words from another array

I want to loop word document and find words from arrayOne.
for example
arrayOne = ["john", "jack", "dog"]
and each word that matches to replace with words from arrayTwo
arrayTwo = ["ana", "tanja", "cat"]
You have not supplied any code with issues so there is nothing to fix, however you have asked 'How to find words and replace them'
Below are the steps you will need to code to achieve this: -
Build your array
Dim AryFNR(1,2) As String
AryFNR(0,0) = "john"
AryFNR(1,0) = "ana"
AryFNR(0,1) = "jack"
AryFNR(1,1) = "tanja"
...
Connect to the document
Dim WdDoc As Document
Set WdDoc = ThisDocument
...
Set WdDoc = Nothing
Connect to the selection ensuring it is in the connected document
Dim WdSlct As Selection
WdDoc.Content.Select
Set WdSlct = Selection
WdSlct.SetRange 0, 0
....
Set WdSlct = Nothing
Connect to Find and Replace and clear previous settings
Dim WdFnd As Find
Set WdFnd = WdSlct.Find
WdFnd.ClearAllFuzzyOptions
WdFnd.ClearFormatting
...
Set WdFnd = Nothing
Process your array using WdFnd on each item
Dim LngCounter As Long
For LngCounter = 0 To Ubound(AryFNR,2)
With WdFnd
.Execute FindText:=AryFNR(0, LngCounter), ReplaceWith:=AryFNR(1, LngCounter)
End With
Next
Some points you'll want to watch out for: -
Footnotes and Endnotes are not processed unless the selection in the Footnote/Endnote.
There are extra values on the .Execute command that you will want to set, depending on how you code it .Found may be useful.
That is enough information for you to start your own code to achieve your goal, enjoy!

VBA - Naming of variables from array

Good Morning.
Is possible naming of variables with composition of text and another variables ?
Only for an example (not functional):
Components = "AA,BB,CC"
Values = "101,102,103"
ComponentsArr = Split(Components)
ValuesArr = Split(Values)
For i = 1 To UBound(ComponentsArr)
Let "Var" & ComponentsArr(i) = ValuesArr(i)
Next i
You might be able to use a dictionary to accomplish your purpose. You add key/value pairs to the dictionary. Then you can use the key to get the value.
Set a reference to MS Scripting runtime ('Microsoft Scripting Runtime')
Components = "AA,BB,CC"
Values = "101,102,103"
ComponentsArr = Split(Components, ",")
ValuesArr = Split(Values, ",")
Dim dict As New Scripting.Dictionary
For i = LBound(ComponentsArr) To UBound(ComponentsArr)
Call dict.Add(ComponentsArr(i), ValuesArr(i))
Next i
If dict.Exists("AA") Then
Debug.Print dict.Item("AA") 'prints 101
End If
See also: https://stackoverflow.com/a/915333/2559297
It might be easier to do it like the following:
Components = Array("AA", "BB", "CC")
Values = Array("101", "102", "103")
Then you wouldn't need ComponentsArr and ValuesArr.
For i = LBound(Components) To UBound(Components)
dict.Add(Components(i), Values(i))
Next i

VBA. Array for search or replace

Need to found any symbol of array.
For example:
replace(string,[a,b,c,e,f,g],"a1b2c3d4e567");
result = "1234567"
How do it ?
If your goal is to remove all non-numeric characters, the following will work:
' Added reference for Microsoft VBScript Regular Expressions 5.5
Const s As String = "a1b2c3d4e567"
Dim regex2 As New RegExp
Dim a As String
regex2.Global = True
regex2.Pattern = "[^0-9]"
Dim a As String = regex2.Replace(s, "")
MsgBox (a) ' Outputs 1234567
If you are looking for specific characters, change the pattern.
AFAIK you are going to have to do this by consecutive calls to replace
result = "a1b2c3d4e567"
result = replace(result,"a","")
result = replace(result,"b","")
result = replace(result,"c","")
etc

Resources