473,842 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Constructor Overloading Bug?

Hi,

does anybody know, why the second contructor isn't called in this example?
Are you able to reproduce this bug?
Thanks

Klaus

Public Class Something

Shared Sub Main()
Dim test1 As New Something("C"c, 1)
Dim test2 As New Something("Stri ng", 2)
End Sub

Sub New(ByVal aChar As Char, ByVal aByte As Byte)
Console.WriteLi ne("Char/Byte-Konstruktor")
End Sub

Sub New(ByVal aString As String, ByVal anInteger As Integer)
Console.WriteLi ne("String/Integer-Konstruktor")
End Sub

End Class
Nov 20 '05 #1
14 1743
....actually, it is the first constructor, that is not being called...

Klaus

"Klaus Löffelmann" <fo***********@ loeffelmann.de> schrieb im Newsbeitrag
news:40******** *************** @news.freenet.d e...
Hi,

does anybody know, why the second contructor isn't called in this example?
Are you able to reproduce this bug?
Thanks

Klaus

Public Class Something

Shared Sub Main()
Dim test1 As New Something("C"c, 1)
Dim test2 As New Something("Stri ng", 2)
End Sub

Sub New(ByVal aChar As Char, ByVal aByte As Byte)
Console.WriteLi ne("Char/Byte-Konstruktor")
End Sub

Sub New(ByVal aString As String, ByVal anInteger As Integer)
Console.WriteLi ne("String/Integer-Konstruktor")
End Sub

End Class

Nov 20 '05 #2
Klaus,
does anybody know, why the second contructor isn't called in this example?
Are you able to reproduce this bug?

If you force the constant 1 to be a Byte (it's an Integer by default),
the first constructor will be called.

Dim test1 As New Something("C"c, CByte(1))

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 20 '05 #3
Cor
Hi Klaus,

I do not know what is it theoretical, but this he does well,
Dim test1 As New Something("C"c, CByte(1))

Just if it first trys to resolve to find if it is an integer and than
threats the character as a one position string.

I hope this helps something.

Cor
does anybody know, why the second contructor isn't called in this example?
Are you able to reproduce this bug?
Thanks

Nov 20 '05 #4
Mattias,

why do I have to force the second parameter to be a byte, when it's already
clear that I'm pushing a char, to begin with. I mean, if you declare a
"normal" procedure to take a char and you then try to give it a string, the
compiler is complaining. So why can't he then tell the difference in this
example?

Aside of that: I tried what you suggested in my project already (this is
just an excerpt), and of course converted the second parameter to a byte. It
called the string/Integer constructor, anyway. Only after I changed the
order, I got it to work. But the curious thing is: When I rechanged the
order, it suddenly could recognize the byte as a byte, and now I can't
reproduce the behaviour anymore.

However, the behaviour described in this exceprt is weird enough, isn't it?

Klaus

"Mattias Sjögren" <ma************ ********@mvps.o rg> schrieb im Newsbeitrag
news:er******** ******@TK2MSFTN GP12.phx.gbl...
Klaus,
does anybody know, why the second contructor isn't called in this example?Are you able to reproduce this bug?

If you force the constant 1 to be a Byte (it's an Integer by default),
the first constructor will be called.

Dim test1 As New Something("C"c, CByte(1))

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 20 '05 #5
Klaus,
Dim test1 As New Something("C"c, 1) 1 is an Integer literal, Char can be upgraded to a string, but an Integer
cannot be downgraded to a Byte, hence the second constructor is called.

I don't see how to specify a Byte literal itself, so try:
Dim test1 As New Something("C"c, CByte(1))
Alternatively add a third constructor (or change the first constructor),
such as: Sub New(ByVal aChar As Char, ByVal anInteger As Integer)
Console.WriteLi ne("Char/Integer-Konstruktor")
End Sub
I would probably simply define two constructors Char/Integer &
String/Integer constructors to "avoid" the problem...

Hope this helps
Jay

"Klaus Löffelmann" <fo***********@ loeffelmann.de> wrote in message
news:40******** *************** @news.freenet.d e... Hi,

does anybody know, why the second contructor isn't called in this example?
Are you able to reproduce this bug?
Thanks

Klaus

Public Class Something

Shared Sub Main()
Dim test1 As New Something("C"c, 1)
Dim test2 As New Something("Stri ng", 2)
End Sub

Sub New(ByVal aChar As Char, ByVal aByte As Byte)
Console.WriteLi ne("Char/Byte-Konstruktor")
End Sub

Sub New(ByVal aString As String, ByVal anInteger As Integer)
Console.WriteLi ne("String/Integer-Konstruktor")
End Sub

End Class

Nov 20 '05 #6
Klaus,
What do you mean "changed the order"?

Do you mean you made the String/Integer come before the Char/Byte in the
source?

Or that you had Integer/String & Byte/Char constructors?

As I stated in my other post, the 1 is being treated as an Integer Literal
first, VB.NET knows it can convert the Char to a string, hence it is picking
the String/Integer overload!

Is this a bug, possible! is this "by design", possible! I know enough about
implementing overloading in a compiler to understand it can be very
difficult to get overload resolution to work.

Here are a number of articles on VB.NET overload method resolution:
http://msdn.microsoft.com/library/de...bspec9_3_4.asp

http://msdn.microsoft.com/library/de...procedures.asp

Mostly this one:
http://msdn.microsoft.com/library/de...resolution.asp

I would say it is seeing the 1 as an integer, and according to rule 2 on the
last link is throwing away the Char/Byte overload.

Hope this helps
Jay

"Klaus Löffelmann" <fo***********@ loeffelmann.de> wrote in message
news:40******** *************** @news.freenet.d e...
Mattias,

why do I have to force the second parameter to be a byte, when it's already clear that I'm pushing a char, to begin with. I mean, if you declare a
"normal" procedure to take a char and you then try to give it a string, the compiler is complaining. So why can't he then tell the difference in this
example?

Aside of that: I tried what you suggested in my project already (this is
just an excerpt), and of course converted the second parameter to a byte. It called the string/Integer constructor, anyway. Only after I changed the
order, I got it to work. But the curious thing is: When I rechanged the
order, it suddenly could recognize the byte as a byte, and now I can't
reproduce the behaviour anymore.

However, the behaviour described in this exceprt is weird enough, isn't it?
Klaus

"Mattias Sjögren" <ma************ ********@mvps.o rg> schrieb im Newsbeitrag
news:er******** ******@TK2MSFTN GP12.phx.gbl...
Klaus,
does anybody know, why the second contructor isn't called in this example?Are you able to reproduce this bug?

If you force the constant 1 to be a Byte (it's an Integer by default),
the first constructor will be called.

Dim test1 As New Something("C"c, CByte(1))

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Nov 20 '05 #7
Nope,

I was wrong. Chars get promoted to Strings, when VB feels to do so. That was
my mistake.
But consistently, overloaded procedures only differing in char and string
shouldn't be allowed then, anyway.

Thanks, everybody

Klaus
"Klaus Löffelmann" <fo***********@ loeffelmann.de> schrieb im Newsbeitrag
news:40******** *************** @news.freenet.d e...
Mattias,

why do I have to force the second parameter to be a byte, when it's already clear that I'm pushing a char, to begin with. I mean, if you declare a
"normal" procedure to take a char and you then try to give it a string, the compiler is complaining. So why can't he then tell the difference in this
example?

Aside of that: I tried what you suggested in my project already (this is
just an excerpt), and of course converted the second parameter to a byte. It called the string/Integer constructor, anyway. Only after I changed the
order, I got it to work. But the curious thing is: When I rechanged the
order, it suddenly could recognize the byte as a byte, and now I can't
reproduce the behaviour anymore.

However, the behaviour described in this exceprt is weird enough, isn't it?
Klaus

"Mattias Sjögren" <ma************ ********@mvps.o rg> schrieb im Newsbeitrag
news:er******** ******@TK2MSFTN GP12.phx.gbl...
Klaus,
does anybody know, why the second contructor isn't called in this example?Are you able to reproduce this bug?

If you force the constant 1 to be a Byte (it's an Integer by default),
the first constructor will be called.

Dim test1 As New Something("C"c, CByte(1))

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Nov 20 '05 #8
Jay,

Sorry, I replied and then read you message - I was out of sync, so to say.
The Integer and Byte stuff was totally clear to me; it's just, as I already
pointed out, that overloading procedures just with a different char and
string parameter shouldn't be allowed then, isn't it?

Thanks for your help,

Klaus

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> schrieb im
Newsbeitrag news:up******** ******@TK2MSFTN GP11.phx.gbl...
Klaus,
What do you mean "changed the order"?

Do you mean you made the String/Integer come before the Char/Byte in the
source?

Or that you had Integer/String & Byte/Char constructors?

As I stated in my other post, the 1 is being treated as an Integer Literal
first, VB.NET knows it can convert the Char to a string, hence it is picking the String/Integer overload!

Is this a bug, possible! is this "by design", possible! I know enough about implementing overloading in a compiler to understand it can be very
difficult to get overload resolution to work.

Here are a number of articles on VB.NET overload method resolution:
http://msdn.microsoft.com/library/de...bspec9_3_4.asp
http://msdn.microsoft.com/library/de...procedures.asp
Mostly this one:
http://msdn.microsoft.com/library/de...resolution.asp
I would say it is seeing the 1 as an integer, and according to rule 2 on the last link is throwing away the Char/Byte overload.

Hope this helps
Jay

"Klaus Löffelmann" <fo***********@ loeffelmann.de> wrote in message
news:40******** *************** @news.freenet.d e...
Mattias,

why do I have to force the second parameter to be a byte, when it's already
clear that I'm pushing a char, to begin with. I mean, if you declare a
"normal" procedure to take a char and you then try to give it a string,

the
compiler is complaining. So why can't he then tell the difference in this example?

Aside of that: I tried what you suggested in my project already (this is
just an excerpt), and of course converted the second parameter to a byte. It
called the string/Integer constructor, anyway. Only after I changed the
order, I got it to work. But the curious thing is: When I rechanged the
order, it suddenly could recognize the byte as a byte, and now I can't
reproduce the behaviour anymore.

However, the behaviour described in this exceprt is weird enough, isn't

it?

Klaus

"Mattias Sjögren" <ma************ ********@mvps.o rg> schrieb im

Newsbeitrag news:er******** ******@TK2MSFTN GP12.phx.gbl...
Klaus,

>does anybody know, why the second contructor isn't called in this

example?
>Are you able to reproduce this bug?
If you force the constant 1 to be a Byte (it's an Integer by default),
the first constructor will be called.

Dim test1 As New Something("C"c, CByte(1))

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.



Nov 20 '05 #9
"Klaus Löffelmann" <fo***********@ loeffelmann.de> schrieb
...actually, it is the first constructor, that is not being
called...


Dim test1 As New Something("C"c, 1)

The number 1 is interpreted as integer, not as byte. That's why the second
constructor is called - the Char "C"c is converted to a String because
widening conversions are allowed. Calling the first constructor would be a
narrowing conversion.

Use
Dim test1 As New Something("C"c, CByte(1))
instead.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
Nov 20 '05 #10

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

Similar topics

2
5266
by: Sergey Krushinsky | last post by:
Hello all, Is there a common way to emulate constructor overloading in Python class? For instanse, I have 3 classes: 1/ Polar - to hold polar coordinates; 2/ Cartesian - to hold cartesian coordinates; 3/ Coordinates3D, which holds synchronized instances of the both in __p__ and __c__ fields respectively.
26
21350
by: Paul | last post by:
public class A { public A () { // here I would like to call the second version of _ctor, how to accomplish this ? } public A (int a, int b, int c) {
19
3587
by: Martin Oddman | last post by:
Hi, I have a compiling problem. Please take a look at the code below. I have an application that is built upon three tiers: one data tier (Foo.DataManager), one business tier (Foo.Kernel) and one web presentation tier (Foo.WebFiles). The data tier shall only be accessible thru the business tier so I do NOT want a reference to the data tier in the presentation tier. In the business tier I have a class with the name CategoryItem that...
8
2258
by: Sam Sungshik Kong | last post by:
Hello! I am wondering what's the best practice about setting object data using constructor parameters. Let's say that I am creating a Person class. class Person { private string name;
31
5203
by: Peter E. Granger | last post by:
I'm fairly new to C++ and VC++, but for the most part it seems to do most of the same things that can be done in Java, with just some syntactic and structural adjustments. However, one thing I haven't been able to figure out is how to call one constructor from another within a class. It's easy enough to call the base class's constructor from the derived class, but that's not what I'm trying to do. For example, in Java (or J#) it's easy...
7
2376
by: Doug | last post by:
Hi I am working through a learning module privately where there is discussion about overloading the constructor. Could someone please let me know the benefit or purpose of doing this in a class? Thanks
7
4162
by: coinjo | last post by:
i have to overload a constructor that takes a constant static public data member of a class... Can anybody tell me its syntax of prototype and implementation?
22
3635
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float v);
12
7227
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? class Trial { public: Trial() {
0
9865
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
10936
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...
1
10669
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
10303
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
7025
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5695
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
5882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4085
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.