Multiple parameters for a method (only one to be used)

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.

Why not simply pass a string (textfield1.text for example) as a parameter? Would make your life easier.

Function IsEmpty(MyItem as Control) As Boolean

if MyItem isa TextField then

// TextField
dim item as TextField = MyItem
if item.Text.len = 0 then
  return true
else
  return false
end if

// PopupMenu

elseif MyItem isa PopupMenu then

dim item as PopupMenu = MyItem
if item.Text.len = 0 then
  return true
else
  return false
end if

// ComboBox

elseif MyItem isa ComboBox then

dim item as ComboBox = MyItem
if item.Text.len = 0 then
  return true
else
  return false
end if

end if
End Function

Sure, but I like to know the answer to ease my curious mind.

[quote=81466:@Horst Jehle]Function IsEmpty(MyItem as Control) As Boolean

if MyItem isa TextField then

// TextField
dim item as TextField = MyItem
if item.Text.len = 0 then
  return true
else
  return false
end if

// PopupMenu

elseif MyItem isa PopupMenu then

dim item as PopupMenu = MyItem
if item.Text.len = 0 then
  return true
else
  return false
end if

// ComboBox

elseif MyItem isa ComboBox then

dim item as ComboBox = MyItem
if item.Text.len = 0 then
  return true
else
  return false
end if

end if
End Function[/quote]

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=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)

Sorry. Here is the correct way

Function IsEmpty(MyItem as Variant) As Boolean

if MyItem isA TextField then

// TextField
dim item as TextField
item = MyItem
if item.Text.len = 0 then
  return true
else
  return false
end if

end if
End Function

You can also use select case and isA

Select Case Me
Case IsA PushButton
MsgBox “I’m a pushbutton.”
Case IsA TextField
MsgBox “Nope!”
End Select

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.

Or use this project for highlighting a TextField

Or, add such method to a module:

[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 can avoid the Dim statements and make everything shorter and faster by using extends (See example above).

For instance it can be used like this:

MsgBox If(TextField1.isempty,"Textfield is empty", "TextField is not empty")

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.

Type mismatch error. Expected TextField, but got RectControl dim RecognizedItem as TextField = UnknownItem

[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.

Hm, I can’t edit my post for some reason… Anyway, I wanted to add one more thing…

[code]Function IsEmpty(Extends value As String) As Boolean
Return (value.Len = 0)
End Function

Function IsEmpty(Extends w As TextBox) As Boolean
Return w.Text.IsEmpty
End Function

Function IsEmpty(Extends w As PopupMenu) As Boolean
Return w.Text.IsEmpty
End Function

// etc
[/code]

This gives you very simple maintenance as well as the added benefit of testing your strings in other areas easily.

For those wanting to know… what Jeremy described is called OVERLOADING, a very powerful and often misunderstood feature of many programming languages

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