Changing a class at runtime

Here’s an interesting one.
I have a page, where I load in some containers. Depending on if its desktop or mobile screen size I have 2 different containers.
card1 and card2 are webcontainers

if mobileversion then
dim c as new card1
else
dim c as new card2
end if

For obvious reasons this will not work as c will not be visible outside the loop , so i tried

dim c as varient
if mobileversion then
c=new card1
else
c= new card2
end if

This also failed so i tried this

dim c as card1

if mobileversion then
c.close
c=nil
c = new card2
end if

Still does not like it

Is there any way to do this, or am i missing something obvious?

With what message?
ps: Did you actually type varient?

assigning new card1 to a variant should have worked.
But to use it afterwards, you need to know what type it is

if c isa card1 then //do something card1y else //do something card2y end if

The do something part is 2 pages of code and would end up duplicating everything . The errors I get are that it you can not assign a different object type to one thats already defined.

what i need to do is something like this

dim card as (some kind of varient)
if a= 1
card=new object1
else
card=new object2
end if

You could do this with an interface with no methods (let’s call it GenericCard). If you apply the interface to both classes, you could then dim the variable as GenericCard.

Then later you use IsA and casting to get it back.

if c isa Card1 then Card1(c).doSomething End if

or you could place all containers on webpage and hide/unhide the un-relevant containers, if not too many

card1.visible = mobileVersion
card2.visible = not mobileVersion

eah, thought of that, but there are loads of objects on the card, would end up sending double, and with 2,500 users that would be a major overhead.

Greg, there are around 30 items on the card, and many pages with cards, I may as well just copy the page and have one for mobile and one for desktop.

Is there no way to create/cast/change a variable after its been defined?

No.

Can you make both subclasses of the same ContainerControl and create a common API in the super? Not only would that eliminate your issue, it would make your coding easier.

Unfortunately, the containers have too many differences :frowning:
Looks like a responsive design in Xojo means duplicating pages at the moment.

why not just

dim c as WebContainer
if mobileversion then
   c=new card1
else
   c= new card2 
end if

Both are WebContainers but to use them you will have to rely on ISA and casting