Original Post
I am learning C# right now, coming from C++, and encountering a problem. I have a Form class that has a bitmap called DrawArea that it draws to the screen OnPaint. Further, I have a Renderer object that stores a reference to that DrawArea so it can draw to it. What I do is I pass the DrawArea to my Renderer, by reference, and set it to Renderer's private variable. Thus, the Renderer has a reference that points to the instance of DrawArea. This, however, is NOT what I want. You see, when my window resize, I delete my DrawArea and recreate it. Hence, when that happens, the Renderer's reference points to a, now obsolete, Bitmap*. Is there any way to have a reference point to an actual variable (other reference), rather than an instance in memory? Or do I need to use the "unsafe" pointers? Also can I return a reference via a function? how? (gah, I really dislike the C#'s referencing model ><) Btw, here's my code: *as a sidenote, I get an exception when I try to modify that bitmap after reference - I thoguht C# did auto-garbage collection and would not delete the bitmap as long as I had some reference pointing to it...
/// <summary>
/// This class takes care of rendering out TMEngine components to the screen
/// </summary>
class Renderer
{
//=========================================================================
// Public
//=========================================================================
public Renderer(ref Bitmap DrawArea, ref cEntityManager EntityManager, ref cTimer Timer)
{
_DrawArea = DrawArea; //this doesn't do what I want
_rEntityManager = EntityManager;
_rTimer = Timer;
}
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();
}
//=========================================================================
// Private
//=========================================================================
private Bitmap _DrawArea;
private cEntityManager _rEntityManager;
private cTimer _rTimer;
}