Reputation: 513
I have this code:
using System.Drawing;
int offset;
string longest = "";
Font F = new Font("Microsoft Sans Serif", 8, FontStyle.Regular);
list.Aggregate("", (max, cur) => max.Length > cur.Length ? longest = max : longest = cur);
offset = Graphics.MeasureString(longest, F).Width;
And I get an
"An object referece is required for the non-static [...]"
error on the Graphics.MeasureString
method, but using:
offset = new Graphics.MeasureString(longest, F).Width;
Raises a "The type name MeasureString
does not exist in the type Graphics
". The weird thing is, the compiler does find the MeasureString
method in the Graphics
class (or whatever it is) when the new
word is abscent.
So my problem is that when the compiler finds the method it is static and when initialising a new instance of it - it cannot be found.
Upvotes: 3
Views: 387
Reputation: 4879
You need to create an instance of the Graphics
object. In WinForms (this looks like WinForms code):
var graphics = this.CreateGraphics();
...
offset = graphics.MeasureString(longest, F).Width;
Upvotes: 5