473,387 Members | 1,529 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.

Ctype fire default contructor?

If I am trying to cast an object to one of its decendants, will the
decendants' default constructor (New sub) fire?

Thanks,

Craig
Nov 20 '05 #1
12 1864
I don't think so...that would result in allocating memory and copying the
parent object to the decendent object. which seems wasteful... then again,
what do I know. =)

I would lead to no... but someone will tell me I'm wrong in aminute.

"Craig Buchanan" <so*****@microsoft.com> wrote in message
news:uw*************@TK2MSFTNGP09.phx.gbl...
If I am trying to cast an object to one of its decendants, will the
decendants' default constructor (New sub) fire?

Thanks,

Craig

Nov 20 '05 #2
"Craig Buchanan" <so*****@microsoft.com> schrieb
If I am trying to cast an object to one of its decendants, will
the decendants' default constructor (New sub) fire?


If you /cast/ an object: no. Casting does not create a new object, it only
changes the type of the reference. CType, as mentioned in the subject, can
do both (in opposite to Directcast that only casts): cast and convert (often
mixed up). If CType creates a new object, yes, the constructor will be
called. If CType only casts: no new instance -> no constructor called
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #3
Alright, I don't understand that...

How does Ctype call the constructor? What if there is no argumentless
constructor? When calling the constructor what happens to the old allocated
memory? is that object just lost and cleaned up by GC?

Sorry for all the questions Armin, I just dont understand how it works.

-CJ
"Armin Zingler" <az*******@freenet.de> wrote in message
news:40***********************@news.freenet.de...
"Craig Buchanan" <so*****@microsoft.com> schrieb
If I am trying to cast an object to one of its decendants, will
the decendants' default constructor (New sub) fire?
If you /cast/ an object: no. Casting does not create a new object, it only
changes the type of the reference. CType, as mentioned in the subject, can
do both (in opposite to Directcast that only casts): cast and convert

(often mixed up). If CType creates a new object, yes, the constructor will be
called. If CType only casts: no new instance -> no constructor called
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #4
You cannot cast an object to one of its descendants (you can to an
ancestor). You can assign a reference from a descendant to an ancestor
however, which implies the descendant has already been created and (thus)
its constructor called.

"Craig Buchanan" <so*****@microsoft.com> wrote in message
news:uw*************@TK2MSFTNGP09.phx.gbl...
If I am trying to cast an object to one of its decendants, will the
decendants' default constructor (New sub) fire?

Thanks,

Craig

Nov 20 '05 #5

"CJ Taylor" <no****@blowgoats.com> schrieb im Newsbeitrag
news:10*************@corp.supernews.com...
I don't think so...that would result in allocating memory and copying the
parent object to the decendent object. which seems wasteful... then again, what do I know. =)
*lol*

I would lead to no... but someone will tell me I'm wrong in aminute.

"Craig Buchanan" <so*****@microsoft.com> wrote in message
news:uw*************@TK2MSFTNGP09.phx.gbl...
If I am trying to cast an object to one of its decendants, will the
decendants' default constructor (New sub) fire?

Thanks,

Craig


Nov 20 '05 #6

"CJ Taylor" <no****@blowgoats.com> schrieb im Newsbeitrag
news:10*************@corp.supernews.com...
I don't think so...that would result in allocating memory and copying the
parent object to the decendent object. which seems wasteful... then again, what do I know. =)
*lol*

I would lead to no... but someone will tell me I'm wrong in aminute.

"Craig Buchanan" <so*****@microsoft.com> wrote in message
news:uw*************@TK2MSFTNGP09.phx.gbl...
If I am trying to cast an object to one of its decendants, will the
decendants' default constructor (New sub) fire?

Thanks,

Craig


Nov 20 '05 #7
"CJ Taylor" <no****@blowgoats.com> schrieb
Alright, I don't understand that...

How does Ctype call the constructor? What if there is no
argumentless constructor? When calling the constructor what happens
to the old allocated memory? is that object just lost and cleaned up
by GC?

Sorry for all the questions Armin, I just dont understand how it
works.


Very good question. :) Well, Ctype does create new Single, Double,
Integer,... objects but they don't seem to have a real constructor. I
thought there are other conversions that create new objects, but I didn't
find a special case, so, if anybody has one for us ....
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #8
"CJ Taylor" <no****@blowgoats.com> schrieb
Alright, I don't understand that...

How does Ctype call the constructor? What if there is no
argumentless constructor? When calling the constructor what happens
to the old allocated memory? is that object just lost and cleaned up
by GC?

Sorry for all the questions Armin, I just dont understand how it
works.


Very good question. :) Well, Ctype does create new Single, Double,
Integer,... objects but they don't seem to have a real constructor. I
thought there are other conversions that create new objects, but I didn't
find a special case, so, if anybody has one for us ....
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #9
Hey Armin,

Thanks for the response, you bring up a really good point about singles,
doubles, integers, etc. Something I've been thinking about ironically.

Alright, so lookup in help for System.Int32, says its a structure. I can
believe this, and this also makes sense with the allocating and deallocating
of memory dynamically during the runtime and calling of a default
constructor as explanined here

http://www.vbdotnetheaven.com/Code/Jun2003/2036.asp

This is what I don't understand, look up System.Int32, it says it inherits
from System.ValueType. I take it this is implicilty done by the compiler?
I thought the only one that did that in a managed environment was
System.Object. So how can it inherit?

As for the calling of new, with the implicit constructur (because I just
found out you cannot create an argumentless constructor in a struct) that
now makes sense. But created on the stack instead of the heap.

so I suppose it can be concluded that any structure can be created using
CType because of an implicity constructor that no one talks about. =) But
classes cannot.

Facinating isn't it? =)

-CJ

"CJ Taylor" <no****@blowgoats.com> schrieb
Alright, I don't understand that...

How does Ctype call the constructor? What if there is no
argumentless constructor? When calling the constructor what happens
to the old allocated memory? is that object just lost and cleaned up
by GC?

Sorry for all the questions Armin, I just dont understand how it
works.


Very good question. :) Well, Ctype does create new Single, Double,
Integer,... objects but they don't seem to have a real constructor. I
thought there are other conversions that create new objects, but I didn't
find a special case, so, if anybody has one for us ....
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #10
Hey Armin,

Thanks for the response, you bring up a really good point about singles,
doubles, integers, etc. Something I've been thinking about ironically.

Alright, so lookup in help for System.Int32, says its a structure. I can
believe this, and this also makes sense with the allocating and deallocating
of memory dynamically during the runtime and calling of a default
constructor as explanined here

http://www.vbdotnetheaven.com/Code/Jun2003/2036.asp

This is what I don't understand, look up System.Int32, it says it inherits
from System.ValueType. I take it this is implicilty done by the compiler?
I thought the only one that did that in a managed environment was
System.Object. So how can it inherit?

As for the calling of new, with the implicit constructur (because I just
found out you cannot create an argumentless constructor in a struct) that
now makes sense. But created on the stack instead of the heap.

so I suppose it can be concluded that any structure can be created using
CType because of an implicity constructor that no one talks about. =) But
classes cannot.

Facinating isn't it? =)

-CJ

"CJ Taylor" <no****@blowgoats.com> schrieb
Alright, I don't understand that...

How does Ctype call the constructor? What if there is no
argumentless constructor? When calling the constructor what happens
to the old allocated memory? is that object just lost and cleaned up
by GC?

Sorry for all the questions Armin, I just dont understand how it
works.


Very good question. :) Well, Ctype does create new Single, Double,
Integer,... objects but they don't seem to have a real constructor. I
thought there are other conversions that create new objects, but I didn't
find a special case, so, if anybody has one for us ....
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #11
Armin,
Very good question. :)
You need to overload the CType operator for the Class or Structure & define
how to construct the new object, I normally use the constructor that accepts
the type of the conversion.

Of course you need Whidbey (VS.NET 2005) to define overloaded CType
operators.

In the PDC & CTP releases of Whidbey Overloading the CType operator looks
like:

Public Class Something

Public Sub New(ByVal value As Boolean)
End Sub

Public Shared Widening Operator CType(ByVal rhs As Boolean) As
Something
Return New Something(rhs)
End Operator

End Class

There is also a Narrowing CType Operator.

Overloading CType (IMHO) is the reason to be aware of when you want to Cast
(DirectCast) and when you want to Convert (CType).

Hope this helps
Jay

"Armin Zingler" <az*******@freenet.de> wrote in message
news:40***********************@news.freenet.de... "CJ Taylor" <no****@blowgoats.com> schrieb
Alright, I don't understand that...

How does Ctype call the constructor? What if there is no
argumentless constructor? When calling the constructor what happens
to the old allocated memory? is that object just lost and cleaned up
by GC?

Sorry for all the questions Armin, I just dont understand how it
works.


Very good question. :) Well, Ctype does create new Single, Double,
Integer,... objects but they don't seem to have a real constructor. I
thought there are other conversions that create new objects, but I didn't
find a special case, so, if anybody has one for us ....
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #12
Armin,
Very good question. :)
You need to overload the CType operator for the Class or Structure & define
how to construct the new object, I normally use the constructor that accepts
the type of the conversion.

Of course you need Whidbey (VS.NET 2005) to define overloaded CType
operators.

In the PDC & CTP releases of Whidbey Overloading the CType operator looks
like:

Public Class Something

Public Sub New(ByVal value As Boolean)
End Sub

Public Shared Widening Operator CType(ByVal rhs As Boolean) As
Something
Return New Something(rhs)
End Operator

End Class

There is also a Narrowing CType Operator.

Overloading CType (IMHO) is the reason to be aware of when you want to Cast
(DirectCast) and when you want to Convert (CType).

Hope this helps
Jay

"Armin Zingler" <az*******@freenet.de> wrote in message
news:40***********************@news.freenet.de... "CJ Taylor" <no****@blowgoats.com> schrieb
Alright, I don't understand that...

How does Ctype call the constructor? What if there is no
argumentless constructor? When calling the constructor what happens
to the old allocated memory? is that object just lost and cleaned up
by GC?

Sorry for all the questions Armin, I just dont understand how it
works.


Very good question. :) Well, Ctype does create new Single, Double,
Integer,... objects but they don't seem to have a real constructor. I
thought there are other conversions that create new objects, but I didn't
find a special case, so, if anybody has one for us ....
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #13

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

Similar topics

10
by: Douglas Buchanan | last post by:
I am using the following code instead of a very lengthly select case statement. (I have a lot of lookup tables in a settings form that are selected from a ListBox. The data adapters are given a...
3
by: al | last post by:
class Base { public: //Base() {} Base(int m) {} }; class Derive : public Base {
7
by: Gordon Smith | last post by:
I have four (4) ASP.NET Web applications/Web sites on a IIS/6 - Windows Server 2003 production server. 3 of them work fine. I just installed the 4th one and it's Application_Start event is not...
3
by: Mark Kamoski | last post by:
Hi-- What is the difference between Convert.ToString(obj) and CType(obj, String)? (Assume obj is a variable of type Object.) Please advise. Thank you.
16
by: Craig Buchanan | last post by:
If I am trying to cast an object to one of its decendants, will the decendants' default constructor (New sub) fire? Thanks, Craig
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...

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.