I am sure that this question has already been answered, but after an hour of constant searching, I am still very confused. I am learning C # and getting used to how everything works, but one part that confuses me is how to create objects created in a method that is accessible to other methods.
I am working on an application that works with some images. I want to create the objects used by the program when loading the form, and then make changes to it in another way. Seems simple enough. Here are the bare bits of code:
private void Form1_Load(object sender, EventArgs e) { InitializeBMPObjects(); } public void InitializeBMPObjects () { Bitmap Bmp1 = new Bitmap(320, 226); } public void pushPixels() { Graphics g = Graphics.FromImage(Bmp1);
I want to create a raster object "Bmp1", then I want pushPixels () to make changes to this object.
The problem is that the pushPixels method complains because "Name" Bmp1 "does not exist in the current context"
I believe that the problem is mainly here. That the Bmp1 object exists only inside the scope of the InitializeBMPObjects method. But what if I want to create a bunch of objects on form loading? Should I create objects outside the method? Or do I need to somehow mark them as global objects?
Thanks.
source share