C#if-else逻辑错误
我做了一些逻辑,但它得到了错误 这是我的密码C#if-else逻辑错误,c#,winforms,C#,Winforms,我做了一些逻辑,但它得到了错误 这是我的密码 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 &l
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();
}
在php中,逻辑是正确的,但在C中,它的错误是什么?如何在C中执行此逻辑
有什么帮助吗?(仍在学习C#通过执行:D)将
矩形
定义移动到if
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));
}
矩形显示矩形;
if(text.Length将矩形
定义移动到if
else
块上方,使其不超出范围
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));
}
矩形显示矩形;
如果(text.Length基本上不是这样的:)
在{
和}
之间的if
或else
块中定义的变量仅在该块中可见。这就是为什么可以定义它两次,一次在if
分支中,一次在else
分支中
因此,解决方案是在分支之前声明变量,然后在if/else分支中设置它
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);
Rectangle displayRectangle;//声明
如果(text.Length基本上不是这样的:)
在{
和}
之间的if
或else
块中定义的变量仅在该块中可见。这就是为什么可以定义它两次,一次在if
分支中,一次在else
分支中
因此,解决方案是在分支之前声明变量,然后在if/else分支中设置它
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);
Rectangle displayRectangle;//声明
if(text.Length您可以在if/else逻辑之外定义displayRectangle
变量:
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));
}
矩形显示矩形;
if(text.Length您可以在if/else逻辑之外定义displayRectangle
变量:
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));
}
矩形显示矩形;
if(text.Length您不能使用在当前范围外声明的变量。您已在if子句
内声明了displayRectangle
,因此它在当前范围外“不可见”
而是在外部声明它并在内部初始化它:
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));
}
Rectangle displayRectangle=Rectangle.Empty;
if(text.Length您不能使用在当前范围外声明的变量。您已在if子句
内声明了displayRectangle
,因此它在当前范围外“不可见”
而是在外部声明它并在内部初始化它:
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));
}
Rectangle displayRectangle=Rectangle.Empty;
如果(text.Length将变量声明移到块外:
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));
}
矩形显示矩形;
如果(text.Length将变量声明移到块外:
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));
}
矩形显示矩形;
如果(text.Length我认为这是因为您的初始化
应该是这样的:
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();
}
private static void DrawText(字符串文本、字体字体、彩色文本颜色、彩色背景色)
{
Rectangle displayRectangle;//必须在此处或函数外部声明它,
//但决不要在“如果”语句中。
图像img=新位图(640360);
图形绘制=Graphics.FromImage(img);
颜色=文本颜色;
如果(text.Length我认为这是因为您的初始化
应该是这样的:
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();
}
private static void DrawText(字符串文本、字体字体、彩色文本颜色、彩色背景色)
{
Rectangle displayRectangle;//必须在此处或函数外部声明它,
//但决不要在“如果”语句中。
图像img=新位图(640360);
图形绘制=Graphics.FromImage(img);
颜色=文本颜色;
if(text.Length如果您确实希望简洁,可以用以下代码行替换整个if/ELSE块:
var displayRectangle=new Rectangle(20,(text.Length>80?80:100),img.Width-1,img.Height-1);
如果您确实希望简洁,可以用以下代码行替换整个If/ELSE块:
var displayRectangle=新矩形(20,(text.Length>80?80:100),img.Width-1,img.Height-1);
displayRectangle
在右括号处超出范围。在if/else外部声明它,并将其分配给if/else内部的现有变量。3分钟6个答案,人们肯定会跳到这一步上!@all,谢谢你的帮助,现在我知道了“正确的方法”在C#displayRectangle
中创建逻辑超出了右大括号的范围。在if/else之外声明它,然后简单地将它分配给if/else内的现有变量。3分钟6个答案,人们肯定会跳到这一步上!@好的,谢谢你的帮助,现在我知道了“正确的方法”在C#中创建逻辑获取错误1一个新表达式需要(),[]或{}键入C:\Documents and Settings\admin\My Documents\Visual Studio 2008\Projects\template\template\Form1.cs 102 55 template
HisRectangle
是一个struct
,因此您不能在开始时将null
赋值给它。@TimSchmelter但您为默认(矩形)
找到了一个别致的名称就我个人而言,在这种情况下,我会在声明中保留未分配的变量。但是我不能使用var
关键字。要获得这个ErrorError 1新表达式需要()、[]或{}键入C:\Documents and Settings\admin\My Documents\Visual Studio 2008\Projects\template\template\Form1.cs 102 55 template
HisRectangle
是一个struct
,因此您不能在开始时将null
赋值给它。@TimSchmelter但您为默认(矩形)
找到了一个别致的名称就我个人而言,在这种情况下,我会在声明时不指定变量。但是我不能使用var
关键字。你似乎把“initialize”和“declare”混淆了。你需要“declare”I