PC Review


Reply
Thread Tools Rate Thread

Ctype fire default contructor?

 
 
Craig Buchanan
Guest
Posts: n/a
 
      5th Apr 2004
If I am trying to cast an object to one of its decendants, will the
decendants' default constructor (New sub) fire?

Thanks,

Craig


 
Reply With Quote
 
 
 
 
CJ Taylor
Guest
Posts: n/a
 
      5th Apr 2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> If I am trying to cast an object to one of its decendants, will the
> decendants' default constructor (New sub) fire?
>
> Thanks,
>
> Craig
>
>



 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      5th Apr 2004
"Craig Buchanan" <(E-Mail Removed)> 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

 
Reply With Quote
 
CJ Taylor
Guest
Posts: n/a
 
      5th Apr 2004
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" <(E-Mail Removed)> wrote in message
news:407180a6$0$26190$(E-Mail Removed)...
> "Craig Buchanan" <(E-Mail Removed)> 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
>



 
Reply With Quote
 
Robin Tucker
Guest
Posts: n/a
 
      5th Apr 2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> If I am trying to cast an object to one of its decendants, will the
> decendants' default constructor (New sub) fire?
>
> Thanks,
>
> Craig
>
>



 
Reply With Quote
 
Klaus Löffelmann
Guest
Posts: n/a
 
      5th Apr 2004

"CJ Taylor" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > If I am trying to cast an object to one of its decendants, will the
> > decendants' default constructor (New sub) fire?
> >
> > Thanks,
> >
> > Craig
> >
> >

>
>



 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      5th Apr 2004
"CJ Taylor" <(E-Mail Removed)> 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

 
Reply With Quote
 
CJ Taylor
Guest
Posts: n/a
 
      6th Apr 2004
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" <(E-Mail Removed)> 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
>



 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      9th Apr 2004
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" <(E-Mail Removed)> wrote in message
news:40726328$0$13064$(E-Mail Removed)...
> "CJ Taylor" <(E-Mail Removed)> 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: WHY SendObject Did not Fire Off Outlook (Default Email) Rick Brandt Microsoft Access Form Coding 0 6th Mar 2009 12:12 PM
CLI and calling the parent contructor DaTurk Microsoft VC .NET 1 21st Jun 2007 01:17 AM
Outlook should fire reminders in non default folders calendars =?Utf-8?B?RnJhbg==?= Microsoft Outlook Calendar 1 14th Jun 2005 11:58 AM
CreateInstance Contructor Parameters?? Bud via DotNetMonster.com Microsoft Dot NET Framework Forms 3 6th May 2005 08:35 AM
Re: Contructor not called? Mattias Sjögren Microsoft C# .NET 8 18th Aug 2003 09:04 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:56 AM.