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.

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 6
March 16, 2010
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

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.

Conditional Visual Basic Statements to Check Answers

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:

        If Answer1 = "round" Then

            Point1 = 3

        Else

            Point1 = 0

        End If

        If Answer2 = "15" Then

            Point2 = 3

        Else

            Point2 = 0

        End If

        If Answer3 = "15" Then

            Point3 = 3

        Else

            Point3 = 0

        End If

        If Answer4 = "12" Then

            Point4 = 3

        Else

            Point4 = 0

        End If

        If Answer5 = "1" Then

            Point5 = 3

        Else

            Point5 = 0

        End If

        If Answer6 = 13 Then

            Point6 = 3

        Else

            Point6 = 0

        End If

        If Answer7 = "cnn" Then

            Point7 = 4

        Else

            Point7 = 0

        End If

        If Answer8 = "1980" Then

            Point8 = 4

        Else

            Point8 = 0

        End If

        If Answer9 = "vlookup" Then

            Point9 = 4

        Else

            Point9 = 0

        End If

        If Answer10 = "360000" Then

            Point10 = 30

        Else

            Point10 = 0

        End If

Computation of mental age, IQ and displaying results on the browser

The mental age is the sum of all scores (Point1 to Point10). In Visual Basic it is expressed as:

mentalage = Point1 + Point2 + Point3 + Point4 + Point5 + Point6 + Point7 + Point8 + Point9 + Point10

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).

Complete Default.aspx.vb Source Code

Below is the complete source code as discussed in the previous sections:

Partial Class _Default

    Inherits System.Web.UI.Page

 

    Protected Sub submitbutton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submitbutton.Click

        'Grab user inputs and assign to a variable in Visual Basic.

        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

        'Assign scores depending if the user got the correct answer or not

        If Answer1 = "round" Then

            Point1 = 3

        Else

            Point1 = 0

        End If

        If Answer2 = "15" Then

            Point2 = 3

        Else

            Point2 = 0

        End If

        If Answer3 = "15" Then

            Point3 = 3

        Else

            Point3 = 0

        End If

        If Answer4 = "12" Then

            Point4 = 3

        Else

            Point4 = 0

        End If

        If Answer5 = "1" Then

            Point5 = 3

        Else

            Point5 = 0

        End If

        If Answer6 = 13 Then

            Point6 = 3

        Else

            Point6 = 0

        End If

        If Answer7 = "cnn" Then

            Point7 = 4

        Else

            Point7 = 0

        End If

        If Answer8 = "1980" Then

            Point8 = 4

        Else

            Point8 = 0

        End If

        If Answer9 = "vlookup" Then

            Point9 = 4

        Else

            Point9 = 0

        End If

        If Answer10 = "360000" Then

            Point10 = 30

        Else

            Point10 = 0

        End If

        'Compute mental age based on scores

        mentalage = Point1 + Point2 + Point3 + Point4 + Point5 + Point6 + Point7 + Point8 + Point9 + Point10

        '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.

blog comments powered by Disqus
ASP.NET ARTICLES

- Implementing ASP.NET 4.0 Page.MetaDescriptio...
- ASP.Net Development Tips
- Intro to Sessions in ASP.Net
- Google Maps API Introduction in ASP.NET usin...
- Creating an ASP.NET 3.5 Gridview Image Galle...
- Encrypt QueryString in ASP.NET 3.5 using VB....
- ASP.NET 3.5 Drop Down List Controls
- Connect to Access Database with ASP.Net
- Secure Audio Streaming with ASP.Net and Flash
- Dynamic Sitemap and Navigation in ASP.Net
- Implement Gzip and Deflate Compression in AS...
- Run ASP.Net in Ubuntu with Apache
- ASP.Net Mono Website Contact Forms
- ASP.Net URL Rewriting Methods
- Murach`s ASP.NET 4 Web Programming with C# 2...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials