Hi a1x;
Here is a way to do it without a Case statements.
' Declear pointer to a function using Delegate
Delegate Function delFunction() As String
' Hash table to hold key / value pair
Dim ht As New Hashtable
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
' Load Hash table with the key and pointer to function to call
Dim func As delFunction
func = AddressOf FunctionABC
ht.Add("ABC", func)
func = AddressOf FunctionDEF
ht.Add("DEF", func)
End Sub
Then you can call them this way which is much faster then going through 100 case test.
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim deleg As delFunction
deleg = CType(ht("ABC"), delFunction)
Console.WriteLine(deleg())
deleg = CType(ht("DEF"), delFunction)
Console.WriteLine(deleg())
End Sub
Private Function FunctionABC() As String
Return "ABC"
End Function
Private Function FunctionDEF() As String
Return "DEF"
End Function
Fernando
Main Topics
Browse All Topics





by: JR2003Posted on 2006-06-22 at 16:12:48ID: 16964805
You could nest case statement and test the first character.
Select Case Left(sSwitch, 1)
Case "A"
Select Case sSwitch
Case "ABC"
Return FunctionABC
Case "ABD"
Return FunctionABD
End Select
Case "B"
Select Case sSwitch
Case "BCD"
Return FunctionBCD
Case "BDS"
Return FunctionBDS
End Select
Case "C"
Select Case sSwitch
Case "CDE"
Return FunctionCDE
Case "CDF"
Return FunctionCDF
End Select
...
End Select
That way you reduce the average number test for each item.