Changing a class at runtime

  1. 2 weeks ago

    dave d

    Sep 4 Pre-Release Testers, Xojo Pro Australia

    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?

  2. Jeff T

    Sep 4 Pre-Release Testers Midlands of England, Europe

    Still does not like it

    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
  3. dave d

    Sep 4 Pre-Release Testers, Xojo Pro Australia

    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.

  4. dave d

    Sep 4 Pre-Release Testers, Xojo Pro Australia

    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

  5. Greg O

    Sep 4 Xojo Inc

    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
  6. Olivier C

    Sep 4 Pre-Release Testers, Xojo Pro Europe (Belgium, France)

    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

  7. dave d

    Sep 4 Pre-Release Testers, Xojo Pro Australia

    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?

  8. Kem T

    Sep 4 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    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.

  9. dave d

    Sep 4 Pre-Release Testers, Xojo Pro Australia

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

  10. Norman P

    Sep 4 Pre-Release Testers, Xojo Pro great-white-software.com/blog

    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

or Sign Up to reply!