Ctype fire default contructor?

  • Thread starter Thread starter Craig Buchanan
  • Start date Start date
C

Craig Buchanan

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

Thanks,

Craig
 
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 said:
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
 
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
 
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.
 
CJ Taylor said:
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.
 
CJ Taylor said:
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
 
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
 
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
 
Back
Top