EtanSivad
EtanSivad

Reputation: 511

Learning C# and objects inside of methods are confusing me

I'm sure this question has been answered already, but after an hour of constant searching I'm still very confused.
I'm learning C# and getting used to how things work, but one piece that baffles me is how do I make objects created in a method available to other methods.
I'm working on an app that does some image work. I want to create the objects used by the program when the form loads, then make changes to it with another method. Seems simple enough. Here's the bare bits of the 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 the bitmap object "Bmp1" then I want pushPixels() to make changes to that Object.
Trouble is, the method pushPixels complains because "The name 'Bmp1' does not exist in the current context"

I believe the issue is basically scope here. That the object Bmp1 only exists inside the scope of the method InitializeBMPObjects. But what if I want to create a bunch of objects on the form load; should I be creating the objects outside of a method? Or do I need to be flagging these as global objects somehow?

Thank you.

Upvotes: 4

Views: 1357

Answers (6)

jac
jac

Reputation: 9726

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

To begin add a public static class named what ever you want to your project. Then add a static method returning a System.Drawing.Bitmap and accepting a System.Drawing.Bitmap parameter. Put your code inside the method. Using your 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();

Upvotes: 2

Justin
Justin

Reputation: 6549

You are correct, scope is preventing you from accessing that object from outside the function. If you have multiple images like you say may occur, you could 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
    }
}

Upvotes: 0

Ulises
Ulises

Reputation: 13419

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

Put this outside the method:

Bitmap Bmp1;

Then replace your code in your method with this:

Bmp1 = new Bitmap(320, 226);

Notice that the same method that was declared globally is now instantianted in one method and available to the rest.

Upvotes: 0

Aleksandar Toplek
Aleksandar Toplek

Reputation: 2831

One would use global variables like so:

public class Program...

    int a;

    void Method1() ...
    void Method2() ...

All methods in this class now have access to variable a.

Upvotes: 0

Lews Therin
Lews Therin

Reputation: 10995

One option is to make that object a member variable, another is to inject 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.
}

Upvotes: 6

Dan Saltmer
Dan Saltmer

Reputation: 2165

Create the variable as a property/field against the parent class, so you end up with 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....

    }
}

Upvotes: 1

Related Questions