Click here to Skip to main content
15,917,611 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Suppose I have a static method

C#
static void GetImage(ref Byte pbyBuffer) // can not modified the function,which is provided by third parties
{
   //do sth,and then get a bitmap 
   //bitmap Now want to assign to the current form'picturebox ,how to do?
}
Posted

No this is not the correct way.

You should return that Bitmap to the calling function.
Inside the Calling/Parent function, you should assign that to PictureBox.
 
Share this answer
 
You need to either:

1. as Tadit Dash suggests, modify the static method so it returns a bitmap/image to the caller ... or ...

2. pass a reference to the PictureBox to which the bitmap/image is to be assigned to the static method as a parameter.

Keep in mind that a static method "belongs" to the Class it is defined in: it is visible/usable from/in all instances of the Class.

Consider this Class:
C#
public class Class1
{
    public string ID { get; private set; }

    public Class1(string id)
    {
        ID = id;
    }

    public static void showID()
    {
        // error:
        // Keyword 'this' is not valid in a static property, 
        // static method, or static field initializer
        // MessageBox.Show(this.ID);

        // error:
        // An object reference is required for the non-static field, 
        // method, or property
        // MessageBox.Show(ID);
    }
}
What this demonstrates is that a static method has no "knowledge" of which instance of the Class it was called from: you must explicitly pass a reference to the current Class to the static method. Or, you need another variable/field defined as static that holds a reference to the current Class which the static method can access.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900