473,396 Members | 1,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Current Cast vs. Object Type

I am trying to determine what the current cast of an object is.
Essentially, if I use GetType, it always returns the original type of
the object created but I want to know what the current cast of the
object is (i.e.: is it still cast as a Derived class, or is it cast as
a Base class?) Here is some pseudo code to show the example.

Class Base

....
End Class

Class Derived : Inherits Base

....
End Class

Public Sub Main()
Dim derived1 as New Derived()
Dim base1 as Base

base1 = Ctype(derived1, Base)

Console.Writeline (base1.GetType().ToString())

End Sub

'Output would be "Derived". How to I determine what the CAST is? i.e.
Base?

Thanks in advance.

Dinsdale

Jun 28 '07 #1
11 1759
"Dinsdale" <ru********@gmail.comschrieb
I am trying to determine what the current cast of an object is.
Essentially, if I use GetType, it always returns the original type
of the object created but I want to know what the current cast of
the object is (i.e.: is it still cast as a Derived class, or is it
cast as a Base class?) Here is some pseudo code to show the example.

Class Base

...
End Class

Class Derived : Inherits Base

...
End Class

Public Sub Main()
Dim derived1 as New Derived()
Dim base1 as Base

base1 = Ctype(derived1, Base)

Console.Writeline (base1.GetType().ToString())

End Sub

'Output would be "Derived". How to I determine what the CAST is?
i.e. Base?

Thanks in advance.

Dinsdale

Why would you want to know this? The compiler permits what's possible with
the reference anyway. I don't dare to say "look at the dim statement" ;-)

With local variables this is not possible. With fields, you could use
reflection (whyever) and have a look at System.Type.DeclaringType.
Armin

Jun 28 '07 #2
What are you trying to do ? You could get the base type or an ancestor type
using Reflection.

Strictly speaking you can't do that as once you casted the variable to
another variable you don't have any information about where it came from.
Basically this is bit as if you wanted to know if an integer variable was
created from a constant in the code or parsed from a string or taken from
another variable etc....

IMO the first step would be to explain what you are trying to do so that we
can understand if reflection would be enough or if someone can suggest
another way of doing whatever you are trying to do.

---
Patrice

"Dinsdale" <ru********@gmail.coma écrit dans le message de news:
11**********************@n60g2000hse.googlegroups. com...
>I am trying to determine what the current cast of an object is.
Essentially, if I use GetType, it always returns the original type of
the object created but I want to know what the current cast of the
object is (i.e.: is it still cast as a Derived class, or is it cast as
a Base class?) Here is some pseudo code to show the example.

Class Base

...
End Class

Class Derived : Inherits Base

...
End Class

Public Sub Main()
Dim derived1 as New Derived()
Dim base1 as Base

base1 = Ctype(derived1, Base)

Console.Writeline (base1.GetType().ToString())

End Sub

'Output would be "Derived". How to I determine what the CAST is? i.e.
Base?

Thanks in advance.

Dinsdale

Jun 28 '07 #3
On Jun 28, 10:35 am, "Armin Zingler" <az.nos...@freenet.dewrote:
"Dinsdale" <russ.ha...@gmail.comschrieb
I am trying to determine what the current cast of an object is.
Essentially, if I use GetType, it always returns the original type
of the object created but I want to know what the current cast of
the object is (i.e.: is it still cast as a Derived class, or is it
cast as a Base class?) Here is some pseudo code to show the example.
Class Base
...
End Class
Class Derived : Inherits Base
...
End Class
Public Sub Main()
Dim derived1 as New Derived()
Dim base1 as Base
base1 = Ctype(derived1, Base)
Console.Writeline (base1.GetType().ToString())
End Sub
'Output would be "Derived". How to I determine what the CAST is?
i.e. Base?
Thanks in advance.
Dinsdale

Why would you want to know this? The compiler permits what's possible with
the reference anyway. I don't dare to say "look at the dim statement" ;-)

With local variables this is not possible. With fields, you could use
reflection (whyever) and have a look at System.Type.DeclaringType.

Armin
Ya, I was waiting for someone to say that. lol.

The database uses a sequence number for all primary keys in the table
and we are trying to create a class library around it. I didn't want
to have to code derived classes as having base_id, derived_id,
derived_2_id so I've created a collection of identifiers (including
concurrency identifiers such as modified date). Then the lowest base
class has an ID property that looks at the type and returns the
correct Identifier value from the collection for that cast. The
problem is GetType() doesn't do this, as noted above. The following is
the code that doesn't work. :(

Public Property ID() As Integer
Get
CheckIdentifiers() 'Ensures that there are in fact identifiers
Dim typeName As String
typeName = Me.GetType.Name
Return Identifiers(typeName).ID
End Get
Set(ByVal value As Integer)
CheckIdentifiers()
Dim typeName As String
typeName = Me.GetType.Name
Identifiers(typeName).ID = value
End Set
End Property
So, If I create an object of DerivedType2 and want to cast it as a
BaseType and then get the BaseType.ID property, the above code
executes as DerivedType2 so I get the wrong ID.

Unfortunately, in my test app the DeclaringType is coming up as
Nothing, but thanks for the help. It gives me something to work on...

Cheers
Dinsdale

Jun 28 '07 #4
On Jun 28, 10:58 am, "Patrice" <http://www.chez.com/scribe/wrote:
What are you trying to do ? You could get the base type or an ancestor type
using Reflection.

Strictly speaking you can't do that as once you casted the variable to
another variable you don't have any information about where it came from.
Basically this is bit as if you wanted to know if an integer variable was
created from a constant in the code or parsed from a string or taken from
another variable etc....

IMO the first step would be to explain what you are trying to do so that we
can understand if reflection would be enough or if someone can suggest
another way of doing whatever you are trying to do.

---
Patrice

"Dinsdale" <russ.ha...@gmail.coma écrit dans le message de news:
1183043084.135466.165...@n60g2000hse.googlegroups. com...
I am trying to determine what the current cast of an object is.
Essentially, if I use GetType, it always returns the original type of
the object created but I want to know what the current cast of the
object is (i.e.: is it still cast as a Derived class, or is it cast as
a Base class?) Here is some pseudo code to show the example.
Class Base
...
End Class
Class Derived : Inherits Base
...
End Class
Public Sub Main()
Dim derived1 as New Derived()
Dim base1 as Base
base1 = Ctype(derived1, Base)
Console.Writeline (base1.GetType().ToString())
End Sub
'Output would be "Derived". How to I determine what the CAST is? i.e.
Base?
Thanks in advance.
Dinsdale
I'm not concerned with the historic value, I'm concerned about the
current casting. "What is the name of the object reference type that I
am currently using?"

Jun 28 '07 #5
"Dinsdale" <ru********@gmail.comschrieb
On Jun 28, 10:35 am, "Armin Zingler" <az.nos...@freenet.dewrote:
"Dinsdale" <russ.ha...@gmail.comschrieb
I am trying to determine what the current cast of an object is.
Essentially, if I use GetType, it always returns the original
type of the object created but I want to know what the current
cast of the object is (i.e.: is it still cast as a Derived
class, or is it cast as a Base class?) Here is some pseudo code
to show the example.
Class Base
...
End Class
Class Derived : Inherits Base
...
End Class
Public Sub Main()
Dim derived1 as New Derived()
Dim base1 as Base
base1 = Ctype(derived1, Base)
Console.Writeline (base1.GetType().ToString())
End Sub
'Output would be "Derived". How to I determine what the CAST is?
i.e. Base?
Thanks in advance.
Dinsdale
Why would you want to know this? The compiler permits what's
possible with the reference anyway. I don't dare to say "look at
the dim statement" ;-)

With local variables this is not possible. With fields, you could
use reflection (whyever) and have a look at
System.Type.DeclaringType.

Armin

Ya, I was waiting for someone to say that. lol.

The database uses a sequence number for all primary keys in the
table and we are trying to create a class library around it. I
didn't want to have to code derived classes as having base_id,
derived_id,
derived_2_id so I've created a collection of identifiers (including
concurrency identifiers such as modified date). Then the lowest base
class has an ID property that looks at the type and returns the
correct Identifier value from the collection for that cast. The
problem is GetType() doesn't do this, as noted above. The following
is the code that doesn't work. :(

Public Property ID() As Integer
Get
CheckIdentifiers() 'Ensures that there are in fact identifiers
Dim typeName As String
typeName = Me.GetType.Name
Return Identifiers(typeName).ID
End Get
Set(ByVal value As Integer)
CheckIdentifiers()
Dim typeName As String
typeName = Me.GetType.Name
Identifiers(typeName).ID = value
End Set
End Property
So, If I create an object of DerivedType2 and want to cast it as a
BaseType and then get the BaseType.ID property, the above code
executes as DerivedType2 so I get the wrong ID.

Unfortunately, in my test app the DeclaringType is coming up as
Nothing, but thanks for the help. It gives me something to work
on...

Sorry, but I don't understand this. Maybe somebody else can help.
Armin
Jun 28 '07 #6
Dinsdale,

I suspect that if you repost this with much more detailed information about
what you are trying to accomplish (including detailed info about the database
structure, etc) then you will get some helpful responses.

As it is, your description is too cryptic for anybody to be of much help.

Kerry Moorman
"Dinsdale" wrote:
On Jun 28, 10:35 am, "Armin Zingler" <az.nos...@freenet.dewrote:
"Dinsdale" <russ.ha...@gmail.comschrieb
I am trying to determine what the current cast of an object is.
Essentially, if I use GetType, it always returns the original type
of the object created but I want to know what the current cast of
the object is (i.e.: is it still cast as a Derived class, or is it
cast as a Base class?) Here is some pseudo code to show the example.
Class Base
...
End Class
Class Derived : Inherits Base
...
End Class
Public Sub Main()
Dim derived1 as New Derived()
Dim base1 as Base
base1 = Ctype(derived1, Base)
Console.Writeline (base1.GetType().ToString())
End Sub
'Output would be "Derived". How to I determine what the CAST is?
i.e. Base?
Thanks in advance.
Dinsdale
Why would you want to know this? The compiler permits what's possible with
the reference anyway. I don't dare to say "look at the dim statement" ;-)

With local variables this is not possible. With fields, you could use
reflection (whyever) and have a look at System.Type.DeclaringType.

Armin

Ya, I was waiting for someone to say that. lol.

The database uses a sequence number for all primary keys in the table
and we are trying to create a class library around it. I didn't want
to have to code derived classes as having base_id, derived_id,
derived_2_id so I've created a collection of identifiers (including
concurrency identifiers such as modified date). Then the lowest base
class has an ID property that looks at the type and returns the
correct Identifier value from the collection for that cast. The
problem is GetType() doesn't do this, as noted above. The following is
the code that doesn't work. :(

Public Property ID() As Integer
Get
CheckIdentifiers() 'Ensures that there are in fact identifiers
Dim typeName As String
typeName = Me.GetType.Name
Return Identifiers(typeName).ID
End Get
Set(ByVal value As Integer)
CheckIdentifiers()
Dim typeName As String
typeName = Me.GetType.Name
Identifiers(typeName).ID = value
End Set
End Property
So, If I create an object of DerivedType2 and want to cast it as a
BaseType and then get the BaseType.ID property, the above code
executes as DerivedType2 so I get the wrong ID.

Unfortunately, in my test app the DeclaringType is coming up as
Nothing, but thanks for the help. It gives me something to work on...

Cheers
Dinsdale

Jun 29 '07 #7
Dinsdale wrote:
The database uses a sequence number for all primary keys in the table
and we are trying to create a class library around it. I didn't want
to have to code derived classes as having base_id, derived_id,
derived_2_id so I've created a collection of identifiers (including
concurrency identifiers such as modified date). Then the lowest base
class has an ID property that looks at the type and returns the
correct Identifier value from the collection for that cast.
/If/ I understand this correctly, you want each derived class to be able
to change the value of the ID that it works with?

That's a job for Overriding.

Class Base
Public Overridable ReadOnly Property ID() as Integer
Get
Return m_iSomeIdentifier
End Get
End Property
End Class

Class Derived
Inherits Base
Public Overrides ReadOnly Property ID() As Integer
Get
Return m_iOtherIdentifier
End Get
End Property
End Class

Now, it doesn't matter what type of variable you put either of the above
into; each works out which Identifier it needs to work with.

(/If/ I understand this correctly ...)

HTH,
Phill W.
Jul 2 '07 #8
On Jul 2, 7:22 am, "Phill W." <p-.-a-.-w-a-r...@-o-p-e-n-.-a-c-.-u-k>
wrote:
Dinsdale wrote:
The database uses a sequence number for all primary keys in the table
and we are trying to create a class library around it. I didn't want
to have to code derived classes as having base_id, derived_id,
derived_2_id so I've created a collection of identifiers (including
concurrency identifiers such as modified date). Then the lowest base
class has an ID property that looks at the type and returns the
correct Identifier value from the collection for that cast.

/If/ I understand this correctly, you want each derived class to be able
to change the value of the ID that it works with?

That's a job for Overriding.

Class Base
Public Overridable ReadOnly Property ID() as Integer
Get
Return m_iSomeIdentifier
End Get
End Property
End Class

Class Derived
Inherits Base
Public Overrides ReadOnly Property ID() As Integer
Get
Return m_iOtherIdentifier
End Get
End Property
End Class

Now, it doesn't matter what type of variable you put either of the above
into; each works out which Identifier it needs to work with.

(/If/ I understand this correctly ...)

HTH,
Phill W.
Phill,

Yes, your understanding is correct (thank you, I didn't want to have
to re-type my original posting!). Unfortunately, I do not get the
results that I was anticipating. regardless of the current cast of the
object, I still get the value from the property of the original
declaring type. Maybe I'm doing something wrong (I hope)?

Module Module1
Public MustInherit Class BaseClass

Public MustOverride Property ID() As Integer

End Class
Public Class Derived1
Inherits BaseClass

Private _derived1Int As Integer
Public Overrides Property ID() As Integer
Get
Return _derived1Int
End Get
Set(ByVal value As Integer)
_derived1Int = value
End Set
End Property
Public Sub New(ByVal der1 As Integer)
_derived1Int = der1
End Sub

End Class

Public Class Derived2
Inherits Derived1

Private _derived2Int As Integer
Public Overrides Property ID() As Integer
Get
Return _derived2Int
End Get
Set(ByVal value As Integer)
_derived2Int = value
End Set
End Property

Public Sub New(ByVal der1 As Integer, ByVal der2 As Integer)
MyBase.New(der1)
_derived2Int = der2
End Sub
End Class

Sub Main()
Dim der2 As New Derived2(12, 3)

Console.WriteLine("The ID from the Derived2 object pointer is: {0}",
der2.ID)

Dim der1 As Derived1

der1 = CType(der2, Derived1) 'The CType call is just for the sake of
being explicit...

Console.WriteLine("The ID from the Derived1 object pointer is: {0}",
der1.ID)

Console.Read()
End Sub
End Module
Any suggestions?

Thanks for all the help!

Dinsdale

Jul 3 '07 #9
On Jul 3, 10:06 am, Dinsdale <russ.ha...@gmail.comwrote:
On Jul 2, 7:22 am, "Phill W." <p-.-a-.-w-a-r...@-o-p-e-n-.-a-c-.-u-k>
wrote:
Dinsdale wrote:
The database uses a sequence number for all primary keys in the table
and we are trying to create a class library around it. I didn't want
to have to code derived classes as having base_id, derived_id,
derived_2_id so I've created a collection of identifiers (including
concurrency identifiers such as modified date). Then the lowest base
class has an ID property that looks at the type and returns the
correct Identifier value from the collection for that cast.
/If/ I understand this correctly, you want each derived class to be able
to change the value of the ID that it works with?
That's a job for Overriding.
Class Base
Public Overridable ReadOnly Property ID() as Integer
Get
Return m_iSomeIdentifier
End Get
End Property
End Class
Class Derived
Inherits Base
Public Overrides ReadOnly Property ID() As Integer
Get
Return m_iOtherIdentifier
End Get
End Property
End Class
Now, it doesn't matter what type of variable you put either of the above
into; each works out which Identifier it needs to work with.
(/If/ I understand this correctly ...)
HTH,
Phill W.

Phill,

Yes, your understanding is correct (thank you, I didn't want to have
to re-type my original posting!). Unfortunately, I do not get the
results that I was anticipating. regardless of the current cast of the
object, I still get the value from the property of the original
declaring type. Maybe I'm doing something wrong (I hope)?

Module Module1
Public MustInherit Class BaseClass

Public MustOverride Property ID() As Integer

End Class

Public Class Derived1
Inherits BaseClass

Private _derived1Int As Integer
Public Overrides Property ID() As Integer
Get
Return _derived1Int
End Get
Set(ByVal value As Integer)
_derived1Int = value
End Set
End Property

Public Sub New(ByVal der1 As Integer)
_derived1Int = der1
End Sub

End Class

Public Class Derived2
Inherits Derived1

Private _derived2Int As Integer
Public Overrides Property ID() As Integer
Get
Return _derived2Int
End Get
Set(ByVal value As Integer)
_derived2Int = value
End Set
End Property

Public Sub New(ByVal der1 As Integer, ByVal der2 As Integer)
MyBase.New(der1)
_derived2Int = der2
End Sub
End Class

Sub Main()
Dim der2 As New Derived2(12, 3)

Console.WriteLine("The ID from the Derived2 object pointer is: {0}",
der2.ID)

Dim der1 As Derived1

der1 = CType(der2, Derived1) 'The CType call is just for the sake of
being explicit...

Console.WriteLine("The ID from the Derived1 object pointer is: {0}",
der1.ID)

Console.Read()
End Sub

End Module

Any suggestions?

Thanks for all the help!

Dinsdale

Oh, I guess I should post the results and what I expect? lol.

What I was HOPING to get was:

The ID from the Derived2 Object Pointer is: 3
The ID from the Derived1 Object Pointer is: 12

The ACTUAL output was as follows:

The ID from the Derived2 Object Pointer is: 3
The ID from the Derived1 Object Pointer is: 3
Cheers
Dinsdale

Jul 3 '07 #10
On Jul 3, 10:13 am, Dinsdale <russ.ha...@gmail.comwrote:
On Jul 3, 10:06 am, Dinsdale <russ.ha...@gmail.comwrote:
On Jul 2, 7:22 am, "Phill W." <p-.-a-.-w-a-r...@-o-p-e-n-.-a-c-.-u-k>
wrote:
Dinsdale wrote:
The database uses a sequence number for all primary keys in the table
and we are trying to create a class library around it. I didn't want
to have to code derived classes as having base_id, derived_id,
derived_2_id so I've created a collection of identifiers (including
concurrency identifiers such as modified date). Then the lowest base
class has an ID property that looks at the type and returns the
correct Identifier value from the collection for that cast.
/If/ I understand this correctly, you want each derived class to be able
to change the value of the ID that it works with?
That's a job for Overriding.
Class Base
Public Overridable ReadOnly Property ID() as Integer
Get
Return m_iSomeIdentifier
End Get
End Property
End Class
Class Derived
Inherits Base
Public Overrides ReadOnly Property ID() As Integer
Get
Return m_iOtherIdentifier
End Get
End Property
End Class
Now, it doesn't matter what type of variable you put either of the above
into; each works out which Identifier it needs to work with.
(/If/ I understand this correctly ...)
HTH,
Phill W.
Phill,
Yes, your understanding is correct (thank you, I didn't want to have
to re-type my original posting!). Unfortunately, I do not get the
results that I was anticipating. regardless of the current cast of the
object, I still get the value from the property of the original
declaring type. Maybe I'm doing something wrong (I hope)?
Module Module1
Public MustInherit Class BaseClass
Public MustOverride Property ID() As Integer
End Class
Public Class Derived1
Inherits BaseClass
Private _derived1Int As Integer
Public Overrides Property ID() As Integer
Get
Return _derived1Int
End Get
Set(ByVal value As Integer)
_derived1Int = value
End Set
End Property
Public Sub New(ByVal der1 As Integer)
_derived1Int = der1
End Sub
End Class
Public Class Derived2
Inherits Derived1
Private _derived2Int As Integer
Public Overrides Property ID() As Integer
Get
Return _derived2Int
End Get
Set(ByVal value As Integer)
_derived2Int = value
End Set
End Property
Public Sub New(ByVal der1 As Integer, ByVal der2 As Integer)
MyBase.New(der1)
_derived2Int = der2
End Sub
End Class
Sub Main()
Dim der2 As New Derived2(12, 3)
Console.WriteLine("The ID from the Derived2 object pointer is: {0}",
der2.ID)
Dim der1 As Derived1
der1 = CType(der2, Derived1) 'The CType call is just for the sake of
being explicit...
Console.WriteLine("The ID from the Derived1 object pointer is: {0}",
der1.ID)
Console.Read()
End Sub
End Module
Any suggestions?
Thanks for all the help!
Dinsdale

Oh, I guess I should post the results and what I expect? lol.

What I was HOPING to get was:

The ID from the Derived2 Object Pointer is: 3
The ID from the Derived1 Object Pointer is: 12

The ACTUAL output was as follows:

The ID from the Derived2 Object Pointer is: 3
The ID from the Derived1 Object Pointer is: 3

Cheers
Dinsdale
Ha ha! One of my work mates seems to have solved the problem. The
solution is to use the Shadows keyword. I'll post the explicit
implementation when I figure out how I want to do it...

Thanks everyone!
Dinsdale

Jul 3 '07 #11
On Jul 3, 10:28 am, Dinsdale <russ.ha...@gmail.comwrote:
On Jul 3, 10:13 am, Dinsdale <russ.ha...@gmail.comwrote:
On Jul 3, 10:06 am, Dinsdale <russ.ha...@gmail.comwrote:
On Jul 2, 7:22 am, "Phill W." <p-.-a-.-w-a-r...@-o-p-e-n-.-a-c-.-u-k>
wrote:
Dinsdale wrote:
The database uses a sequence number for all primary keys in the table
and we are trying to create a class library around it. I didn't want
to have to code derived classes as having base_id, derived_id,
derived_2_id so I've created a collection of identifiers (including
concurrency identifiers such as modified date). Then the lowest base
class has an ID property that looks at the type and returns the
correct Identifier value from the collection for that cast.
/If/ I understand this correctly, you want each derived class to be able
to change the value of the ID that it works with?
That's a job for Overriding.
Class Base
Public Overridable ReadOnly Property ID() as Integer
Get
Return m_iSomeIdentifier
End Get
End Property
End Class
Class Derived
Inherits Base
Public Overrides ReadOnly Property ID() As Integer
Get
Return m_iOtherIdentifier
End Get
End Property
End Class
Now, it doesn't matter what type of variable you put either of the above
into; each works out which Identifier it needs to work with.
(/If/ I understand this correctly ...)
HTH,
Phill W.
Phill,
Yes, your understanding is correct (thank you, I didn't want to have
to re-type my original posting!). Unfortunately, I do not get the
results that I was anticipating. regardless of the current cast of the
object, I still get the value from the property of the original
declaring type. Maybe I'm doing something wrong (I hope)?
Module Module1
Public MustInherit Class BaseClass
Public MustOverride Property ID() As Integer
End Class
Public Class Derived1
Inherits BaseClass
Private _derived1Int As Integer
Public Overrides Property ID() As Integer
Get
Return _derived1Int
End Get
Set(ByVal value As Integer)
_derived1Int = value
End Set
End Property
Public Sub New(ByVal der1 As Integer)
_derived1Int = der1
End Sub
End Class
Public Class Derived2
Inherits Derived1
Private _derived2Int As Integer
Public Overrides Property ID() As Integer
Get
Return _derived2Int
End Get
Set(ByVal value As Integer)
_derived2Int = value
End Set
End Property
Public Sub New(ByVal der1 As Integer, ByVal der2 As Integer)
MyBase.New(der1)
_derived2Int = der2
End Sub
End Class
Sub Main()
Dim der2 As New Derived2(12, 3)
Console.WriteLine("The ID from the Derived2 object pointer is: {0}",
der2.ID)
Dim der1 As Derived1
der1 = CType(der2, Derived1) 'The CType call is just for the sake of
being explicit...
Console.WriteLine("The ID from the Derived1 object pointer is: {0}",
der1.ID)
Console.Read()
End Sub
End Module
Any suggestions?
Thanks for all the help!
Dinsdale
Oh, I guess I should post the results and what I expect? lol.
What I was HOPING to get was:
The ID from the Derived2 Object Pointer is: 3
The ID from the Derived1 Object Pointer is: 12
The ACTUAL output was as follows:
The ID from the Derived2 Object Pointer is: 3
The ID from the Derived1 Object Pointer is: 3
Cheers
Dinsdale

Ha ha! One of my work mates seems to have solved the problem. The
solution is to use the Shadows keyword. I'll post the explicit
implementation when I figure out how I want to do it...

Thanks everyone!
Dinsdale
Okay, so the following code works and returns the correct values for
the specified object pointer (i.e. Derived2 returns 3, Derived1
returns 12: See previous postings...)

Public MustInherit Class BaseClass

Public MustOverride Property ID() As Integer

End Class

Public Class Derived1
Inherits BaseClass

Private _derived1Int As Integer
Public Overrides Property ID() As Integer
Get

Return _derived1Int

End Get
Set(ByVal value As Integer)
_derived1Int = value
End Set
End Property
Public Sub New(ByVal der1 As Integer)
_derived1Int = der1
End Sub

End Class

Public Class Derived2
Inherits Derived1

Private _derived2Int As Integer
Public Shadows Property ID() As Integer
Get

Return _derived2Int

End Get
Set(ByVal value As Integer)
_derived2Int = value
End Set
End Property

Public Sub New(ByVal der1 As Integer, ByVal der2 As Integer)
MyBase.New(der1)
_derived2Int = der2
End Sub
End Class

This is all very wonderful, but I am essentially a very lazy person
and don't want to have to code all of this for each class. Is there a
way to use reflection to deduce what type of object pointer was used
to call the property from inside the property? If so, I can code this
ONCE in the base class and then use a name value pair collection to
get back the correct value holder as such

Public MustInherit Class BaseClass

Private col1 As New Dictionary(Of String, Integer)

Public Property ID() As Integer
Get
Dim typeName As String
typeName = "?" '? Determine Type Here

Return col1(typeName)
End Get
Set(ByVal value As Integer)

Dim typeName As String
typeName = "?" '? Determine Type Here
col1(typeName) = value
End Set
End Property
End Class

This is over simplified a little as I would need to check for an
instance in the dictionary and that sort of thing, but what seems to
be difficult is to deduce what the object pointer type that called
this property was. Is this possible?

Cheers
Dinsdale
Jul 3 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Daylor | last post by:
first of all , PLEASE,say somthing..about my post. any reply will be nice to read. i have vb.app , with 2 appdomains. (now, in the next section , ill write "appdomain create" ,means a code in...
4
by: Richard Lee | last post by:
Hi, I have a question when I do a data type cast. the common way when we do a cast, is we know the type we want to cast to, i.e. we want to cast object to string, object xyz = "question";...
2
by: Fred Zolar | last post by:
Anyone know how to cast an object to the type stored in a System.Type variable? I need to cast m_oValue to an int, bool, ect... based on the variable Type. public class TestClass { private...
5
by: miki | last post by:
Hi all, How can I cast from a string to an object? For example, suppose I have classes as employee, manager, supervisor, director,...I have a user interface that takes a person name, then...
8
by: GlennDoten | last post by:
I just happened to be looking through the implementation of the System.Version class in the SSCLI and one of the constructors starts like this: public Version(String version) { if ((Object)...
17
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
3
by: mra | last post by:
I want to cast an object that I have created from a typename to the corresponding type. Can anycone tell me how to do this? Example: //Here, Create the object of type "MyClass" object...
9
by: Jim in Arizona | last post by:
I get this error: Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlInputText' to type 'System.Web.UI.WebControls.TextBox'. Using this code: Dim test3 As TextBox test3 =...
9
by: Victor | last post by:
Hi all Does anyone has any reference or examples about how to customize the httpcontext.current.user object to my own user object? Cheers Victor
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.