HomeASP.NET Coding an IQ Test with Conditionally Drive...
Coding an IQ Test with Conditionally Driven Event Handlers in ASP.NET 3.5
This is the second part of a tutorial series on developing conditionally-driven event handlers in ASP.NET 3.5. In part one, you started learning how to build an online IQ test with ASP.NET 3.5; the creation of web forms and setting of objectives were discussed. In this second part, we'll really sink our teeth into using conditionally-driven event handlers to make the test work.
Specifically, in this part you will learn how to create the Visual Basic program that will serve as a server side script to handle conditional decisions of the web application. A typical decision making task includes determining whether the user has the correct answer to the question and then assigning scores.
If you read part one and have fully created the web form (Default.aspx) in your own Visual Web Developer Express 2008, then let’s get started. All scripts mentioned in this article will be added to the Visual Basic file of Default.aspx in Visual Web Developer Express (Default.aspx.vb)
Variable Declaration and Grabbing of Form Inputs
Visual Basic is a “strong type” programming language. This means that you need to declare the type of all variables used in the script. In Visual Basic, the following are the important variable types and their application:
Also, the Visual Basic server side script cannot do anything without having form values to be processed. These act as inputs to the scripts, which will then be processed and output the IQ of the person to the web browser. In Visual Basic, to grab the user inputs from the web form which is submitted to the ASP.NET server, the following is the syntax:
Dim VisualBasicVariable As VisualBasicVariableType = FormIDused.Text
The bolded words are the ones that need to be changed depending on the variable name you used and the type you need to assign to each of those variables. FormIDused is the text box web control ID declared in Part 1, like test1, test2, etc.
To simplify the program, you will need to name those variables related to the form imputs as simply as possible. For example:
Dim Answer1 As Object = test1.Text
Dim Answer2 As Object = test2.Text
Dim Answer3 As Object = test3.Text
Dim Answer4 As Object = test4.Text
Dim Answer5 As Object = test5.Text
Dim Answer6 As Object = test6.Text
Dim Answer7 As Object = test7.Text
Dim Answer8 As Object = test8.Text
Dim Answer9 As Object = test9.Text
Dim Answer10 As Object = test10.Text
Dim Point1 As Integer
Dim Point2 As Integer
Dim Point3 As Integer
Dim Point4 As Integer
Dim Point5 As Integer
Dim Point6 As Integer
Dim Point7 As Integer
Dim Point8 As Integer
Dim Point9 As Integer
Dim Point10 As Integer
Dim chronologicalage As Integer = age.Text
Dim mentalage As Integer
Dim IQ As Decimal
The user's answers from the web form (variable Answer1 to Answer10) use the Object variable type because they are loosely defined (the answers can take the form of numbers, text, etc).
Dim Answer1 As Object = test1.Text
This above variable declaration line means “get the value of Textbox form ID name as test1 and then assign it to Answer1 as object.” So the answer for test question 1 using test1 form ID will be assigned as Answer1 in Visual Basic.
What about the score? The score assigned to Answer1 will simply be called Point1, and so on and so forth. The scoring system is as shown below:
Both correct and wrong answer points are integer values, therefore the variable type used in Visual Basic for those values is of the Integer type.
Chronological age and mental age also use the Integer type, since human age in years is commonly expressed as integers.
Lastly, the IQ can be expressed as a decimal, although during presentation of results back to the web browser, we can truncate or round off the values to be more user-friendly.
Like all programming languages, there are also conditional statements in Visual Basic, which is used as a server side scripting language in ASP.NET. The following is the syntax of these statements:
If Variable = value Then
Variablex = Valuex
Else
Variablex = Valuey
End If
In our online IQ test, the scoring system for easy questions awards three points if the user correctly answers a question.
For example, to check the answer of the first question, the following IF statements will be used:
If Answer1 = "round" Then
Point1 = 3
Else
Point1 = 0
End If
The above code snippet says that if Answer1 is equal to “round,” then assign a correct answer point total of 3; if it is wrong, assign 0 to Answer1.
The following are the if then else statements that check the answers provided for the test questions in Part 1:
Finally, the IQ can be computed in Visual Basic as:
IQ = (mentalage / chronologicalage) * 100
Once Visual Basic computes the IQ of the user, this information can be returned to the web browser. It will be displayed on the label web control with the ID of iqresult.
Here is the code:
iqresult.Text = "Thank you for taking the test.Your Intelligence Quotient is: <b>" & Fix(IQ) & "</b>"
Please note that the Fix() function returns the integer part of the IQ result (which can contain decimal values).
'Compute IQ based on mental age and persons chronological age
IQ = (mentalage / chronologicalage) * 100
'Output IQ back to the user browser
iqresult.Text = "Thank you for taking the test.Your Intelligence Quotient is: <b>" & Fix(IQ) & "</b>"
End Sub
End Class
Project Implementation
Copy and paste the source code above to Default.aspx.vb in your IQ Visual Web Developer Express project. Save all files and try launching it in the browser. It should look like the screen shot below:
Of course, the test is unofficial and is not guaranteed to be a "real" IQ test. Only registered school administrators or academic-based psychologists can create official IQ test questions.