Accessing Structure Members

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John

    #1

    Accessing Structure Members

    I am having a problem accessing the members in a structure via an interface
    and Class.

    ***Development Code***
    Structure Person
    Public Name as String
    End Structure

    Interface IData
    Public Property Employee As Person
    End Interface

    Public Class MyObject
    Implements IData
    End Class
    **************

    ***Client Code***
    Sub Main
    Dim m as New MyObject
    m.Employee.Name = "John"
    End
    **************

    The problem is with the m.Employee.Name = "John" line. The IDE tells me
    that since it is non-shared, I can't assign a value to it.

    Is this not the right way to use structures?

    Thanks
  • GhostInAK

    #2
    Re: Accessing Structure Members

    Hello John,

    It would be correct except you didnt really implement the interface. You
    told the object you were going to implement it.. but you didnt.

    Public Class MyObject ' Why the hell do people insist on using that My*
    crap???
    Implements IData

    Private oEmployee As Person = New Person

    Public Property Employee As Person Implements IData.Employee
    Get
    Return oEmployee
    End Get
    Set (byval tValue as Person)
    oEmployee = tValue
    End Set
    End Property

    End Class


    I dont know how you got the code to compile without implementing the property.

    -Boo

    I am having a problem accessing the members in a structure via an
    interface and Class.
    >
    ***Development Code***
    Structure Person
    Public Name as String
    End Structure
    Interface IData
    Public Property Employee As Person
    End Interface
    Public Class MyObject
    Implements IData
    End Class
    **************
    >
    ***Client Code***
    Sub Main
    Dim m as New MyObject
    m.Employee.Name = "John"
    End
    **************
    >
    The problem is with the m.Employee.Name = "John" line. The IDE tells
    me that since it is non-shared, I can't assign a value to it.
    >
    Is this not the right way to use structures?
    >
    Thanks
    >

    Comment

    • John

      #3
      Re: Accessing Structure Members

      Hi Boo-

      Thank you for your reply. I tried to keep my original post short and to the
      point so I didn't include all the code in my post. Specifically, the
      interface was implemented, I just didn't show it. Here is the actual code:
      (apologies in advance for the My* crap :-) )

      ****Development Code****
      Public Structure sPerson
      Public FirstName As String
      Public LastName As String
      Public FullName As String
      Public Phone As String
      End Structure

      Public Interface IReportData
      Property Requestor() As sPerson
      Property Engineer() As sPerson
      End Interface

      Public Class ExcelRequestFor m
      Implements IReportData

      Private mEngineer As sPerson = New sPerson
      Private mRequestor As sPerson = New sPerson

      Public Property Engineer() As SPerson Implements
      IReportData.Eng ineer
      Get
      Return mEngineer
      End Get
      Set(ByVal Value As SPerson)
      mEngineer = Value
      End Set
      End Property

      Public Property Requestor() As SPerson Implements
      IReportData.Req uestor
      Get
      Return mRequestor
      End Get
      Set(ByVal Value As SPerson)
      mRequestor = Value
      End Set
      End Property

      End Class
      *************** ****

      ***Client Code***
      Sub Main
      Dim myTest As New ExcelRequestFor m
      myTest.Engineer .FirstName = "John" '<---THIS LINE DOESN'T COMPILE
      End Sub
      **************

      The IDE tells me that the "myTest.Enginee r.Firstname" is an expression that
      is a value and cannot be the target of an assignment... or some nonsense like
      that.

      Thanks again for your time-

      John


      "GhostInAK" wrote:
      Hello John,
      >
      It would be correct except you didnt really implement the interface. You
      told the object you were going to implement it.. but you didnt.
      >
      Public Class MyObject ' Why the hell do people insist on using that My*
      crap???
      Implements IData
      >
      Private oEmployee As Person = New Person
      >
      Public Property Employee As Person Implements IData.Employee
      Get
      Return oEmployee
      End Get
      Set (byval tValue as Person)
      oEmployee = tValue
      End Set
      End Property
      >
      End Class
      >
      >
      I dont know how you got the code to compile without implementing the property.
      >
      -Boo
      >
      >
      I am having a problem accessing the members in a structure via an
      interface and Class.

      ***Development Code***
      Structure Person
      Public Name as String
      End Structure
      Interface IData
      Public Property Employee As Person
      End Interface
      Public Class MyObject
      Implements IData
      End Class
      **************

      ***Client Code***
      Sub Main
      Dim m as New MyObject
      m.Employee.Name = "John"
      End
      **************

      The problem is with the m.Employee.Name = "John" line. The IDE tells
      me that since it is non-shared, I can't assign a value to it.

      Is this not the right way to use structures?

      Thanks
      >
      >
      >

      Comment

      • GhostInAK

        #4
        Re: Accessing Structure Members

        Hello John,

        Heh. That's a fun one. I can't give you the detailed technical description
        of exactly whats going on.. but here's a quick glossover..
        Structures are value types. Just like an Int16 or a Boolean. So, just like
        an Int16, you wouldnt assign a value to a portion of the variable. Like..
        you wouldn't assign byte 2 of an Int16 directly (yeah, you could construct
        an Int16 from an array of bytes using the BitConverter.. that's not the point).
        Point is, you have to assign ExcelRequestFor m.Engineer. Not a portion of
        ..Engineer.

        Dim tForm as ExcelRequestFor m = New ExcelRequestFor m
        Dim tEngineer as sPerson = new sPreson

        tEngineer.First Name = "John"
        tForm.Engineer = tEngineer


        (You may want to add a ctor or two to the struct.)

        -Boo
        Hi Boo-
        >
        Thank you for your reply. I tried to keep my original post short and
        to the point so I didn't include all the code in my post.
        Specifically, the interface was implemented, I just didn't show it.
        Here is the actual code: (apologies in advance for the My* crap :-) )
        >
        ****Development Code****
        Public Structure sPerson
        Public FirstName As String
        Public LastName As String
        Public FullName As String
        Public Phone As String
        End Structure
        Public Interface IReportData
        Property Requestor() As sPerson
        Property Engineer() As sPerson
        End Interface
        Public Class ExcelRequestFor m
        Implements IReportData
        Private mEngineer As sPerson = New sPerson
        Private mRequestor As sPerson = New sPerson
        Public Property Engineer() As SPerson Implements
        IReportData.Eng ineer
        Get
        Return mEngineer
        End Get
        Set(ByVal Value As SPerson)
        mEngineer = Value
        End Set
        End Property
        Public Property Requestor() As SPerson Implements
        IReportData.Req uestor
        Get
        Return mRequestor
        End Get
        Set(ByVal Value As SPerson)
        mRequestor = Value
        End Set
        End Property
        End Class
        *************** ****
        ***Client Code***
        Sub Main
        Dim myTest As New ExcelRequestFor m
        myTest.Engineer .FirstName = "John" '<---THIS LINE DOESN'T
        COMPILE
        End Sub
        **************
        >
        The IDE tells me that the "myTest.Enginee r.Firstname" is an expression
        that is a value and cannot be the target of an assignment... or some
        nonsense like that.
        >
        Thanks again for your time-
        >
        John
        >
        "GhostInAK" wrote:
        >
        >Hello John,
        >>
        >It would be correct except you didnt really implement the interface.
        >You told the object you were going to implement it.. but you didnt.
        >>
        >Public Class MyObject ' Why the hell do people insist on using
        >that My*
        >crap???
        >Implements IData
        >Private oEmployee As Person = New Person
        >>
        >Public Property Employee As Person Implements IData.Employee
        >Get
        >Return oEmployee
        >End Get
        >Set (byval tValue as Person)
        >oEmployee = tValue
        >End Set
        >End Property
        >End Class
        >>
        >I dont know how you got the code to compile without implementing the
        >property.
        >>
        >-Boo
        >>
        >>I am having a problem accessing the members in a structure via an
        >>interface and Class.
        >>>
        >>***Developmen t Code***
        >>Structure Person
        >>Public Name as String
        >>End Structure
        >>Interface IData
        >>Public Property Employee As Person
        >>End Interface
        >>Public Class MyObject
        >>Implements IData
        >>End Class
        >>************* *
        >>***Client Code***
        >>Sub Main
        >>Dim m as New MyObject
        >>m.Employee.Na me = "John"
        >>End
        >>************* *
        >>The problem is with the m.Employee.Name = "John" line. The IDE
        >>tells me that since it is non-shared, I can't assign a value to it.
        >>>
        >>Is this not the right way to use structures?
        >>>
        >>Thanks
        >>>


        Comment

        • John

          #5
          Re: Accessing Structure Members

          Ahhh... okay. That works now. I didn't know you could add constructors to
          Structures (i.e. I didn't think they could contain any working code).

          Thanks for your help Boo!

          John

          "GhostInAK" wrote:
          Hello John,
          >
          Heh. That's a fun one. I can't give you the detailed technical description
          of exactly whats going on.. but here's a quick glossover..
          Structures are value types. Just like an Int16 or a Boolean. So, just like
          an Int16, you wouldnt assign a value to a portion of the variable. Like..
          you wouldn't assign byte 2 of an Int16 directly (yeah, you could construct
          an Int16 from an array of bytes using the BitConverter.. that's not the point).
          Point is, you have to assign ExcelRequestFor m.Engineer. Not a portion of
          ..Engineer.
          >
          Dim tForm as ExcelRequestFor m = New ExcelRequestFor m
          Dim tEngineer as sPerson = new sPreson
          >
          tEngineer.First Name = "John"
          tForm.Engineer = tEngineer
          >
          >
          (You may want to add a ctor or two to the struct.)
          >
          -Boo
          >
          Hi Boo-

          Thank you for your reply. I tried to keep my original post short and
          to the point so I didn't include all the code in my post.
          Specifically, the interface was implemented, I just didn't show it.
          Here is the actual code: (apologies in advance for the My* crap :-) )

          ****Development Code****
          Public Structure sPerson
          Public FirstName As String
          Public LastName As String
          Public FullName As String
          Public Phone As String
          End Structure
          Public Interface IReportData
          Property Requestor() As sPerson
          Property Engineer() As sPerson
          End Interface
          Public Class ExcelRequestFor m
          Implements IReportData
          Private mEngineer As sPerson = New sPerson
          Private mRequestor As sPerson = New sPerson
          Public Property Engineer() As SPerson Implements
          IReportData.Eng ineer
          Get
          Return mEngineer
          End Get
          Set(ByVal Value As SPerson)
          mEngineer = Value
          End Set
          End Property
          Public Property Requestor() As SPerson Implements
          IReportData.Req uestor
          Get
          Return mRequestor
          End Get
          Set(ByVal Value As SPerson)
          mRequestor = Value
          End Set
          End Property
          End Class
          *************** ****
          ***Client Code***
          Sub Main
          Dim myTest As New ExcelRequestFor m
          myTest.Engineer .FirstName = "John" '<---THIS LINE DOESN'T
          COMPILE
          End Sub
          **************

          The IDE tells me that the "myTest.Enginee r.Firstname" is an expression
          that is a value and cannot be the target of an assignment... or some
          nonsense like that.

          Thanks again for your time-

          John

          "GhostInAK" wrote:
          Hello John,
          >
          It would be correct except you didnt really implement the interface.
          You told the object you were going to implement it.. but you didnt.
          >
          Public Class MyObject ' Why the hell do people insist on using
          that My*
          crap???
          Implements IData
          Private oEmployee As Person = New Person
          >
          Public Property Employee As Person Implements IData.Employee
          Get
          Return oEmployee
          End Get
          Set (byval tValue as Person)
          oEmployee = tValue
          End Set
          End Property
          End Class
          >
          I dont know how you got the code to compile without implementing the
          property.
          >
          -Boo
          >
          >I am having a problem accessing the members in a structure via an
          >interface and Class.
          >>
          >***Developme nt Code***
          >Structure Person
          >Public Name as String
          >End Structure
          >Interface IData
          >Public Property Employee As Person
          >End Interface
          >Public Class MyObject
          >Implements IData
          >End Class
          >************ **
          >***Client Code***
          >Sub Main
          >Dim m as New MyObject
          >m.Employee.Nam e = "John"
          >End
          >************ **
          >The problem is with the m.Employee.Name = "John" line. The IDE
          >tells me that since it is non-shared, I can't assign a value to it.
          >>
          >Is this not the right way to use structures?
          >>
          >Thanks
          >>
          >
          >
          >
          >

          Comment

          Working...