Announcement

Collapse
No announcement yet.

Method xxxx As UDT

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Scott Slater
    replied
    Update:

    I was able to accomplish what I wanted using another object instead of the UDT. See "What is a Compound Object Reference?" in the docs. That is exactly what I wanted to do. So now code looks like:

    Code:
    #Compile Exe
    #Dim All
     
    Class RightsClass
       Instance AllRights As Dword
       
       Class Method Create
          AllRights = 5641
       End Method
       
       Interface RightsInterface
          Inherit IUnknown
          
          Property Get CanPrint As Byte
             Property = Bit(AllRights,0)
          End Property
     
          Property Get CanHaveFun As Byte
             Property = Bit(AllRights,1)
          End Property
       End Interface
    End Class
     
    Class EmployeeClass
       Instance oSecurity  As RightsInterface
       
       Class Method Create
          oSecurity = Class "RightsClass"
       End Method
       
       Interface EmployeeInterface
          Inherit IUnknown
          
          Method Rights As RightsInterface
             Method = oSecurity
          End Method
       End Interface
     
    End Class
     
    Function PBMain () As Long
     
        Local Employee As EmployeeInterface
        
        Employee = Class "EmployeeClass"
        
        ? Format$( Employee.Rights.CanPrint )   ' <--  Using Object instead of UDT
        
        Employee = Nothing
     
    End Function

    Leave a comment:


  • Scott Slater
    replied
    Here's a sample of what I was trying to do. I need the rights to be all together accessible as "whole" as well as the parts. That's why I wanted to use a UDT or another object.

    Code:
    #Compile Exe
    #Dim All
     
    Type EmployeeRights
       CanPrint As Bit * 1 In Byte
       CanDo_A  As Bit * 1
       CanDo_B  As Bit * 1
       CanDo_C  As Bit * 1
       CanDo_D  As Bit * 1
       CanDo_E  As Bit * 1
       CanDo_F  As Bit * 1
       CanDo_G  As Bit * 1
    End Type
     
    Class EmployeeClass
       Instance tSecurity  As EmployeeRights
       
       Class Method Create
          tSecurity.CanPrint = 1
          tSecurity.CanDo_A = 0
          tSecurity.CanDo_B = 1
          tSecurity.CanDo_C = 0
          tSecurity.CanDo_D = 1
          tSecurity.CanDo_E = 0
          tSecurity.CanDo_F = 1
          tSecurity.CanDo_G = 0
       End Method
       
       Interface EmployeeInterface
          Inherit IUnknown
          
          Property Get Rights As EmployeeRights
             Property = tSecurity
          End Property
       End Interface
     
    End Class
     
    Function PBMain () As Long
     
        Local Employee As EmployeeInterface
        
        Employee = Class "EmployeeClass"
        
        ? Format$( Employee.Rights.CanPrint )   ' <--  This is where the problem is accessing the UDT
        
        Employee = Nothing
     
    End Function
    *Thanks for your help on this Colin.

    I guess I will look into returning an Object instead of a UDT.

    Leave a comment:


  • colin glenn
    replied
    What you should do then is create a property:
    Code:
    PROPERTY GET EmployeeRights() AS LONG
        PROPERTY = IIF( ... , %TRUE, %FALSE)
    END PROPERTY
    And that provides your flag in addition to the encapsulation of the data, the end result is that your code don't CARE how the object stores the data, it just knows HOW to get the desired results from the object.
    Last edited by colin glenn; 21 Feb 2009, 08:31 AM. Reason: Eh, not sure of what you're calling things, ...

    Leave a comment:


  • Scott Slater
    replied
    I can assign the Method's return using LSet so it must really be returning the type as a string. If I were to use another object instead of the UDT, could it be called in the manner that I was trying above?

    The reason I was trying a UDT is that I want to return Bool values so I was going to use BITs to do this. I know could just use a LONG and the bit function, but having readable names makes the code easier to look at. I suppose that I could just use BYTE vars instead, but I think something like;

    If Employee.Rights.CanPrint Then... is more readable than

    If Flag(Employee.Rights, 2) Then...

    I just saw this in the Docs and thought I could access the data directly at the second level.

    Methods may be declared (using AS type...) to return a string, any of the numeric types, a specific class of object variable (AS MyClass), a Variant, or a user defined Type.

    Leave a comment:


  • Scott Slater
    replied
    Then you wouldn't really need to TYPE the property as a UDT, just the element's type would suffice in that case.

    Leave a comment:


  • colin glenn
    replied
    You would have to have a PROPERTY GET for each element of the UDT, at least that's the way I read it.

    Leave a comment:


  • Scott Slater
    started a topic Method xxxx As UDT

    Method xxxx As UDT

    I see in the docs that a method can return a user defined type. I have been experimenting with this, but can't quite figure out how to access the individual elements of the UDT. Here's an example:

    Code:
    Type TestType
       elem1 as long
       elem2 as long
       elem3 as bit * 1 in Byte
       elem4 as bit * 1
       elem5 as bit * 6
       elem6 as asciiz * 20
    End Type
     
     ...
       Method TestMethod As TestType
          Method = TestTypeVar
       End Method
    ...
       Property Get TestProp As TestType
          Property = TestTypeVar
       End Property
    ...
    In the above example I would expect to access elem6 in the type by calling my object like MyObj.TestMethod.elem6, but this doesn't seem to work. Can someone tell me the correct way to read back UDT data embedded in an object.

    My goal is to have this as read-only data so I first tried the Property, but it doesn't work using the Method either. At least the way that I was trying

    I was trying ObjectName.Method/Property.UDT_Element

    I am new at this object stuff, so forgive me... But I am beginning to see great potential here!
Working...
X