473,395 Members | 1,631 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,395 software developers and data experts.

GetClassName from an Interface

Hi

Still trying to get to grips with VB.Net so sorry if these seem silly
questions ...

I have an object which is implementing a secondary interface. I want to
get the name of the underlying object.

My Interface is:

Public Interface ICommand
Sub Execute
End Interface

ATM, I'm doing

name = DirectCast(myInterfacedObject, Object).GetType.Name

Are there better ways of doing this? Examples I've seen in C# seem to
suggest that you can call the GetType off the Interface itself.

Obviously I could put the Name property onto the interface, but I'm
wondering if there are some other ways I could do this?

Also, what is going on when DirectCast is called? Is the object being
copied to somewhere else in memory or is a different address in memory
being used to retrieve the Type info?

Thanks

Simon
Oct 22 '07 #1
5 2248
"Simon Woods" <si*********@hotmail.comschrieb
Hi

Still trying to get to grips with VB.Net so sorry if these seem
silly questions ...

I have an object which is implementing a secondary interface. I want
to get the name of the underlying object.
My Interface is:

Public Interface ICommand
Sub Execute
End Interface

ATM, I'm doing

name = DirectCast(myInterfacedObject, Object).GetType.Name

Are there better ways of doing this? Examples I've seen in C# seem
to suggest that you can call the GetType off the Interface itself.

Obviously I could put the Name property onto the interface, but I'm
wondering if there are some other ways I could do this?
Why? Purpose for getting type information?

Also, what is going on when DirectCast is called?
The type of the reference is changed to the destination type.
Is the object
being copied to somewhere else in memory
No
or is a different address
in memory being used to retrieve the Type info?
The Object's Gettype method is used to get the type info. The interface does
not have a GetType method.
Armin

Oct 22 '07 #2
Armin Zingler wrote:
"Simon Woods" <si*********@hotmail.comschrieb
>Hi

Still trying to get to grips with VB.Net so sorry if these seem
silly questions ...

I have an object which is implementing a secondary interface. I want
to get the name of the underlying object.
>My Interface is:

Public Interface ICommand
Sub Execute
End Interface

ATM, I'm doing

name = DirectCast(myInterfacedObject, Object).GetType.Name

Are there better ways of doing this? Examples I've seen in C# seem
to suggest that you can call the GetType off the Interface itself.

Obviously I could put the Name property onto the interface, but I'm
wondering if there are some other ways I could do this?

Why? Purpose for getting type information?
I'm learning some design patterns and am translating some java code ...
It has been translated into c# okay, but vb doen't give you the GetType
off the interface.

Oct 22 '07 #3
"Simon Woods" <si*********@hotmail.comschrieb
Armin Zingler wrote:
"Simon Woods" <si*********@hotmail.comschrieb
Hi
>
Still trying to get to grips with VB.Net so sorry if these seem
silly questions ...
>
I have an object which is implementing a secondary interface. I
want to get the name of the underlying object.
My Interface is:
>
Public Interface ICommand
Sub Execute
End Interface
>
ATM, I'm doing
>
name = DirectCast(myInterfacedObject, Object).GetType.Name
>
Are there better ways of doing this? Examples I've seen in C#
seem to suggest that you can call the GetType off the Interface
itself.
>
Obviously I could put the Name property onto the interface, but
I'm wondering if there are some other ways I could do this?
Why? Purpose for getting type information?

I'm learning some design patterns and am translating some java code
... It has been translated into c# okay, but vb doen't give you the
GetType off the interface.

Shared Function GetTheType(ByVal o As Object) As System.Type
Return o.GetType
End Function
Armin
Oct 22 '07 #4
Simon Woods wrote:
I have an object which is implementing a secondary interface. I want to
get the name of the underlying object.
"Name" or "Type"?
name = DirectCast(myInterfacedObject, Object).GetType.Name

Are there better ways of doing this? Examples I've seen in C# seem to
suggest that you can call the GetType off the Interface itself.
You /should/ be able to.
The only thing to watch out for is some "bright-spark" may have
Shadow'ed the GetType method and made it do something completely
different - stupid, I know, but it's the kind of thing Developers /love/
to do - "up-casting" to Object gets around this by using Object's most
basic implementation.
Obviously I could put the Name property onto the interface, but I'm
wondering if there are some other ways I could do this?
If you're after a Name property on a class that you know about, just
cast the reference to the required Type (after checking that you /can/,
of course):

If TypeOf myInterfacedObject Is NamedThing Then
sName = DirectCast( myInterfacedObject, NamedThing ).Name
End If
Also, what is going on when DirectCast is called? Is the object being
copied to somewhere else in memory or is a different address in memory
being used to retrieve the Type info?
Nope. If you've ever worked with 'C++' or 'C#', you'll have come across
the likes of:

int * pointerToInteger = 0 ;
char * pointerToChar = (char *)pointerToInteger ;

The second line takes a pointer to an integer value and blithely stuffs
it into a variable that's supposed to point to a character value. Both
pointers refer to the same chunk of memory, but treat it as different
"types".

DirectCast does much the same thing. It tells the compiler to treat the
given "object reference" (a.k.a. strongly-typed Pointer) as a reference
to a /different/ Type. However, it does go a little bit further and
checks the object's inheritance hierarchy and, if it can't make the cast
you've asked it to, it'll throw an Exception on you:

Class Class1
Public Property Name
Class Class2 Inherits Class1
Class Class3 Inherits Class2
Class Class4

Dim c2 as New Class2
?DirectCast( c2, Class1 ).Name ' OK
?DirectCast( c2, Class4 ).Name ' Boom!
?DirectCast( c2, Class3 ).Name ' Boom! Think about this one

HTH,
Phill W.
Oct 23 '07 #5
Phill W. wrote:
Simon Woods wrote:
>I have an object which is implementing a secondary interface. I want
to get the name of the underlying object.

"Name" or "Type"?
>name = DirectCast(myInterfacedObject, Object).GetType.Name

Are there better ways of doing this? Examples I've seen in C# seem to
suggest that you can call the GetType off the Interface itself.

You /should/ be able to.
The only thing to watch out for is some "bright-spark" may have
Shadow'ed the GetType method and made it do something completely
different - stupid, I know, but it's the kind of thing Developers /love/
to do - "up-casting" to Object gets around this by using Object's most
basic implementation.
>Obviously I could put the Name property onto the interface, but I'm
wondering if there are some other ways I could do this?

If you're after a Name property on a class that you know about, just
cast the reference to the required Type (after checking that you /can/,
of course):

If TypeOf myInterfacedObject Is NamedThing Then
sName = DirectCast( myInterfacedObject, NamedThing ).Name
End If
>Also, what is going on when DirectCast is called? Is the object being
copied to somewhere else in memory or is a different address in memory
being used to retrieve the Type info?

Nope. If you've ever worked with 'C++' or 'C#', you'll have come across
the likes of:

int * pointerToInteger = 0 ;
char * pointerToChar = (char *)pointerToInteger ;

The second line takes a pointer to an integer value and blithely stuffs
it into a variable that's supposed to point to a character value. Both
pointers refer to the same chunk of memory, but treat it as different
"types".

DirectCast does much the same thing. It tells the compiler to treat the
given "object reference" (a.k.a. strongly-typed Pointer) as a reference
to a /different/ Type. However, it does go a little bit further and
checks the object's inheritance hierarchy and, if it can't make the cast
you've asked it to, it'll throw an Exception on you:

Class Class1
Public Property Name
Class Class2 Inherits Class1
Class Class3 Inherits Class2
Class Class4

Dim c2 as New Class2
?DirectCast( c2, Class1 ).Name ' OK
?DirectCast( c2, Class4 ).Name ' Boom!
?DirectCast( c2, Class3 ).Name ' Boom! Think about this one

HTH,
Phill W.
That's very helpful ... thanks very much (to Armin as well)

Simon
Oct 23 '07 #6

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

Similar topics

2
by: bala | last post by:
Hi, I want to know how to getclassname of application from python..In python There is no method like GetClassName... Is there any other Method ...I kindly give some example in python.. If anyone...
4
by: Roy Pereira | last post by:
I have an application that is composed of a set of "Content" dlls and a viewer application. The viewer calls a standard set of functions that are present in all the dlls. I maintain this by...
9
by: Anon Email | last post by:
Hi people, I'm learning about header files in C++. The following is code from Bartosz Milewski: // Code const int maxStack = 16; class IStack
4
by: Doug | last post by:
I am working on an existing .NET (C Sharp) component that had a com interface that was used by a VB component. When it was originally written, it had the SSEAssemblyCom class below - minus the two...
3
by: zlst | last post by:
Many technological innovations rely upon User Interface Design to elevate their technical complexity to a usable product. Technology alone may not win user acceptance and subsequent marketability....
4
by: Kartic | last post by:
Hi, Declare Ansi Function GetClassName Lib "user32" Alias "GetClassNameA" ( ByVal hWnd As IntPtr, ByVal lpClassName As System.Text.StringBuilder, ByVal nMaxCount As Int32) As Int32 I am...
2
by: stephen.garner | last post by:
Hi, I am using the IntPtr FindWindow(string lpClassName, string lpWindowName); to get the handle for a window (notepad in this case), I am then using int GetClassName(IntPtr hWnd, out...
0
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
15
by: Xah Lee | last post by:
On Java's Interface Xah Lee, 20050223 In Java the language, there's this a keyword “interface”. In a functional language, a function can be specified by its name and parameter specs....
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.