StrataFrame Forum

Non-AppFrame Question

http://forum.strataframe.net/Topic6261.aspx

By Ben Kim - 1/25/2007

Also, since I am new to VB.NET and coming from Clarion, is there a way to check a single field for specific values instead of a long IF statement or Select...Case?

Example in Clarion:

IF INLIST(MyField, "Male", "Female", "Both")
    MESSAGE("Valid")
ELSE
    MESSAGE("INVALID")
END

In Clarion I could test for multiple values with a single statement as shown above (I believe up to 25 values at a time).  Is there a similar statement in VB.NET?  Currently I have code like:

IF MyField <> "Male" AND MyField <> "Female" AND MyField <> "Both"
...
END

Thank you for any ideas!

Ben

By Greg McGuffey - 1/25/2007

Ben,



Did you know there was a general .NET forum here? Click on the home link (under your name above the Add Post, Reply buttons) and down at the bottom you'll see a .NET forum. Its just for this sort of thing. The SF developers don't care much were you post it, as they get emails for new posts (based on what Ben Chase has said), but for us normal uses, it helps Hehe



I'm really curious to know if this can happen in VB.NET, BTW BigGrin
By Trent L. Taylor - 1/25/2007

There is not an INLIST method in VB.NET.  Generally when I have a situation like this, I would not use a String field.  This would be a integer field then I would create an Enum for each of the values.  You can then do a bitwise test depending upon how you create the enum. 
By Ben Kim - 1/26/2007

Thanks Trent.  Unfortunately we have to support a legacy database that cannot be changed.

Ben

By Trent L. Taylor - 1/26/2007

No fun.  You could easily create this type of functionality by creating a shared class and a collection.

Public Sub NotInheritable MyCommonTools
    Public Shared Function InList(ByVal TestValue as String, ParamArray TestItems() As String) As Boolean
       '-- Cycle through and test the values here and return out
    End Sub
End Sub
By Ben Kim - 1/29/2007

Trent,

Thank you for your help.  I implemented the function in a common "module.vb" file and it works like a treat.  Here is the code if anyone else wants to implement the feature:

Module CommonTools

Public Function InList(ByVal TestValue As String, ByVal ParamArray TestItems() As String) As Boolean

Dim lLoop As Integer

Dim lResult As Boolean

lResult = False

For lLoop = LBound(TestItems) To UBound(TestItems)

If TestValue.Trim = TestItems(lLoop).Trim Then

lResult = True

Exit For

End If

Next

Return lResult

End Function

End Module

Here is an example on how to use the InList function:


If Not InList(Me.EventType, "FULL", "ACCD", "CIT", "PARK", "FI") Then
    Me.AddBrokenRule(IncdTypeBOFieldNames.PathOf, "Please choose a event type.")
End If

Thanks again!

Ben

By Trent L. Taylor - 1/29/2007

It looks great.  The only suggestion I would make would be to replace the LBound and UBound functions as these are left in at the moment for backward compatability.  The array has all of this information already within it. 

For Each lcItem In TestItems
Next

or

For lnIndex = 0 To TestItems.Length-1
Next

Otherwise I think it looks great!

 

By Steve L. Taylor - 1/29/2007

Ben another version:


''' <summary>
''' Accepts an expression and a list of values.
''' <example>
''' Inlist("FULL", "FULL", "ACCD", "CIT", "PARK", "FI") ' Returns true
''' InList(456, 23, 456, 44, 357) ' Returns true
''' </example>
''' </summary>
''' <param name="Expression"></param>
''' <param name="Items"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function InList(ByVal Expression As Object, ByVal ParamArray Items() As Object) As Boolean
     Return Array.IndexOf(Items, Expression) > -1
End Function
By Ben Kim - 1/29/2007

Would I just Dim lcItem per your example as follows?

Dim lcItem As String

Sorry for the newbie questions!

Ben

By Trent L. Taylor - 1/29/2007

Yes.  You can also do it inline.

For Each lcItem As String IN TestItems
Next

Either case will work.  Steve's example is also a very good idea as well if you don't need to do any internal processing.  The only problem with using the IndexOf may be case-sensitivity.  Otherwise it is definitely the quickest route.

By Ben Kim - 1/29/2007

Also...

In the BeforeSave event method, I cannot for the life of me figure out what the code should be to fire each control's validate method.  Here is where I am:

Dim lcItem As Control
For Each lcItem In Me.Controls
lcItem.?????
Next

Can you guide me the rest of the way?

Thanks!