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

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 1273
"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
by: Lora Connors | last post by:
What is Boxing & UnBoxing in .NET?
5
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...
1
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...
94
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. ...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
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...

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.