Object or Variant or ?

I want to create a method that is common to both a desktop and a Web app. It needs to receive as a parameter either a ListBox (from within a desktop app) or a WebListBox (from within a WebApp). Obviously I cannot pass it as ListBox/WebListBox else it doesn’t compile.

I have tried passing it as a Variant and as an Object, but seem unable to access it within the method. Inside the method I have:

[code]#if targetdesktop …
Dim tempListBox As ListBox
If listObject IsA ListBox then
tempListBox = listObject 'this is the error
… do stuff with tempListBox
end if
#elseif targetweb …

so the code doesn’t clash within the method.

I then tried to Dim a new ListBox/WebListBox within each #target area, but seem unable to assign the passed Object to the newly Dimmed variable. What is the right way to do this kind of thing?!

If you’re passing it as Object, then you need to CAST it to the correct type. (Read up on casting in the Users Guide.)

tempListBox = ListBox(listObject)

I would create two methods and mark one for desktop and the other for web. Look at the second page of the Inspector.

To extend Kem’s suggestion, you might want to look into overloading:


Effectively, two methods with the same name but different parameters.
Xojo will call the right one based on what you pass as a parameter

In the end I used Tim’s technique and it works:

I passed the ‘listbox’ (ListBox or WebListBox) as a parameter myWebListBox As Object.
Then divided the code into:
#if TargetDesktop then
#elseif TargetWeb then

Within each of the two sections I created a new ListBox (or WebListBox) then worked on that e.g.

Dim tempListBox As WebListBox if myWebListBox IsA WebListBox then tempListBox = WebListBox(myWebListBox) … 'here is my code to work on tempListBox … end if

I now have one method for both desktop and web apps, but I have duplicate code within this method.