Studying C # and objects inside methods is confusing to me

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); //Do some graphics stuff.... } 

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.

+4
source share
6 answers

One option is to make this object a member variable, the other is to insert it into your method:

 public void InitializeBMPObjects () { Bitmap Bmp1 = new Bitmap(320, 226); pushPixels(Bmp1);//bmp1 accessible only to pushPixels in this case } public void pushPixels(Bitmap bmp1) { Graphics g = Graphics.FromImage(Bmp1); //Do some graphics stuff.... } 

Or..

 public class YourClass { private Bitmap bmp1 = new Bitmap(320,226) ; //this makes bmp1 accessible to all member methods. } 
+6
source

All answers about the scope are correct, and I don't want to confuse you anymore, but that would be an excellent use of the extension class. The extension allows you to call your method on any bitmap from anywhere in your solution (which has access).

To get started, add a public static class called what you want for your project. Then add a static method that returns System.Drawing.Bitmap and accepts the System.Drawing.Bitmap parameter. Put your code inside the method. Using the extension method is similar to using the ToString () method.

 public static class MyExtensions { public static System.Drawing.Bitmap PushPixels(this System.Drawing.Bitmap bitmap) { //do stuff here return bitmap; } } 

To use this in your code System.Drawing.Bitmap newBitmap = Bmp1.PushPixels();

+2
source

Create a variable as a / field property for the parent class, so you get something like.

 public class MyClass { private Bitmap _myImage; public void InitializeBMPObjects() { _myImage = new Bitmap(320, 226); } public void pushPixels() { Graphics g = Graphics.FromImage(_myImage); //Do some graphics stuff.... } } 
+1
source

You can use global variables as follows:

 public class Program... int a; void Method1() ... void Method2() ... 

All methods of this class now have access to the variable a.

0
source

Yes, you need to declare these objects outside the method; thus setting the scale as global.

Put this outside the method:

Bitmap Bmp1;

Then replace your code in the method as follows:

Bmp1 = new bitmap (320, 226);

Note that the same method that was declared globally is now updated in one way and is available to the rest.

0
source

You are right, the scope prevents access to this object outside the function. If you have several images, as you say, you can do the following:

 List<Bitmap> bitmapList = new List<Bitmap>(); private void Form1_Load(object sender, EventArgs e) { InitializeBMPObjects(); } public void InitializeBMPObjects () { Bitmap Bmp1 = new Bitmap(320, 226); bitmapList.Add(Bmp1); } public void pushPixels() { foreach(Bitmap bmp in bitmapList) { //do stuff here } } 
0
source

All Articles