|
-
Mar 8th, 2012, 02:23 PM
#1
Thread Starter
Member
Link items in List
Hi,
I have two string lists in VB.NET 2010, one with questions and one with answers:
Code:
questionlist.Add("When shall we meet?")
questionlist.Add("Let's meet at eight")
questionlist.Add("Shall we meet in front of the bar?")
questionlist.Add("Where are you going to go at the weekend?")
questionlist.Add("Who are you going to go with?")
questionlist.Add("Where shall we meet?")
questionlist.Add("I'm going to go to Granada with my friend")
questionlist.Add("When are you going to leave?")
questionlist.Add("I'm going to leave on Saturday at eight")
questionlist.Add("How long are you going to stay?")
questionlist.Add("I'm going to stay two nights")
questionlist.Add("What are you going to do?")
questionlist.Add("I'm going to visit some typical villages")
answerlist.Add("A que hora nos vemos?")
answerlist.Add("Nos vemos a las ocho")
answerlist.Add("Nos vemos delante del bar?")
answerlist.Add("Adonde vas a ir el fin de semana?")
answerlist.Add("Con quien vas a ir?")
answerlist.Add("Donde nos vemos?")
answerlist.Add("Voy a ir a Grandada con mi amiga")
answerlist.Add("Cuando vas a salir?")
answerlist.Add("Voy a salir el sabado a las ocho")
answerlist.Add("Cuanto tiempo vas a quedarte")
answerlist.Add("Voy a quedarme dos noches")
answerlist.Add("Que vas a hacer?")
answerlist.Add("Voy a visitar pueblos tipicos")
I have got code to randomly pick a entry from the list and display it in a textbox:
Code:
TextBox2.Text = questionlist.Item(Int(Rnd() * (questionlist.Count - 1)))
TextBox3.Text = questionlist.Item(Int(Rnd() * (questionlist.Count - 1)))
TextBox5.Text = questionlist.Item(Int(Rnd() * (questionlist.Count - 1)))
TextBox7.Text = questionlist.Item(Int(Rnd() * (questionlist.Count - 1)))
TextBox9.Text = questionlist.Item(Int(Rnd() * (questionlist.Count - 1)))
There is then another box to enter the answer to the question. When a button is clicked the program will check if the answer matches the answer in the list.
The problem is that I need it to pick the corresponding entry from the answer list to check if the answer is correct. How can I do this?
-
Mar 8th, 2012, 04:51 PM
#2
Re: Link items in List
First off, take a look at the Random object. It is much better than using the old school Rnd.
Second, there are better collections to use than lists. For example, you could actually use a Dictionary (of String, String), where the key would be the question and the value would be the answer. Another alternative would be to create a class or structure that had the question and answer as member variables and keep a list of that object, thereby keeping the two together.
Alternatively, you would need to keep track of the indexes of the questions being asked such that you could show the corresponding answer. If you are asking a series of questions, as you appear to be, you'd have to keep a list or array of the indexes.
My usual boring signature: Nothing
 
-
Mar 8th, 2012, 05:01 PM
#3
Re: Link items in List
If you are hard-coding questions/answers then a simple class would work.
Code:
Public Class Question
Public Property Identifier As Int32
Public Property Text As String
Public Property Response As String
Public Sub New()
End Sub
End Class
Store items in a List(Of Question)
Code:
Private MyQuestions As New List(Of Question)
Populate it
Code:
MyQuestions.Add(
New Question With
{
.Identifier = 1,
.Text = "Question 1",
.Response = "Answer for question 1"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 2,
.Text = "Question 2",
.Response = "Answer to middle questions"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 3,
.Text = "Question 3",
.Response = "Answer to last question"
}
)
Use the Identifier to select a random question from the list of questions which has both question and answer. Of course you would not be using Console.WriteLine, this is used to simply show you can get this information easily.
Code:
Dim Current = MyQuestions.Item(MyRandomNumber)
Console.WriteLine("Question: {0}", Current.Text)
Console.WriteLine("Answer: {0}", Current.Response)
-
Mar 8th, 2012, 05:17 PM
#4
Re: Link items in List
Here's a simple class with English and Spanish fields. I could have called them Question and Answer but this way you can ask the Spanish phrase to be translated to English.
Code:
Public Class Question
Public Property English As String
Public Property Spanish As String
Public Sub New(english As String, spanish As String)
_English = english
_Spanish = spanish
End Sub
End Class
Here I'm just populating a List(Of Question) with test data like English: English 0 Spanish: Spanish 0 just so you can easily see that 5 random questions have been picked.
Code:
Public Class Form1
Private translationList As List(Of Question)
Private randomQuestionsList As List(Of Question)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
translationList = PopulateQuestions()
randomQuestionsList = GetRandomQuestions(translationList, 5)
End Sub
Private Shared Function PopulateQuestions() As List(Of Question)
Return Enumerable.Range(0, 10).Select(Function(i) New Question("English " & i, "Spanish " & i)).ToList()
End Function
Private Shared Function GetRandomQuestions(questionList As List(Of Question), numberOfQuestions As Integer) As List(Of Question)
Dim rnd As New Random()
Return questionList.OrderBy(Function() rnd.Next).Take(numberOfQuestions).ToList()
End Function
End Class
-
Mar 9th, 2012, 11:47 AM
#5
Thread Starter
Member
Re: Link items in List
 Originally Posted by kevininstructor
Code:
Dim Current = MyQuestions.Item(MyRandomNumber)
Console.WriteLine("Question: {0}", Current.Text)
Console.WriteLine("Answer: {0}", Current.Response)
Comes up with the error saying it is not declared. This is my code:
Code:
Public Class Form1
Dim mark = 0
Dim time = 0
Dim questionlist As New List(Of String)
Dim answerlist As New List(Of String)
Private MyQuestions As New List(Of Question)
Public Class Question
Public Property Identifier As Int32
Public Property Text As String
Public Property Responce As String
Public Sub New()
End Sub
End Class
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Current = MyQuestions.Item(MyRandomNumber)
MyQuestions.Add(
New Question With
{
.Identifier = 1,
.Text = "When shall we meet?",
.Responce = "A que hora nos vemos?"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 2,
.Text = "Let's meet at eight",
.Responce = "Nos vemos a las ocho"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 3,
.Text = "Shall we meet in front of the bar?",
.Responce = "Nos vemos delante del bar?"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 4,
.Text = "Where are you going to go at the weekend?",
.Responce = "Adonde vas a ir el fin de semana?"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 5,
.Text = "Who are you going to go with?",
.Responce = "Con quien vas a ir?"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 6,
.Text = "Where shall we meet?",
.Responce = "Donde nos vemos?"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 7,
.Text = "I'm going to go to Granada with my friend",
.Responce = "Voy a ir a Grandada con mi amiga"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 8,
.Text = "When are you going to leave?",
.Responce = "Cuando vas a salir?"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 9,
.Text = "I'm going to leave on Saturday at eight",
.Responce = "Voy a salir el sabado a las ocho"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 10,
.Text = "How long are you going to stay?",
.Responce = "Cuanto tiempo vas a quedarte"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 11,
.Text = "I'm going to stay two nights",
.Responce = "Voy a quedarme dos noches"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 12,
.Text = "What are you going to do?",
.Responce = "Que vas a hacer?"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 13,
.Text = "I'm going to visit some typical villages",
.Responce = "Voy a visitar pueblos tipicos"
}
)
Console.WriteLine("Question: {0}" & Current.Text)
Console.WriteLine("Question: {0}" & Current.Responce)
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
time = time + 1
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1.Enabled = False
TextBox1.Enabled = False
ProgressBar1.Value = ProgressBar1.Value + 1
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
If mark >= 5 Then
MsgBox("Your Results:" & vbNewLine & "You got " & mark & " out of 5" & vbNewLine & "You took " & time & " seconds to finish, which is " & (Math.Round(time / 60, 2)) & " mins.", MsgBoxStyle.Information, "Your Results")
Button8.Enabled = True
Else
MsgBox("Your Results:" & vbNewLine & "You got " & mark & " out of 5" & vbNewLine & "You took " & time & " seconds to finish, which is " & (Math.Round(time / 60, 2)) & " mins." & vbNewLine & vbNewLine & "Because you mark was less than 4 out of 5, you will need to re-take this test. Press OK to close this message, then click Start Test to retry.", MsgBoxStyle.Information, "Your Results")
End If
End Sub
End Class
-
Mar 9th, 2012, 11:53 AM
#6
Re: Link items in List
So declare it. It should be fairly obvious what MyRandomNumber represents.
-
Mar 9th, 2012, 11:55 AM
#7
Re: Link items in List
 Originally Posted by benedict3578
Comes up with the error saying it is not declared. This is my code:
Code:
Public Class Form1
Dim mark = 0
Dim time = 0
Dim questionlist As New List(Of String)
Dim answerlist As New List(Of String)
Private MyQuestions As New List(Of Question)
Public Class Question
Public Property Identifier As Int32
Public Property Text As String
Public Property Responce As String
Public Sub New()
End Sub
End Class
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Current = MyQuestions.Item(MyRandomNumber)
MyQuestions.Add(
New Question With
{
.Identifier = 1,
.Text = "When shall we meet?",
.Responce = "A que hora nos vemos?"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 2,
.Text = "Let's meet at eight",
.Responce = "Nos vemos a las ocho"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 3,
.Text = "Shall we meet in front of the bar?",
.Responce = "Nos vemos delante del bar?"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 4,
.Text = "Where are you going to go at the weekend?",
.Responce = "Adonde vas a ir el fin de semana?"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 5,
.Text = "Who are you going to go with?",
.Responce = "Con quien vas a ir?"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 6,
.Text = "Where shall we meet?",
.Responce = "Donde nos vemos?"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 7,
.Text = "I'm going to go to Granada with my friend",
.Responce = "Voy a ir a Grandada con mi amiga"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 8,
.Text = "When are you going to leave?",
.Responce = "Cuando vas a salir?"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 9,
.Text = "I'm going to leave on Saturday at eight",
.Responce = "Voy a salir el sabado a las ocho"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 10,
.Text = "How long are you going to stay?",
.Responce = "Cuanto tiempo vas a quedarte"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 11,
.Text = "I'm going to stay two nights",
.Responce = "Voy a quedarme dos noches"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 12,
.Text = "What are you going to do?",
.Responce = "Que vas a hacer?"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 13,
.Text = "I'm going to visit some typical villages",
.Responce = "Voy a visitar pueblos tipicos"
}
)
Console.WriteLine("Question: {0}" & Current.Text)
Console.WriteLine("Question: {0}" & Current.Responce)
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
time = time + 1
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Button1.Enabled = False
TextBox1.Enabled = False
ProgressBar1.Value = ProgressBar1.Value + 1
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
If mark >= 5 Then
MsgBox("Your Results:" & vbNewLine & "You got " & mark & " out of 5" & vbNewLine & "You took " & time & " seconds to finish, which is " & (Math.Round(time / 60, 2)) & " mins.", MsgBoxStyle.Information, "Your Results")
Button8.Enabled = True
Else
MsgBox("Your Results:" & vbNewLine & "You got " & mark & " out of 5" & vbNewLine & "You took " & time & " seconds to finish, which is " & (Math.Round(time / 60, 2)) & " mins." & vbNewLine & vbNewLine & "Because you mark was less than 4 out of 5, you will need to re-take this test. Press OK to close this message, then click Start Test to retry.", MsgBoxStyle.Information, "Your Results")
End If
End Sub
End Class
I am on a different computer from yesterday, took the code I posted and created a new console app, ran it and it works as expected.
Code:
Module Module1
Private MyQuestions As New List(Of Question)
Sub Main()
LoadItems()
Dim Current = MyQuestions.Item(MyRandomNumber)
Console.WriteLine("Question: {0}", Current.Text)
Console.WriteLine("Answer: {0}", Current.Response)
Console.WriteLine("press any key to exit...")
Console.ReadLine()
End Sub
''' <summary>
''' Mocked function to simulate a random number but is static
''' </summary>
''' <returns></returns>
''' <remarks>
''' </remarks>
Public Function MyRandomNumber() As Integer
Return 2
End Function
Private Sub LoadItems()
MyQuestions.Add(
New Question With
{
.Identifier = 1,
.Text = "Question 1",
.Response = "Answer for question 1"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 2,
.Text = "Question 2",
.Response = "Answer to middle questions"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 3,
.Text = "Question 3",
.Response = "Answer to last question"
}
)
End Sub
Public Class Question
Public Property Identifier As Int32
Public Property Text As String
Public Property Response As String
Public Sub New()
End Sub
End Class
End Module
-
Mar 9th, 2012, 12:13 PM
#8
Thread Starter
Member
Re: Link items in List
 Originally Posted by kevininstructor
I am on a different computer from yesterday, took the code I posted and created a new console app, ran it and it works as expected.
Code:
Module Module1
Private MyQuestions As New List(Of Question)
Sub Main()
LoadItems()
Dim Current = MyQuestions.Item(MyRandomNumber)
Console.WriteLine("Question: {0}", Current.Text)
Console.WriteLine("Answer: {0}", Current.Response)
Console.WriteLine("press any key to exit...")
Console.ReadLine()
End Sub
''' <summary>
''' Mocked function to simulate a random number but is static
''' </summary>
''' <returns></returns>
''' <remarks>
''' </remarks>
Public Function MyRandomNumber() As Integer
Return 2
End Function
Private Sub LoadItems()
MyQuestions.Add(
New Question With
{
.Identifier = 1,
.Text = "Question 1",
.Response = "Answer for question 1"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 2,
.Text = "Question 2",
.Response = "Answer to middle questions"
}
)
MyQuestions.Add(
New Question With
{
.Identifier = 3,
.Text = "Question 3",
.Response = "Answer to last question"
}
)
End Sub
Public Class Question
Public Property Identifier As Int32
Public Property Text As String
Public Property Response As String
Public Sub New()
End Sub
End Class
End Module
I tried it and ran it and it always comes out with question 3!
-
Mar 9th, 2012, 12:16 PM
#9
Re: Link items in List
 Originally Posted by benedict3578
I tried it and ran it and it always comes out with question 3!
I know that but you need to know why it is always returning question 3 and the only way for you to do this is to study the code.
-
Mar 9th, 2012, 12:18 PM
#10
Thread Starter
Member
Re: Link items in List
Can you please explain how to do it so that when the form loads it displays a random question in a textbox, because I can't do it at all.
-
Mar 9th, 2012, 12:56 PM
#11
Thread Starter
Member
Re: Link items in List
 Originally Posted by kevininstructor
I know that but you need to know why it is always returning question 3 and the only way for you to do this is to study the code.
I worked out why and it now generates a new question each time but how do it get it to check if the answer is correct when I press a button.
-
Mar 9th, 2012, 01:40 PM
#12
Re: Link items in List
 Originally Posted by benedict3578
I worked out why and it now generates a new question each time but how do it get it to check if the answer is correct when I press a button.
Having the question means you have the answer (in the property Response). Compare the value the user typed in to the Response property.
So if the user answer is say in TextBox1 you would test it like this where Current (figure 1) Response. Now let's consider another factor, if you do it this way it is possible the user types in the correct answer but the casing of text is not correct so it would be wise to make both the known answer and response the same case, I would use lower casing (see figure 2).
Code:
If TextBox1.Text = Current.Response Then
Figure 1 (see my prior reply)
Code:
Dim Current = MyQuestions.Item(MyRandomNumber)
Console.WriteLine("Question: {0}", Current.Text)
Console.WriteLine("Answer: {0}", Current.Response)
Figure 2
With that said you have things to work out. In hind-sight a better method would be to (if acceptable) have multiple choices to select rather than having the user type in the information. This of course would change the class Question to accept multiple choices and a property indicating the proper answer. I would also consider storing this information in a xml or backend database.
Tags for this Thread
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
|