473,396 Members | 2,039 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.

Casting from interface to it's base type

Hi (final q I hope!)

I have a class which implements an

Public Class MyClass(Of T)
Implements IMyInterface

End Class

The Class was instantiated with a

Dim _myClass as New MyClass(of String)

and then added to a Dictionary(of String, IMyInterface)

When I enumerate the dictionary

For each _kvp as KeyPairValue(of String, IMyInterface)
Dim _myClass = _kvp.value

I look at Intellisense and it tells me that _kvp.Value is a MyClass(of
String), yet when it is assigned to _myClass it is still a IMyInterface.

Is there anyway I can get it to cast to what Intellisense is telling me
it's underlying type is without explicitly

dim _myClass = CType(_kvp.value,MyClass(of String))

Thx

Simon
Jun 27 '08 #1
2 1244
Simon Woods wrote:
Hi (final q I hope!)

I have a class which implements an

Public Class MyClass(Of T)
Implements IMyInterface

End Class

The Class was instantiated with a

Dim _myClass as New MyClass(of String)

and then added to a Dictionary(of String, IMyInterface)

When I enumerate the dictionary

For each _kvp as KeyPairValue(of String, IMyInterface)
Dim _myClass = _kvp.value

I look at Intellisense and it tells me that _kvp.Value is a MyClass(of
String), yet when it is assigned to _myClass it is still a IMyInterface.

Is there anyway I can get it to cast to what Intellisense is telling me
it's underlying type is without explicitly

dim _myClass = CType(_kvp.value,MyClass(of String))

Thx

Simon
No, you have to cast the reference to the type that you want.

You might be able to make the casting implicit by assigning the
reference to a variable of the type that you want, but that would only
be a difference in the syntax. The casting still has to be performed in
the code, regardless if you make it explicit or implicit.
--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #2
Göran Andersson wrote:
Simon Woods wrote:
>Hi (final q I hope!)

I have a class which implements an

Public Class MyClass(Of T)
Implements IMyInterface

End Class

The Class was instantiated with a

Dim _myClass as New MyClass(of String)

and then added to a Dictionary(of String, IMyInterface)

When I enumerate the dictionary

For each _kvp as KeyPairValue(of String, IMyInterface)
Dim _myClass = _kvp.value

I look at Intellisense and it tells me that _kvp.Value is a MyClass(of
String), yet when it is assigned to _myClass it is still a IMyInterface.

Is there anyway I can get it to cast to what Intellisense is telling
me it's underlying type is without explicitly

dim _myClass = CType(_kvp.value,MyClass(of String))

Thx

Simon

No, you have to cast the reference to the type that you want.

You might be able to make the casting implicit by assigning the
reference to a variable of the type that you want, but that would only
be a difference in the syntax. The casting still has to be performed in
the code, regardless if you make it explicit or implicit.
OK ... thanks again
Jun 27 '08 #3

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

Similar topics

3
by: Kurt | last post by:
i just can't figure out why something im doing is not working correctly.... public interface IInterface { int someProperty { get; set; }
0
by: Kurt Lange | last post by:
no... the array is created dynamically. and no... that defeats the purpose of what im trying todo.. encapsulate all initializing of variables in base class... derive from it... by deriving...
2
by: Zac | last post by:
Alright anyone who has 2c throw it in... I am working through a custom xml serializer and have come upon a conundrum, given our class design. The interface implemented on the base class (base...
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
2
by: philippe sillon | last post by:
Hi, I have a problem to implemente the strategy pattern. the problem come from that a method take different arguments type (object in interface and String / Int in implemented class). When...
0
by: Greg Conely via .NET 247 | last post by:
I am creating a application that will be using plugins. I am doing this so that when I want to let this application work with another type of dbase system, I only have to write\install one plugin,...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
3
by: =?Utf-8?B?TWlydHVs?= | last post by:
Hi I'm currently working with vbscripting through MSScriptControl. We have shared some of our objects that should be available for scripting. Some of the functions of these objects will return...
9
by: Naomi | last post by:
I need to make software engineering decision to do with using a derived data type in a container class. So for example, if I have an Edge class, and I want to make a Edge object which contains two...
5
by: Ronald Raygun | last post by:
If I have the following class heirarchy: class A{ protected $m_type; function type(){return $this->m_type;} } class B extends A{} class C extends B{}
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.