Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / November 2003

Tip: Looking for answers? Try searching our database.

Boxing... Casting... Converting!

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Zanna - 29 Nov 2003 20:45 GMT
Hi all,

A question: when I have 2 values (let's say A and B) and want to assign A to
B, if A and B are of different type I need to (please say me if I'm wrong)

// pseudo code
// Let B = A
if A is a value type
   do a boxing of A
else
   do a casting of A to type of B

We know, this may fail (i.e. we cannot cast an Int32 to a Boolean), so we
may require the Convert class to operate.

The question is: how can I determine if for passing from A to B I need to do
a boxing, casting or converting?

Thanks!

--
Math Parser : http://www.neodatatype.net
Michael Giagnocavo [MVP] - 30 Nov 2003 01:58 GMT
A only needs to be boxed if it's going to be used as an object.  If you are
converting from a long to an int, it's a simple cast (in C#), and in fact
there are specific IL opcodes for those cases (conv.* opcodes).

Boxing takes a value type, then sticks it inside an object (hence the term
boxing).  This is needed if you do something that takes an object parameter,
and you're passing a valuetype.  The boxing happens automatically (for C#).
When you take it back out of an object, you unbox.  This requires you do a
cast, for instance int i = (int)myArrayList[0]; (C#).

So, you generally don't need to worry about what to do; the compiler will do
it for you or give you an error if it's an invalid cast and it can detect
it.

-mike
MVP

> Hi all,
>
[quoted text clipped - 18 lines]
> --
> Math Parser : http://www.neodatatype.net
Zanna - 30 Nov 2003 09:10 GMT
"Michael Giagnocavo [MVP]" <mggUNSPAM@Atrevido.net> wrote in message

> A only needs to be boxed if it's going to be used as an object.  If you are
> converting from a long to an int, it's a simple cast (in C#), and in fact
> there are specific IL opcodes for those cases (conv.* opcodes).

Thank you, now it's more clear.
I'm talking about IL, not C#, so I would know how things going on in there
:)

See you

--
Math Parser : http://www.neodatatype.net
Sito Comune : http://www.it-lang-vb.net
Zanna - 30 Nov 2003 09:48 GMT
"Zanna" <znt.fabio@virgilio.it> wrote in message
news:bqcc2e$1vsm0c$1@ID-184808.news.uni-

> I'm talking about IL, not C#, so I would know how things going on in there

Another question: with IL, how can I do a conv. from... let's say from Int32
to System.Drawing.Color?

Thanks again

--
Math Parser : http://www.neodatatype.net
Mattias Sj?gren - 30 Nov 2003 15:57 GMT
>Another question: with IL, how can I do a conv. from... let's say from Int32
>to System.Drawing.Color?

The conv opcodes are only for primitive types. For Int32 -> Color
you'd call Color::FromArgb or use the ColorTranslator class.

Mattias

Signature

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

Michael Giagnocavo [MVP] - 30 Nov 2003 18:40 GMT
To learn easily, write samples in C#, then compile and ILDASM to see exactly
what's going on.
-mike
MVP

> "Michael Giagnocavo [MVP]" <mggUNSPAM@Atrevido.net> wrote in message
>
[quoted text clipped - 12 lines]
> Math Parser : http://www.neodatatype.net
> Sito Comune : http://www.it-lang-vb.net

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.