Superscripts in VB

Good Day Friends,


I was working on how to calculate the position of students in a class and i got stocked. I did some research on line and i was unable to get the position of student so I left the code for three days. On the third day, i had a good rest and when i went back to my code, i decided to create a public sub, pass a variable to the sub that will determine the position and use the length of the position to get the superscripts .

Below is a segment of my code in VB

 Public Sub GetSuffix(position As String)
        Dim suffix As String = "th"

        If Integer.Parse(position) < 11 OrElse Integer.Parse(position) > 20 Then
            position = position.ToCharArray()(position.ToCharArray().Length - 1).ToString()
            Select Case position
                Case "1"
                    suffix = "st"
                    Exit Select
                Case "2"
                    suffix = "nd"
                    Exit Select
                Case "3"
                    suffix = "rd"
                    Exit Select
            End Select
        End If

        SuffixTXT.Text = suffix

    End Sub


Explanation:
  1. Position is a variable declared as integer, converted to string and passed to the public sub.
  2. suffix is a variable declared to have a predefined superscripts . If condition are not met, then then suffix will use the predefined superscripts 
  3. Position is parse as integer and a check is done to determine if it is less than 11 or greater than 20
  4. Position is converted to character of array to get length and 1 is subtracted from the length which will be used on my loop
  5. Cass Statement loop on the length of position that end with either 1,2 and 3, the suffix will change to the appropriate superscripts but if not within the loop then the suffix will retain its predefined superscripts 
  6. The predefined superscripts will be passed to either a label or textbox or global variable.
  7. Thanks

Comments

Popular posts from this blog

Xara Web Designer Easily create your own website

Tips for Better Database Design