简体   繁体   中英

Comparing two non-numeric values, selecting lower in ASP.NET/VB.NET

I am updating an application I've written used by my employer, a University, to allow students to register for their desired residence hall.

I'm working on a new feature that will allow students to "partner" with another student - so that when one or the other registers for a room, the other student will be registered as well.

Now, let us say we have John Doe and James Doe - and they partner with each other for room registration. The time when students is staggered based on their current class level. I need a way to determine (without a billion IF..THEN statements) which student has the lower class level. The values for class level are FR, SO, JR, SR, 5SR (from most junior to most senior).

In the above example, John Doe might be a FR while James Doe may be a SR. Neither James nor John should be allowed to register until FR's are allowed to register - since that is the partnerships "base" class level.

Essentially I want to do something like:

IF John_Doe_Class_Level < James_Doe_Class_Level Then
   Partnership_Class_Level = John_Doe_Class_Level
Else
   Partnernship_Class_Level = James_Doe_Class_Level
End If

Any idea how to accomplish this effectively?

Where do you define your class levels? If it is in the code, use enums with associated numeric values.

Public Enum ClassLevel As Integer
    FR = 0
    SO = 1
    JR = 2
End Enum

Public Class Student
    Public Property Name As String
    Public Property Level As ClassLevel
End Class

Public Sub TestIt(ByVal studentA As Student, ByVal studentB As Student)
    If studentA.Level > studentB.Level Then
        ' dostuff
    End If
End Sub

I know I'm clearly missing something, but if you assign these class levels to an enum, why wouldn't the above work?


public enum ClassLevel
{
  FR=1, 
  SO=2, 
  JR=2,
  SR=3,  
  5SR=4
}

etc

IF John_Doe.ClassLevel < JamesDoe.Class_Level Then
   Partnership.Class_Level = John_Doe.ClassLevel
Else
   Partnernship.Class_Level = James_Doe.ClassLevel
End If

I would run the values through function that returns a number then compare those values to each other. Something like...

IF numericClassLevel(John_Doe_Class_Level) < numericClassLevel(John_Doe_Class_Level) Then   
    Partnership_Class_Level = John_Doe_Class_Level
Else
    Partnernship_Class_Level = James_Doe_Class_Level
End If

public function numericClassLevel(strClassLevel) as integer
    select case strLevel
        case "FR"
            return 1
        etc.
    end select
end function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM