473,699 Members | 2,838 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple boxing question

Please excuse the terribly 'newbie'ness of this question, unfortunately my C# is very rusty..
What I'm trying to do is write a simple interactive drawing application where a few lines can be moved around on the form. What I'm
trying to do is in the MouseDown() event hit test each line point and when I find one, store a _reference_ to it. The MouseMove
event then checks to see if this reference has been set, if so it will edit this point and cause a redraw which should use the new
coordinates since I'm using a reference to the original point.
The way I understand it is that PointF is a value type and as such I have to box it using (object)Point; and store it in an Object
variable in order to create a reference to it. I then unbox it using PointF p = Point; in MouseMove to edit it's fields, however
this is not editing my original line. Is it just my syntax or am I 'just not getting it'? If this is the case does anyone know of
a good reference for boxing and unboxing and what they're actually used for. I could easily write a simple point class which would
already be an object but I'm curious as to how this would be achieved with value types.
Cheers,

Mike
Nov 17 '05 #1
2 1287
"Mike D Sutton" <ED***@mvps.org > wrote:
["boxing" a value doesn't turn it into a reference
to that specific instance of value]


C# is a fairly rigorously object-oriented language, so everything can
be treated as an object. Even a simple value type like an integer can
be treated like an object (reference type) - for instance, you can
write:

int i = 5;
string s = i.ToString(); // calling a method on an int

However, general performance would be very poor if all of this
object-baggage was always passed around with such simple value types,
so behind the scenes your simple native types are "boxed" (put inside
object-wrappers) when an object-operation is required (like the method
call on "i" above). In other words, they don't have the object-baggage
until it's needed.

What boxing *doesn't* do is create a reference to the specific
instance of the value type's value. That is, you can't (as you seem to
be trying to) cast a value type variable to a reference type and
expect changes to the reference to propagate back to the original
value.

PointF is just a pair of co-ordinates - a way of indicating a position
on the screen - so you don't need it to be a reference. You can either
use those co-ordinates to decide where to draw when you come to update
your line, or - if you're a bit more ambitious - create some sort of
Line class of your own (classes are always reference types!) and make
that redraw its own line - or raise an appropriate event so that the
form can do so - whenever either of its co-ordinates is changed.

P.
Nov 17 '05 #2
> C# is a fairly rigorously object-oriented language, so everything can
be treated as an object. Even a simple value type like an integer can
be treated like an object (reference type) - for instance, you can
write:

int i = 5;
string s = i.ToString(); // calling a method on an int

However, general performance would be very poor if all of this
object-baggage was always passed around with such simple value types,
so behind the scenes your simple native types are "boxed" (put inside
object-wrappers) when an object-operation is required (like the method
call on "i" above). In other words, they don't have the object-baggage
until it's needed.

What boxing *doesn't* do is create a reference to the specific
instance of the value type's value. That is, you can't (as you seem to
be trying to) cast a value type variable to a reference type and
expect changes to the reference to propagate back to the original
value.

PointF is just a pair of co-ordinates - a way of indicating a position
on the screen - so you don't need it to be a reference. You can either
use those co-ordinates to decide where to draw when you come to update
your line, or - if you're a bit more ambitious - create some sort of
Line class of your own (classes are always reference types!) and make
that redraw its own line - or raise an appropriate event so that the
form can do so - whenever either of its co-ordinates is changed.


Yup, writing a simple point and/or line class would be an easy solution, I was just trying to use some of the features of the
language but it appears my understanding of what it's up to was off - Kept going back to that "everything is an object" line that
all the C# books used to say, they just forgot to add "but they can't always be used like objects.." to the end, or perhaps I was
just reading too much into it. ;)
Even a pointer would suffice for this but it seems that pointers can only be used with unsafe code in C#, is that right?
Ah well, not to worry it's only a simple little demo I'm throwing together so I'll just go the classes route which should suit my
needs.
Cheers,

Mike
Nov 17 '05 #3

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

Similar topics

14
323
by: Lora Connors | last post by:
What is Boxing & UnBoxing in .NET?
5
1760
by: Craig | last post by:
Hi everyone, As a relative new-comer to the wonderful world of .NET Framework and the C# langauge I have come across something that I would like to clarify (hopefully with the help of kind people such as yourselves). To quote, "Everything in C# is an object" (including "primitive" value types, primitive being in quotations because this is what my question concerns).
1
2218
by: Tom | last post by:
Couple of questions relating to boxing. Firstly, I already know that boxing is the processing of temporarily copying a ValueType (e.g. struct, enum) to the heap so that the system can treat a value type like a reference type. However, I have some questions relating to implicit boxing: 1. If I add custom instance method on a struct, will it box that type each time the method is called? For example, suppose I have the following: public...
94
5668
by: Peter Olcott | last post by:
How can I create an ArrayList in the older version of .NET that does not require the expensive Boxing and UnBoxing operations? In my case it will be an ArrayList of structures of ordinal types. Thanks.
0
8685
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
9171
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...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8905
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
8880
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
7743
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5869
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
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3053
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

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.