Accessing Structure Members

G

Guest

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
 
G

GhostInAK

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
 
G

Guest

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 ExcelRequestForm
Implements IReportData

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

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

Public Property Requestor() As SPerson Implements
IReportData.Requestor
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 ExcelRequestForm
myTest.Engineer.FirstName = "John" '<---THIS LINE DOESN'T COMPILE
End Sub
**************

The IDE tells me that the "myTest.Engineer.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
 
G

GhostInAK

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 ExcelRequestForm.Engineer. Not a portion of
..Engineer.

Dim tForm as ExcelRequestForm = New ExcelRequestForm
Dim tEngineer as sPerson = new sPreson

tEngineer.FirstName = "John"
tForm.Engineer = tEngineer


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

-Boo
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top