Solved

Case statement fails case when it is not supposed to

Posted on 2004-09-13
15
186 Views
Last Modified: 2010-04-23
I have setup a case statement and the statement is bypassed when it is not supposed to be bypassed.  I have checked the data I am compairing for and it does meet the criteria.  When I debug and step through the code, it hits Case grade=9 and then steps to the case grade = 10.  The actual data is grade="9" when I look at the local variables.  I have tried useing the val command to convert it to a value but this did not help.  Can someone point out what I may be missing.  Here is the code.

Thanks
Kenny


'Calculate Objective Scores Totals

            Select Case grade

Case grade = "9"

                    readingEla9AvailableObj1 = 9
                    readingEla9AvailableObj2 = 15
                    readingEla9AvailableObj3 = 18
                    readingEla9TotalAvailable = 42

                    math9AvailableObj1 = 5
                    math9AvailableObj2 = 5
                    math9AvailableObj3 = 5
                    math9AvailableObj4 = 5
                    math9AvailableObj5 = 4
                    math9AvailableObj6 = 4
                    math9AvailableObj7 = 4
                    math9AvailableObj8 = 6
                    math9AvailableObj9 = 5
                    math9AvailableObj10 = 9
                    math9TotalAvailable = 52

                    readingElaObj91ObjectiveGrade = CalculatePF(readingEla9AvailableObj1, readingElaObjScoresArray(1))
                    readingElaObj92ObjectiveGrade = CalculatePF(readingEla9AvailableObj2, readingElaObjScoresArray(2))
                    readingElaObj93ObjectiveGrade = CalculatePF(readingEla9AvailableObj3, readingElaObjScoresArray(3))

                    MathObj91ObjectiveGrade = CalculatePF(math9AvailableObj1, mathObjScoresArray(1))
                    MathObj92ObjectiveGrade = CalculatePF(math9AvailableObj2, mathObjScoresArray(2))
                    MathObj93ObjectiveGrade = CalculatePF(math9AvailableObj3, mathObjScoresArray(3))
                    MathObj94ObjectiveGrade = CalculatePF(math9AvailableObj4, mathObjScoresArray(4))
                    MathObj95ObjectiveGrade = CalculatePF(math9AvailableObj5, mathObjScoresArray(5))
                    MathObj96ObjectiveGrade = CalculatePF(math9AvailableObj6, mathObjScoresArray(6))
                    MathObj97ObjectiveGrade = CalculatePF(math9AvailableObj1, mathObjScoresArray(7))
                    MathObj98ObjectiveGrade = CalculatePF(math9AvailableObj2, mathObjScoresArray(8))
                    MathObj99ObjectiveGrade = CalculatePF(math9AvailableObj3, mathObjScoresArray(9))
                    MathObj910ObjectiveGrade = CalculatePF(math9AvailableObj4, mathObjScoresArray(10))

                    Try
                        Dim objConnection6 As SqlConnection = New SqlConnection("server=(local);database=FBISD-TAKS;user id=sa;password=pa55word")
                        Dim objCommand6 As SqlCommand = New SqlCommand
                        objCommand6.Connection = objConnection6
                        objCommand6.CommandText = "Insert Into objectivesCorrect " & "(admDate) " & _
                                "VALUES(@oadmDate)"
                        objCommand6.Parameters.Add("@oadmDate", SqlDbType.DateTime, 10).Value = madmDate
                        objCommand6.Parameters.Add("@ostudentId", SqlDbType.Int, 10).Value = studentId
                        objCommand6.Parameters.Add("@oteacherId", SqlDbType.Int, 8).Value = teacher1
                        objConnection6.Open()
                        Dim kenny As String
                        kenny = objCommand6.CommandText
                        MsgBox(kenny)
                        objCommand6.ExecuteNonQuery().ToString()
                        objConnection6.Close()

                    Catch ex As Exception
                        MsgBox(ex.ToString())

                    End Try




                Case grade = 10
                    readingEla10AvailableObj1 = 8
                    readingEla10AvailableObj2 = 11
                    readingEla10AvailableObj3 = 18
                    readingEla10AvailableObj6 = 20
                    readingEla10TotalAvailable = 57

                    math10AvailableObj1 = 5

                    ..................etc



               End Select

0
Comment
Question by:kwh3856
  • 8
  • 7
15 Comments
 
LVL 8

Expert Comment

by:wguerram
ID: 12047554
Try placing a msgbox just before the Select case

Msgbox (grade)

if the variable is numeric

Msgbox (grade.tostring)

just to see what is the value.

How do you obtain the value for the grade variable?
can you post the code?
0
 

Author Comment

by:kwh3856
ID: 12047608
I will try that and let you know.  Here is the code where I get the grade.

objStreamReader = New StreamReader("C:\fbisd\t178914a.dat")
        strLine = objStreamReader.ReadLine
        Do While Not strLine Is Nothing
            admDate = Mid(strLine, 1, 4)
            admDate = admDate.Substring(0, 2) & "/01/20" & admDate.Substring(2)
            madmDate = admDate
            grade = Val(Mid(strLine, 5, 2))


0
 
LVL 8

Expert Comment

by:wguerram
ID: 12047644
What type of value is the variable grade?
0
 

Author Comment

by:kwh3856
ID: 12047645
The message box showed a value of 9.  It was not in quotes. Yet when you look at the local varibles for grade...it show it in quotes
0
 

Author Comment

by:kwh3856
ID: 12047663
I read the grade in as a string variable and then covert it to a numeric value.  It is actually the grade level a student is currently in.
0
 
LVL 8

Expert Comment

by:wguerram
ID: 12047684
Are you declaring grade variable or not before using it?

like:

dim grade as string

or
 dim grade as integer

?
0
 

Author Comment

by:kwh3856
ID: 12047696
I dim the grade as string originally
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:kwh3856
ID: 12047703
here is the code

 Dim objStreamReader As StreamReader
        Dim strLine As String
        Dim myConnString As String
        Dim admDate As String
        Dim madmDate As DateTime
        Dim grade As String
0
 
LVL 8

Expert Comment

by:wguerram
ID: 12047718
Try adding a trim, in the select case

 Select Case grade.trim
     Case grade = "9"
         .
         .
     Case grade = "10"
0
 
LVL 8

Expert Comment

by:wguerram
ID: 12047769
You don't need to convert the value to numeric

grade = Val(Mid(strLine, 5, 2))

just leave it like

grade = Mid(strLine, 5, 2)
0
 
LVL 8

Accepted Solution

by:
wguerram earned 500 total points
ID: 12047786
Did you try the trim?

if so and it didn't work try this:

initialize the variable to 9 just before the select case and try it, just to see if you are having other problem.

like:

grade = "9"

 Select Case grade
     Case grade = "9"
         .
         .
     Case grade = "10"
0
 

Author Comment

by:kwh3856
ID: 12047864
That did not work either.  I tried the opposite with no success.  Here is how my code currently sits.

Sub Main()
        Dim objStreamReader As StreamReader
        Dim strLine As String
        Dim myConnString As String
        Dim admDate As String
        Dim madmDate As DateTime
        Dim grade As String
        Dim cvtgrade As Int16


objStreamReader = New StreamReader("C:\fbisd\t178914a.dat")
        strLine = objStreamReader.ReadLine
        Do While Not strLine Is Nothing
            admDate = Mid(strLine, 1, 4)
            admDate = admDate.Substring(0, 2) & "/01/20" & admDate.Substring(2)
            madmDate = admDate
            grade = Mid(strLine, 5, 2)
            cvtgrade = Val(grade)


            cvtgrade = 9

            'Calculate Objective Scores Totals
            MsgBox(cvtgrade)

            Select Case cvtgrade

                Case cvtgrade = 3
                    readingEla3AvailableObj1 = 15
                    readingEla3AvailableObj2 = 7
                    readingEla3AvailableObj3 = 6
                    readingEla3AvailableObj4 = 8
                    readingEla3TotalAvailable = 36
                   .......................etc

                Case cvtgrade = 9 --------------------------------------------Fails test and skips to Case cvtgrade = 10
                    readingEla9AvailableObj1 = 9
                    readingEla9AvailableObj2 = 15
                    readingEla9AvailableObj3 = 18
                    readingEla9TotalAvailable = 42


         EndSelect
0
 

Author Comment

by:kwh3856
ID: 12047975
I got it!!!!!!!!!!

Here was the problem.............do not include the variable...just tell it the value you are looking for

Case 9

                    readingEla9AvailableObj1 = 9
                    readingEla9AvailableObj2 = 15
                    readingEla9AvailableObj3 = 18
                    readingEla9TotalAvailable = 42
0
 

Author Comment

by:kwh3856
ID: 12047990
wguerram

Thanks for your help.  You definately got me going in the right direction..

Thanks
Kenny
0
 
LVL 8

Expert Comment

by:wguerram
ID: 12048061
Oh, yes you are right.

i didn't see the difference

look

 Dim x As String

        x = Val("9 ")

        Select Case x.Trim
            Case "9"
                MsgBox("9")
            Case 10
                MsgBox("10")
        End Select

i was making test and wasn't using the variable in the case.

It's great to know you solve your problem.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now