473,761 Members | 4,511 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can anyone help with proper use of operator CType in VB.net?

I'm running into a situation there I think an operator overload would solve
the issue, but I'm unable to make it work for some reason. If anyone can
help here I would appreciate it.

I have a base class that is common to many other classes.
public class Base
....
end class

I have 2 seperate classes that inherit from base

public ClassA : inherits Base
....
End Class

public ClassB : inherits Base
....
End Class

Now, I have an instance of ClassA and ClassB and I'm trying to assign the
ClassB instance to ClassA.

Dim m_ClassA as new ClassA
dim m_ClassB as new ClassB

m_ClassA = m_ClassB

Obviously, this won't work as is because ClassA is not the same as ClassB.
So we turn to operator overloading. I really want to overload the assignment
operator, but I read that VB.Net doesn't support this. So we have to overload
operator CType.

If each ClassA and ClassB have a constructor that takes a string and each
class also had an operator CType that would convert the class instance to a
string, shouldn't the assignment become valid?

public class ClassA inherits Base
public sub New (param as sting)
....
end sub

public shared overloads operator CType(byval obj as ClassA() as string
return obj.ToString()
end sub

end class

If this is not valid, could someone please share what is wrong about this?
the operator CType will compile successfully, but the results are not desired
and the compile still flags it as an error.

unforunately the derived class (ClassA and ClassB) do no know about each
other so we can't put any more specific types into the class definitions.

thanks
bill
Jul 22 '06 #1
6 3548
Bill foust wrote:
<snip>
I have a base class that is common to many other classes.
public class Base
...
end class

I have 2 seperate classes that inherit from base

public ClassA : inherits Base
...
End Class

public ClassB : inherits Base
...
End Class

Now, I have an instance of ClassA and ClassB and I'm trying to assign the
ClassB instance to ClassA.

Dim m_ClassA as new ClassA
dim m_ClassB as new ClassB

m_ClassA = m_ClassB

Obviously, this won't work as is because ClassA is not the same as ClassB.
So we turn to operator overloading. I really want to overload the assignment
operator, but I read that VB.Net doesn't support this. So we have to overload
operator CType.
<snip>

One possible solution would be to have a virtual (Overridable) Assign
method in Base:

Public Overridable Sub Assign(Value As Base)
'...
End Sub

And have each class get from the value whatever they want:

'On ClassB
Public Overrides Sub Assign(Value As Base)
'get items from a Base Class
End Sub

Public Overridable Overloads Sub Assign(Value As ClassB)
Assign(DirectCa st(Value, Base))
'get items that are specific for ClassB types
'...
End Sub

'On ClassA
Public Overrides Sub Assign(Value As Base)
'get items from a Base Class
End Sub

Public Overridable Overloads Sub Assign(Value As ClassA)
Assign(DirectCa st(Value, Base))
'get items that are specific for ClassA types
'...
End Sub
Dim m_ClassA as new ClassA
dim m_ClassB as new ClassB
m_ClassA.Assign (m_ClassB)

HTH,

Regards

Branco

Jul 22 '06 #2
Since both m_ClassA and m_ClassB inherit from Base, then the following
should work:

m_ClassA = DirectCast(m_Cl assB, Base)

"Bill foust" <Bi*******@disc ussions.microso ft.comwrote in message
news:6A******** *************** ***********@mic rosoft.com...
I'm running into a situation there I think an operator overload would
solve
the issue, but I'm unable to make it work for some reason. If anyone can
help here I would appreciate it.

I have a base class that is common to many other classes.
public class Base
...
end class

I have 2 seperate classes that inherit from base

public ClassA : inherits Base
...
End Class

public ClassB : inherits Base
...
End Class

Now, I have an instance of ClassA and ClassB and I'm trying to assign the
ClassB instance to ClassA.

Dim m_ClassA as new ClassA
dim m_ClassB as new ClassB

m_ClassA = m_ClassB

Obviously, this won't work as is because ClassA is not the same as ClassB.
So we turn to operator overloading. I really want to overload the
assignment
operator, but I read that VB.Net doesn't support this. So we have to
overload
operator CType.

If each ClassA and ClassB have a constructor that takes a string and each
class also had an operator CType that would convert the class instance to
a
string, shouldn't the assignment become valid?

public class ClassA inherits Base
public sub New (param as sting)
...
end sub

public shared overloads operator CType(byval obj as ClassA() as string
return obj.ToString()
end sub

end class

If this is not valid, could someone please share what is wrong about this?
the operator CType will compile successfully, but the results are not
desired
and the compile still flags it as an error.

unforunately the derived class (ClassA and ClassB) do no know about each
other so we can't put any more specific types into the class definitions.

thanks
bill


Jul 22 '06 #3

Matt wrote:
Since both m_ClassA and m_ClassB inherit from Base, then the following
should work:

m_ClassA = DirectCast(m_Cl assB, Base)
snip>
I guess it won't. In assignment of reference types, a most derived type
may be assigned to a least derived one (in the same inheritance chain,
of course), but not the contrary.

Depending on your Option Strict settings, the compiler may even let you
perform the assignment with nothing but a warning. But, believe me, the
assignment will fail at run time (you certainly don't want to see
that)...

The cast would only be assignable to m_ClassA if m_ClassB derived from
ClassA -- which it doesn't.

Regards,

Branco.

Jul 22 '06 #4
Thanks for the quick replies! Unfortunately neither suggestion will work for
me.

I have to use an operator = to do this assignment and cannot use other
methods such as you Assign method. That is unless I'm misunderstandin g what
you are saying and Assign() exists on all objects and is used by the compiler
to implement operator=. The help docs didn't seem to suggest that however. :)

You see, Base contains a string of data and the derived classes simply
implement a variety of properties to access specific substring portions of
the string in the class in order to give them a "pretty name". Also, because
this code is the result of an automatic code generation process, it doesnt
really have any knowledge about the types of these derived classes. For these
reasons, I'm kind of stuck using operator = to accomplish both a deep copy
(not a reference copy) and a type cast in one operation.

I was hoping the usage of operator Ctype for a widening to string and
another ctype for widening to the specific class (example below)...

public ClassA : inherits Base
....
public shared overrides widening operator CType(src as ClassA) as string
return src.toString()
end operator
public shared overrides widening operator Ctype(srtc as string) as ClassA
return new ClassA(src)
end operator

would produce an effect similiar to the following...

m_ClassA = new ClassA(m_ClassB .ToString())

Bill
"Branco Medeiros" wrote:
Bill foust wrote:
<snip>
I have a base class that is common to many other classes.
public class Base
...
end class

I have 2 seperate classes that inherit from base

public ClassA : inherits Base
...
End Class

public ClassB : inherits Base
...
End Class

Now, I have an instance of ClassA and ClassB and I'm trying to assign the
ClassB instance to ClassA.

Dim m_ClassA as new ClassA
dim m_ClassB as new ClassB

m_ClassA = m_ClassB

Obviously, this won't work as is because ClassA is not the same as ClassB.
So we turn to operator overloading. I really want to overload the assignment
operator, but I read that VB.Net doesn't support this. So we have to overload
operator CType.
<snip>

One possible solution would be to have a virtual (Overridable) Assign
method in Base:

Public Overridable Sub Assign(Value As Base)
'...
End Sub

And have each class get from the value whatever they want:

'On ClassB
Public Overrides Sub Assign(Value As Base)
'get items from a Base Class
End Sub

Public Overridable Overloads Sub Assign(Value As ClassB)
Assign(DirectCa st(Value, Base))
'get items that are specific for ClassB types
'...
End Sub

'On ClassA
Public Overrides Sub Assign(Value As Base)
'get items from a Base Class
End Sub

Public Overridable Overloads Sub Assign(Value As ClassA)
Assign(DirectCa st(Value, Base))
'get items that are specific for ClassA types
'...
End Sub
Dim m_ClassA as new ClassA
dim m_ClassB as new ClassB
m_ClassA.Assign (m_ClassB)

HTH,

Regards

Branco

Jul 22 '06 #5
Bill,
| reasons, I'm kind of stuck using operator = to accomplish both a deep copy
| (not a reference copy) and a type cast in one operation.
Why? A square peg (copy/cast) doesn't always fit in a round hole (whatever
you are *actually* attempting to do).
As you show you would need a widening to String, then a narrowing(or
widening) to your specific type.

Unfortunately (or is it fortunately!) VB doesn't apply 2 (user defined)
conversions in a row; Very few languages will apply multiple conversion
operators in a row.

You will need to apply one of the "conversion s" manually:

m_ClassA = m_ClassB.ToStri ng()

or

m_ClassA = CStr(m_ClassB)

However!! conversions here really don't feel appropriate!

| You see, Base contains a string of data and the derived classes simply
| implement a variety of properties to access specific substring portions of
| the string in the class in order to give them a "pretty name".
I would consider making ClassA & ClassB strategies that Base uses. ClassA &
ClassB would not have properties as much as a method (possibly a single
method) that given a name returned the value from the string (the substring
portion)...

| Also, because
| this code is the result of an automatic code generation process, it doesnt
| really have any knowledge about the types of these derived classes.
Is it your code generator or someone elses? If its your's it sounds like it
needs to be smarter...

Overall I would step back, look at what the process is really trying to do,
and pick a solution (algorithm) that solves what you are really trying to
do.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Bill foust" <Bi*******@disc ussions.microso ft.comwrote in message
news:EA******** *************** ***********@mic rosoft.com...
| Thanks for the quick replies! Unfortunately neither suggestion will work
for
| me.
|
| I have to use an operator = to do this assignment and cannot use other
| methods such as you Assign method. That is unless I'm misunderstandin g
what
| you are saying and Assign() exists on all objects and is used by the
compiler
| to implement operator=. The help docs didn't seem to suggest that however.
:)
|
| You see, Base contains a string of data and the derived classes simply
| implement a variety of properties to access specific substring portions of
| the string in the class in order to give them a "pretty name". Also,
because
| this code is the result of an automatic code generation process, it doesnt
| really have any knowledge about the types of these derived classes. For
these
| reasons, I'm kind of stuck using operator = to accomplish both a deep copy
| (not a reference copy) and a type cast in one operation.
|
| I was hoping the usage of operator Ctype for a widening to string and
| another ctype for widening to the specific class (example below)...
|
| public ClassA : inherits Base
| ...
| public shared overrides widening operator CType(src as ClassA) as string
| return src.toString()
| end operator
| public shared overrides widening operator Ctype(srtc as string) as ClassA
| return new ClassA(src)
| end operator
|
| would produce an effect similiar to the following...
|
| m_ClassA = new ClassA(m_ClassB .ToString())
|
| Bill
|
|
| "Branco Medeiros" wrote:
|
| Bill foust wrote:
| <snip>
| I have a base class that is common to many other classes.
| public class Base
| ...
| end class
|
| I have 2 seperate classes that inherit from base
|
| public ClassA : inherits Base
| ...
| End Class
|
| public ClassB : inherits Base
| ...
| End Class
|
| Now, I have an instance of ClassA and ClassB and I'm trying to assign
the
| ClassB instance to ClassA.
|
| Dim m_ClassA as new ClassA
| dim m_ClassB as new ClassB
|
| m_ClassA = m_ClassB
|
| Obviously, this won't work as is because ClassA is not the same as
ClassB.
| So we turn to operator overloading. I really want to overload the
assignment
| operator, but I read that VB.Net doesn't support this. So we have to
overload
| operator CType.
| <snip>
| >
| One possible solution would be to have a virtual (Overridable) Assign
| method in Base:
| >
| Public Overridable Sub Assign(Value As Base)
| '...
| End Sub
| >
| And have each class get from the value whatever they want:
| >
| 'On ClassB
| Public Overrides Sub Assign(Value As Base)
| 'get items from a Base Class
| End Sub
| >
| Public Overridable Overloads Sub Assign(Value As ClassB)
| Assign(DirectCa st(Value, Base))
| 'get items that are specific for ClassB types
| '...
| End Sub
| >
| 'On ClassA
| Public Overrides Sub Assign(Value As Base)
| 'get items from a Base Class
| End Sub
| >
| Public Overridable Overloads Sub Assign(Value As ClassA)
| Assign(DirectCa st(Value, Base))
| 'get items that are specific for ClassA types
| '...
| End Sub
| >
| Dim m_ClassA as new ClassA
| dim m_ClassB as new ClassB
|
| m_ClassA.Assign (m_ClassB)
| >
| HTH,
| >
| Regards
| >
| Branco
| >
| >
Jul 23 '06 #6

Bill foust wrote:
<snip>
I have to use an operator = to do this assignment and cannot use other
methods such as you Assign method. That is unless I'm misunderstandin g what
you are saying and Assign() exists on all objects and is used by the compiler
to implement operator=. The help docs didn't seem to suggest that however. :)
Ha! That would be so conveninent! =))
You see, Base contains a string of data and the derived classes simply
implement a variety of properties to access specific substring portions of
the string in the class in order to give them a "pretty name". Also, because
this code is the result of an automatic code generation process, it doesnt
really have any knowledge about the types of these derived classes. For these
reasons, I'm kind of stuck using operator = to accomplish both a deep copy
(not a reference copy) and a type cast in one operation.
<snip>

But it seems you have access to the classes definitions, though,
otherwise how could you 'append' the implementation of the CType
converter...?

In this case, why don't you have:

In class A
Shared Widening Operator CType(ByVal Other As B) As A
Dim Text As String = 'Gets Text from B
Return New A(Text)
End Operator

In class B
Shared Widening Operator CType(ByVal Other As A) As B
Dim Text As String = 'Gets Text from A
Return New B(Text)
End Operator

Regards,

Branco.

Jul 23 '06 #7

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

Similar topics

9
2219
by: Maedowan | last post by:
Does this code delete right amount of memory: struct SAMPLE{ int a,b,c; char zzz; }; void* ptr; int main() {
2
4034
by: Chris | last post by:
Hi, I try Public Shared Operator +(ByVal Left As Point, ByVal Right As Size) As Point Return New Point(Left.X + Right.Width, Left.Y + Right.Height) End Operator but I get compiler errors. (got code from :
2
2446
by: AMDRIT | last post by:
Hello everyone, I have created a custom component and one of its properties is a class object with it's own properties. During runtime, I can assign values to the class object properties just fine. However, when attempting to assing default values as designtime in the propertygrid, nothing is working on the class object. I know that I am doing it wrong, any ideas what it is? Thanks in advance
3
2029
by: cj | last post by:
If I want to check to see if it's after "11:36 pm" what would I write? I'm sure it's easy but I'm getting tired of having to work with dates and times. Sometimes I just want time or date. And to be able to do comparisons on them.
5
3241
by: Denis Petronenko | last post by:
Hello, how can i read into strings from ifstream? file contains values in following format: value11; val ue12; value 13; valu e21;value22; value23; etc. i need to read like file >string, but strings must be divided by ";" separator.
2
6965
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of attending interviews. If you own a company best way to judge if the candidate is worth of it. http://www.questpond.com/InterviewRatingSheet.zip
2
1284
by: RSH | last post by:
I am trying really hard to grasp the concept of Interfaces and their real world usage. I constructed a simple project of a webpage that contains two controls. I am enabling communication back and forth between the page as well as controls. I used two interfaces to remove the hardcoded references. Is what I'm doing correct? Any suggestions? Thanks!
169
9173
by: JohnQ | last post by:
(The "C++ Grammer" thread in comp.lang.c++.moderated prompted this post). It would be more than a little bit nice if C++ was much "cleaner" (less complex) so that it wasn't a major world wide untaking to create a toolchain for it. Way back when, there used to be something called "Small C". I wonder if the creator(s) of that would want to embark on creating a nice little Small C++ compiler devoid of C++ language features that make...
1
2709
by: SunshineInTheRain | last post by:
The following code is dynamic create dropdownmenu which data within pulled from database However, the code work well on IE but not on Firefox. On Firefox, the whole mouseover and mouseout function din't work. what is not supported on Firefox? the javascript syntax? please help as i am not familiar with javascript. thanks in advanced. this is quite urgent.... please.. By the way hope there has somebody may help me find out why the design may...
0
9554
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10136
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9989
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9925
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9811
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8814
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.