Link to home
Start Free TrialLog in
Avatar of dotnetpro
dotnetpro

asked on

A field initializer cannot reference the nonstatic field, method, or property

Experts,

I am creating a bitmap image
Bitmap bitmap = new Bitmap(BMP_HEIGHT, BMP_WIDTH);     then i a have following line of code
System.Drawing.Graphics chart = Graphics.FromImage(bitmap);

When i compile i get compiler error A field initializer cannot reference the nonstatic field, method, or property.

I do nt want to keep the second line into Page_load event. Any idea how can we fix it ?
   
Avatar of p_davis
p_davis

Avatar of dotnetpro

ASKER

Well you stated the problem in a different way but no solution. This is what i did

Bitmap bitmap = new Bitmap(BMP_HEIGHT, BMP_WIDTH);
System.Drawing.Graphics chart;

.........Page_load(.............)
{
chart = Graphics.FromImage(bitmap);
}

Seems to be working for now. Do you think this could be a problem later.
Haven't tried it, but maybe this:?

System.Drawing.Graphics chart = Graphics.FromImage(new Bitmap(BMP_HEIGHT, BMP_WIDTH));
try initializing the statement

System.Drawing.Graphics chart = null;
ASKER CERTIFIED SOLUTION
Avatar of p_davis
p_davis

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial