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 ?
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 ?
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.
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));
System.Drawing.Graphics chart = Graphics.FromImage(new Bitmap(BMP_HEIGHT, BMP_WIDTH));
try initializing the statement
System.Drawing.Graphics chart = null;
System.Drawing.Graphics chart = null;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://msdn.microsoft.com/en-us/library/5724t6za(vs.71).aspx