Reputation: 212
I doing some kind logic but its getting error?
here my code
private static void DrawText(String text, Font font, Color textColor, Color backColor)
{
Image img = new Bitmap(640, 360);
Graphics drawing = Graphics.FromImage(img);
Color color = textColor;
if (text.Length <= 80) {
Rectangle displayRectangle =
new Rectangle(new Point(20, 100), new Size(img.Width - 1, img.Height - 1));
} else {
Rectangle displayRectangle =
new Rectangle(new Point(20, 80), new Size(img.Width - 1, img.Height - 1));
}
StringFormat format1 = new StringFormat(StringFormatFlags.NoClip);
StringFormat format2 = new StringFormat(format1);
// ERROR ON NEXT LINE
drawing.DrawString(text, font, Brushes.Red, (RectangleF)displayRectangle, format2);
drawing.Dispose();
string fileName = "f.png";
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
img.Save(path, System.Drawing.Imaging.ImageFormat.Png);
img.Dispose();
}
the error is
Error 1 The name 'displayRectangle' does not exist in the current context C:\Documents and Settings\admin\My Documents\Visual Studio 2008>\Projects\template\template\Form1.cs 119 69 template
At this line
drawing.DrawString(text, font, Brushes.Red, (RectangleF)displayRectangle, format2);
In php the logic was right, but when in C# its getting error?, How doing this logic in C#?
Any help? ( Still learning C# by doing :D )
Upvotes: 1
Views: 693
Reputation: 1333
I think it is because of your initialization.
The way it should be:
private static void DrawText(String text, Font font, Color textColor, Color backColor)
{
Rectangle displayRectangle; // YOU MUST DECLARE IT HERE OR OUTSIDE THE FUNCTION,
// but never inside an "if" statement.
Image img = new Bitmap(640, 360);
Graphics drawing = Graphics.FromImage(img);
Color color = textColor;
if (text.Length <= 80) {
displayRectangle =
new Rectangle(new Point(20, 100), new Size(img.Width - 1, img.Height - 1));
} else {
displayRectangle =
new Rectangle(new Point(20, 80), new Size(img.Width - 1, img.Height - 1));
}
StringFormat format1 = new StringFormat(StringFormatFlags.NoClip);
StringFormat format2 = new StringFormat(format1);
// ERROR ON NEXT LINE
drawing.DrawString(text, font, Brushes.Red, (RectangleF)displayRectangle, format2);
drawing.Dispose();
string fileName = "f.png";
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
img.Save(path, System.Drawing.Imaging.ImageFormat.Png);
img.Dispose();
}
Upvotes: 1
Reputation: 822
If you really want to be concise, you can replace the entire IF/ELSE block with the following line of code:
var displayRectangle = new Rectangle(20, (text.Length > 80 ? 80 : 100), img.Width - 1, img.Height - 1);
Upvotes: 1
Reputation: 13579
Move the Rectangle
definition above the if
else
block, so that it doesn't go out of scope.
Rectangle displayRectangle;
if (text.Length <= 80)
{
displayRectangle = new Rectangle(new Point(20, 100), new Size(img.Width img.Height - 1));
}
else
{
displayRectangle = new Rectangle(new Point(20, 80), new Size(img.Width - img.Height - 1));
}
Upvotes: 12
Reputation: 460048
You cannot use a variable which is declared outside of the current scope. You have declared displayRectangle
inside of the if-clause
, hence it is "invisible" outside of it.
Instead declare it outside and initialize it inside:
Rectangle displayRectangle = Rectangle.Empty;
if (text.Length <= 80)
{
displayRectangle = new Rectangle(new Point(20, 100), new Size(img.Width - 1, img.Height - 1));
}
else
{
displayRectangle = new Rectangle(new Point(20, 80), new Size(img.Width - 1, img.Height - 1));
}
8.5.1 Local variable declarations
The scope of a local variable declared in a local-variable-declaration is the block in which the declaration occurs. It is an error to refer to a local variable in a textual position that precedes the local-variable-declarator of the local variable. Within the scope of a local variable, it is a compile-time error to declare another local variable or constant with the same name.
Upvotes: 3
Reputation: 30882
Basically, it doesn't work that way :)
Variables defined within an if
or an else
block, between the {
and the }
are only visible in that block. That is why you were able to define it twice, once in the if
branch and once in the else
branch.
So the solution is to declare the variable before the branching, and just set it in the if/else branches.
Rectangle displayRectangle; //declaration
if (text.Length <= 80)
{
//setting
displayRectangle = new Rectangle(new Point(20, 100), new Size(img.Width - 1, img.Height - 1));
}
else
{
//setting
displayRectangle = new Rectangle(new Point(20, 80), new Size(img.Width - 1, img.Height - 1));
}
....
//usage
drawing.DrawString(text, font, Brushes.Red, (RectangleF)displayRectangle, format2);
In addition, the C# compiler is smart enough to detect that the variable is set in each branch, so that it will definitely be set when it reaches it's usage code. However, if it's not set in every branch, that would be a compile error, e.g.
if (text.Length <= 80)
{
displayRectangle = ...;
}
else if (text.Length > 40)
{
displayRectangle = ...;
}
Upvotes: 5
Reputation: 1038710
You could define the displayRectangle
variable outside of your if/else logic:
Rectangle displayRectangle;
if (text.Length <= 80)
{
displayRectangle = new Rectangle(new Point(20, 100), new Size(img.Width - 1, img.Height - 1));
}
else
{
displayRectangle = new Rectangle(new Point(20, 80), new Size(img.Width - 1, img.Height - 1));
}
Now this variable will be known to the outer scope.
Upvotes: 4
Reputation: 6424
Move your variable declaration outside the block:
Rectangle displayRectangle;
if (text.Length <= 80)
{
displayRectangle = new Rectangle(new Point(20, 100), new Size(img.Width - 1, img.Height - 1));
}
else
{
displayRectangle = new Rectangle(new Point(20, 80), new Size(img.Width - 1, img.Height - 1));
}
The problem is the scope of displayRectangle in your code. As soon as the block is finished executing, displayRectangle is completely forgotten about since it's declared inside the block.
Upvotes: 1