Call random Sub

thestar19

Member
Licensed User
Longtime User
Hey guys

So,I have this application where there will be ALOT of questions which will be sorted using subs.
I did of course only add 3 questions under development so that the code wont be as huge.

I have built a Small C# program which will create this for me but with the real questions so that I can copy and paste them all
Thats the reason to that this code must be simple and the heavy part must be somewhere else...
B4X:
Sub question_2
question.Text = "How many goals?"
answer1.Text = "10"
answer2.Text = "20"
answer3.Text = "30"
correct.Text = "1"
End Sub





My code sofar:
B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim dev_screen As Button
   Dim go_to_test As Button
   Dim nothing_yet As Button
   Dim answer1 As Button
   Dim answer2 As Button
   Dim answer3 As Button
   Dim question As Label
   Dim correct As Label
   Dim Panel1 As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("main")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub nothing_yet_Click
Msgbox("I said NOTHING YET!","Dumass")
End Sub
Sub go_to_test_Click
Msgbox("Laddar","Info")
Activity.LoadLayout("page1")
CallSub(new_question,"new_question")
End Sub
Sub dev_screen_Click
Msgbox("None","Info")
CallSub(question_1,"question1")
End Sub

Sub question_Click
Msgbox("The question is:","Information")
End Sub
Sub answer3_Click
If correct.Text = "3" Then
  Msgbox("Correct","Info")
  CallSub(new_question,"new_question")
  End If
If correct.Text = "1" OR "2" Then
  Msgbox("Wrong!","Info")
  End If
End Sub
Sub answer2_Click
If correct.Text = "2" Then
  Msgbox("Correct","Info")
  CallSub(new_question,"new_question")
  End If
If correct.Text = "1" OR "3" Then
  Msgbox("Wrong!","Info")
  End If
End Sub
Sub answer1_Click
If correct.Text = "1" Then
  Msgbox("Correct","Info")
  CallSub(new_question,"new_question")
  End If
If correct.Text = "2" OR "3" Then
  Msgbox("Wrong!","Info")
  End If
End Sub
Sub Activity_Touch (Action As Int, X As Float, Y As Float)
   
End Sub
Sub new_question
CallSub(question_1,"question_1")
End Sub

Sub question_1
question.Text = "Who is the best refree?"
answer1.Text = "Emma"
answer2.Text = "Alexander"
answer3.Text = "Per"
correct.Text = "2"
End Sub
Sub question_2
question.Text = "How many goals?"
answer1.Text = "10"
answer2.Text = "20"
answer3.Text = "30"
correct.Text = "1"
End Sub

So,How do I get "new_question" to randomly choose from the "questions-subs"?

This is what should happen:
1.The user clicks on one of the buttons
2.he gets a a MSGbox with "Wrong" or "correct"
3.The application randomly chooses another question sub
Like that over and over

Thank you
Thestar19

PS
I have ofcourse created layouts called main and page1
 

DouglasNYoung

Active Member
Licensed User
Longtime User
Hi,
Why do you need individual subs for each question? That seems to over complicate things!
Could you not hold the questions and answers in a database or perhaps an array? That way you'd only need a single sub calling a random record, and you could update the data without having to redo the code!
Clearly the choice is up to you, but if you really have 'A LOT' of questions a Sqlite database would seem to me to be the way to go.

Cheers,
Douglas
 
Upvote 0

thestar19

Member
Licensed User
Longtime User
Server

Hey guys

The answere is simple

To use SQL you must have some kind of server and this is a hobby project so I dont have any servers to use.

I just chose this because is seemed simplest,But hey,Show me something else and Ill try it :)

Thank you
Thestar19

Edit:

Sorry I forgot to say this
There are about 300 questions that I will add to the app over time...
 
Last edited:
Upvote 0

thestar19

Member
Licensed User
Longtime User
Okay

Hey

So,How do I do it?

I have been looking at some examples but they all seem really wierd.

Can I create a Database using some kind of other editor and simply load the data in the application?

And if I can,How????

Thank you
Thestar19
 
Upvote 0

thestar19

Member
Licensed User
Longtime User
Yes, you can download a free SQLite editor like THIS one to create your database, populate your tables etc. then add the database to your project.

Okay,How do I load the facts in to the app and use it for questions and answered??

Edit,Cant I use a textfile????
Would be so much simpler!or?
 
Last edited:
Upvote 0

NJDude

Expert
Licensed User
Longtime User
If you know how to use SQLite then you just add the database to your project, then, you can query and populate the question and answers fields, like I said before, there are plenty of examples in the forums if you need some code.
 
Upvote 0

thestar19

Member
Licensed User
Longtime User
Cant I use a textfile to load the questions?

It seems so much simpler since Im not used to SQL
 
Last edited:
Upvote 0

NJDude

Expert
Licensed User
Longtime User
You could, but you'll have to parse the text, you can use a CSV for that and then handle the random procedure to get the question and answers.

But the easiest way will be using a SQLite database.
 
Upvote 0
Top