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

Type conversion

I know I can convert an instance of a class to an instance of any class type
from which the original class is derived.
So is it possible to convert the other way. E.g.

Let Class Ford be derived from Class Car.

Dim F as New Ford(...)
Dim C as Car

C = CType(F, Car)
F = Nothing
'This conversion works OK and C.GetType.Name is Ford so now how can I
convert C back to a Ford.
F = CType(C, Ford) 'This does not work at runtime although the compiler
thinks its OK.

Since C.GetType is a Ford type why can't I convert to a Ford Class?

Thanks for any help
Fred
Nov 20 '05 #1
3 910
Hi,

This works.

Dim f As New Ford

Dim c As car

f.Make = "Mustang"

f.Color = "Blue"

c = CType(f, car)

Debug.WriteLine(c.GetType.Name)

f = CType(c, Ford)

Debug.WriteLine(f.GetType.Name)

The classes

Public Class car

Dim strMake As String

Public Property Make() As String

Get

Return strMake

End Get

Set(ByVal Value As String)

strMake = Value

End Set

End Property

End Class

Public Class Ford

Inherits car

Dim mstrColor As String

Public Property Color() As String

Get

Return mstrColor

End Get

Set(ByVal Value As String)

mstrColor = Value

End Set

End Property

End Class

Ken

--------------------------

"fred" <fr**@NoSpam.com> wrote in message
news:ek****************@TK2MSFTNGP12.phx.gbl...
I know I can convert an instance of a class to an instance of any class type
from which the original class is derived.
So is it possible to convert the other way. E.g.

Let Class Ford be derived from Class Car.

Dim F as New Ford(...)
Dim C as Car

C = CType(F, Car)
F = Nothing
'This conversion works OK and C.GetType.Name is Ford so now how can I
convert C back to a Ford.
F = CType(C, Ford) 'This does not work at runtime although the compiler
thinks its OK.

Since C.GetType is a Ford type why can't I convert to a Ford Class?

Thanks for any help
Fred

Nov 20 '05 #2
Hi Ken,

I have an old bicycle, can you convert it to a Mercedes for me?
They both have wheels.

Cor
Nov 20 '05 #3
Fred,
Given:

Public Class Car

End Class

Public Class Ford : Inherits Car

End Class

Your code as posted, works, no exceptions!

Dim F as New Ford
Dim C as Car
C = F
F = Nothing
F = DirectCast(C, Ford)

Note you do not need to use CType when you are going from a derived type to
a base type, you do however need to use CType or I prefer DirectCast when
you are going from a base type to a derived type.

The following will not work:

Dim F as New Ford
Dim C as Car
C = F
F = Nothing
C = New Car
F = DirectCast(C, Ford)

As the C variable now contains a Car object, not a Ford object!

Remember that CType is convert type, while DirectCast is cast type. CType
will attempt to convert an object of one type (Integer) into a object of a
different type (String), while DirectCast will only allow casting up & down
the Class inherits & Interface implementation chain. Within VB.NET 2005 (aka
Whidbey, due out in 2005) we will be able to define overloaded operators,
one of which is CType which means that we will be able to define how CType
is going to behave with our class. For example you could define how to
convert any old Car into a new Ford.

Hope this helps
Jay
"fred" <fr**@NoSpam.com> wrote in message
news:ek****************@TK2MSFTNGP12.phx.gbl...
I know I can convert an instance of a class to an instance of any class type from which the original class is derived.
So is it possible to convert the other way. E.g.

Let Class Ford be derived from Class Car.

Dim F as New Ford(...)
Dim C as Car

C = CType(F, Car)
F = Nothing
'This conversion works OK and C.GetType.Name is Ford so now how can I
convert C back to a Ford.
F = CType(C, Ford) 'This does not work at runtime although the compiler
thinks its OK.

Since C.GetType is a Ford type why can't I convert to a Ford Class?

Thanks for any help
Fred

Nov 20 '05 #4

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

Similar topics

4
by: Mark Oliver | last post by:
Hi, I want to put a type conversion in my class, but I don't want the conversion to be usable in a passed parameter because it makes no sense. class cData { string s; public cData(string s)...
7
by: Madhu Gopinathan | last post by:
Hi, I hope this is the right forum for this question. I am extending ICollection to create a Collection Type (say MyCollection) wherein I can control the types of objects being added to the...
27
by: Yuriy Solodkyy | last post by:
Hi VS 2005 beta 2 successfully compiles the following: using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
3
by: pgconnolly | last post by:
/* foreach does implicit type conversion on elements of a params argument or Generic.List. * This is not good. * Examples of evil follow... */ using System; // I love it when C# is strict...
16
by: Enekajmer | last post by:
Hi, 1 int main() 2 { 3 float a = 17.5; 4 printf("%d\n", a); 5 printf("%d\n", *(int *)&a); 6 return 0; 7 }
2
by: Martin v. Lwis | last post by:
I've been working on PEP 353 for some time now. Please comment, in particular if you are using 64-bit systems. Regards, Martin PEP: 353 Title: Using ssize_t as the index type Version:...
1
by: lovecreatesbeauty | last post by:
There is a warning/(error? I remember it is an error for line 10 on some compilers before. At least on g++, it is an error.) for line 10. I first read a similar example from `Expert C Programming...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
4
by: zaeminkr | last post by:
I got a good answer here I have still confusing part. I have two very simple classes class DRect { private : double x0, y0, x1, y1; public : DRect(double a, double b, double c, double d) :...
8
by: Smithers | last post by:
Are there any important differences between the following two ways to convert to a type?... where 'important differences' means something more profound than a simple syntax preference of the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.