C# - reference to a variable?

Started by
11 comments, last by Shiny 16 years, 2 months ago
Quote:Original post by Koobazaur
Sure, maybe C++ is ladden with memory voulnerabilities and unsafe code but, damn, at least when I had ** ppSomething I knew exactly what was going on instead of having to wonder what is referencing what (not to mention extra flexibility).
Keep in mind how long it took you to understand how that worked, though. C# is preposterously easy and very elegant once it clicks; I literally can't write C++ anymore.

http://edropple.com
Advertisement
Quote:Original post by Koobazaur
at least when I had ** ppSomething I knew exactly what was going on instead of having to wonder what is referencing what (not to mention extra flexibility). And wtf is up with the lack of template, errr, generic specialization? Seriously...


You have nearly the exact same flexibility in C# and you can know "exactly what is going on" with great certainty once you learn the .NET model just as you learned the C++ model.

C# can accomplish non-partial generics specialization through inheritance. For example:

class SpecializedForBar : Foo<Bar>
{
//override virtual members of Foo
}
       public void Update(){                    Graphics Graph = Graphics.FromImage(_DrawArea); // this throws exception if the DrawArea I passed in constructor gets deleted and re-created    Pen MyPen = new Pen(Color.Blue); //yaya I know would be more efficient to story this as private member, but optimizations come later    Graph.DrawEllipse(MyPen, 0, 0, 10, 10);    MyPen.Dispose();                         }


Just thought I should make a point -- you're clearing up after your pen right here, but not your System.Drawing.Graphics object that you're drawing on -- this is going to be a potential problem later if you're not careful. Anytime you use a graphics object that you didn't get given (i.e., in a paint() method you get given the surface that GDI+ is looking after...) you ought to manually call dispose() so that things don't end up leaking later on :)

This is a fact of life of using GDI+ -- 'tis a C++ library wrapped up in .NET but still must use unmanaged memory for certain stuff (Bitmaps ought to be disposed too, btw).

Anyways, 'twas my two cents. I second the proposition of inheriting from an existing container, too -- save you lots of work. Drawing an Image (system.drawing.image)/Bitmap onto a panel is as easy as grabbing the paint code for said panel and squishing in something along the lines of

Given some PaintEventArgs e -- e.Graphics.DrawImage(Image i);

and voila. DrawImage is overloaded so you can specify things such as height/width/graphics unit etc.

~Shiny
------------'C makes it easy to shoot yourself in the foot. C++ makes it harder, but when you do, it blows away your whole leg.' -Bjarne Stroustrup

This topic is closed to new replies.

Advertisement