I want to define a method called Empty, which returns a boolean value based on if the given parameter (text property of various controls) is empty or not.
[code]
Empty(item as TextField) as Boolean
##########################
If item.Text = “” THEN
return TRUE
ELSE
return FALSE
END IF[/code]
The given parameter is a TextField, but I do want this method to work on ANY control which has a “text” property, like PopupMenu, ComboBox, etc.
I tried to define item as RectControl, but it turned out RectControl doesn’t have a text property.
Please tell me how to give multiple choices as parameters to a method, of which only ONE is going to be used.
[quote=81468:@Radium Radiovich]Sure, but I like to know the answer to ease my curious mind.
It results in 3 errors (also for PopupMenu and ComboBox):
Type mismatch error. Expected TextField, but got Control
dim item as TextField = [b]MyItem[/b][/quote]
You have to cast myItem to the specific control, like :
Dim item as text field = textfield(myitem)
The better way is to make a new control like myTextField and set its superclass to TextField. Add a Event Handler
“LostFocus”
if me.text.len = 0 then
msgbox me.name + “: Text is Empty”
// or
me.CueText = “This field can not be empty”
end if
[code]Function isempty(item as Control) As boolean
dim chain as string
if item isA TextField then
dim zitem as textfield = textfield(item)
chain = zitem.text
elseif item isA TextArea then
dim zitem as TextArea = textarea(item)
chain = zitem.text
elseif item isA ComboBox then
dim zitem as ComboBox = combobox(item)
chain = zitem.text
end if
if chain = “” then
return false
else
return true
end if
End Function
[/code]
This way if you pass a control that does not have a text property or is not listed (segmented control, popupmenu, etc. here) it raises no error and the method returns false.
[code]Function isempty(extends Ctl As Control) As Boolean
Select Case True
Case Ctl IsA TextField
Return (TextField(Ctl).Text="")
Case Ctl IsA TextArea
Return (TextArea(Ctl).Text="")
// Add more cases here
Case Else
Return False
End Select
End Function
[/code]
Wow, thank you guys! I didn’t expect so many responses in such a short time.
Here’s the code I finally used in the project (inspired by your codes):
FUNCTION IsEmpty(UnknownItem as Variant) as Boolean
SELECT CASE UnknownItem
CASE IsA TextField
dim RecognizedItem as TextField = UnknownItem
if RecognizedItem.text.Len = 0 THEN return TRUE
CASE IsA PopupMenu
dim RecognizedItem as PopupMenu = UnknownItem
if RecognizedItem.text.Len = 0 THEN return TRUE
CASE IsA ComboBox
dim RecognizedItem as ComboBox = UnknownItem
if RecognizedItem.text.Len = 0 THEN return TRUE
END SELECT
END FUNCTION
You should avoid the use of Variant wherever possible. The parameter should be RectControl instead, and none of your other code needs to change. This will also be faster since it doesn’t require a conversion the way Variant does.
[quote=81524:@Radium Radiovich]Type mismatch error. Expected TextField, but got RectControl
dim RecognizedItem as TextField = UnknownItem[/quote]
Like Oliver said, did you cast the item like this:
dim RecognizedItem as TextField = TextField(UnkownItem)
For superclasses and interfaces of the item you have to explicitly cast it to the data type.
My bad, I missed that. The variable has to be cast. For example:
SELECT CASE UnknownItem
CASE IsA TextField
dim RecognizedItem as TextField = TextField( UnknownItem )
if RecognizedItem.text.Len = 0 THEN return TRUE
...
You could also shorten that to:
SELECT CASE UnknownItem
CASE IsA TextField
if TextField( UnknownItem ).text.Len = 0 THEN return TRUE
Also, I’ve tested this in the past and comparing a string variable to “” is faster than checking Len. In fact, Len can be very, very slow if there is a lot of text in the field so, if anything, you should use LenB.
Another way of doing it is to make several methods such as:
[code]Function IsEmpty(Extends w As TextField) As Boolean
Return (w.Text = “”)
End Function
Function IsEmpty(Extends w As PopupMenu) As Boolean
Return (w.Text = “”)
End Function
// etc…
[/code]
I personally would do it that way, let the compiler figure out what to do instead of having your app make the decision logic at runtime. It also is very clean, easy to maintain.
Jeremy’s recommendation is the best one yet. However, let me stress again that Len should not be used here since it is very slow with large quantities of text. I’d rewrite it this way:
Function IsEmpty(Extends value As String) As Boolean
Return (value = "")
End Function