MJob
MJob

Reputation: 33

How to put sub procedures/functions into an array

i want to make a vb program that asks random questions in a label with predefined multiple choices from option buttons. Each question and multiple choices should be in a specific sub procedure. so that if there are 10 questions to be asked, then there will be 10 sub procedures for the questions. Since i want the questions to be asked at random without a specific order, i want to put the question sub procedures in an array in that i can use the randomize function to call the procedures randomly.

i.e putting these two sub procedures into an array

private sub question1()
lblquestion.caption = "What is 1 + 1"
opt1.caption="A. 2"
opt2.caption="B. 3"
end sub

private sub question2()
lblquestion.caption = "What is 5 + 1"
opt1.caption="A. 4"
opt2.caption="B. 6"
end sub

Upvotes: 1

Views: 188

Answers (2)

MJob
MJob

Reputation: 33

i manage to somwhow find a simpler way through @bob77 idea of using decision making statements. i chose to use select case as a possible solution and of course its simplicity. i however got rid of the array(guess this will no longer be answering the question as it is, but will still implement what was expected). here is the code.

dim q as integer
private sub cmdask_click()
Randomize
q = int((rnd * 10)+1)
select case q
case 1 
  call question1
case 2
  call question2
 'and so on..
End select

however, the biggest limitation about this is that the code will really be bulky(assume the program was to have 100 questions) and also repetition of questions will be an issue (i am learner i don,t know how to deal with that) I will really appreciate if anyone will suggest a code that will deal with repetition and bulkiness.(Yes I know there might be already answered questions on repetition and bulkiness. i just want a more customized code to this thread.)

Upvotes: 0

user65839
user65839

Reputation:

(Much of the meat of this answer is copied from my other answer on calling a function by its name as a String, and while this question may be a duplicate some people may find it helpful to describe the solution in this context as well.)

VB6 is in many ways an object-oriented language. Even if it may not have quite as much object-orientedness as C# or Java might in some respects, it does support multiple classes and polymorphism. As such, if you're trying to do something where you want to select one of several implementations at runtime, you clearly just want to have multiple classes, and select which object at runtime to use.

In fact, VB6 even lets you define real actual Interfaces. (They just happen to look like any other class module, but without any content in the functions.) Create a Class Module to use for your interface, say named MyInterface:

Public Sub DoStuff()
End Sub

Then, create your ten Class Modules, one for each possible implementation of the Interface:

In MyClassOne:

Implements MyInterface
Public Sub MyInterface_DoStuff()
    Msgbox "I'm function one"
End Sub

Then, in MyClassTwo, the same thing but with another implementation:

Implements MyInterface
Public Sub MyInterface_DoStuff()
    Msgbox "I'm function two"
End Sub

And so forth. The array you want to create isn't really of the methods, but of the objects that implement your defined interface:

Dim MyObjects(10) As MyInterface
Set MyObjects(1) = New MyClassOne
Set MyObjects(2) = New MyClassTwo
' and so on

Now, you can figure out which implementation you want, and just call it:

Dim WhichObject As Integer
WhichObject = SomehowSelectANumber()
MyObjects(WhichObject).DoStuff

For further reading in the MSDN library:

Upvotes: 3

Related Questions