Jason,
Is there a way to get the instance name of an object at
runtime?
Short answer NO!
Long answer:
Instances are not named in .NET per se, variables & fields are named...
What name would you expect when you do:
Dim jason As New Person
Dim nowhere As New Address
jason.MailingAddress = nowhere
jason.BillingAddress = nowhere
Remember Address is a reference object, so nowhere, jason.MailingAddress and
jason.BillingAddress all refer to the same instance.
I've thought about setting a name property in the object, but
that's a bit of a hack and makes things much less reusable.
If knowing the difference between a Mailing address & a Billing address is
important I would either define an AddressType property (an Enum) that had
the address types on it.
Enum AddressType
Billing
Mailing
End Enum
class Address
...
Type As AddressType
end class
Or I would make Address abstract (MustInherit) and have MailingAddress &
BillingAddress inherit from Address.
class MustInherit Address
...
end class
class MailingAddress
Inherits Address
...
end class
class BillingAddress
Inherits Address
...
end class
class Person
FirstName
LastName
MailingAddress as MailingAddress
BillingAddress as BillingAddress
end class
I would use the second if I had "significant" polymorphic behavior between
mailing addresses & billing addresses
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley -
http://www.tsbradley.net
"Jason Reynolds" <Ja***********@discussions.microsoft.comwrote in message
news:3E**********************************@microsof t.com...
(using .net 2.0)
Say you have a class structure like this:
class Address
...
end class
class Person
FirstName
LastName
MailingAddress as Address
BillingAddress as Address
end class
I'm using reflection to pull out the values at runtime, but I'm having
trouble when I dive into an address. When I'm traversing the object I
know
when the type is address and I can get and set values on that instance,
but I
can't figure a way to know if I'm looking at the mailing address or the
billing address. Is there a way to get the instance name of an object at
runtime? I've thought about setting a name property in the object, but
that's a bit of a hack and makes things much less reusable. Thanks in
advance.