1

ある種のロジックを実行していますが、エラーが発生していますか?

ここに私のコード

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();
}

エラーは

エラー 1 名前 'displayRectangle' は現在のコンテキストに存在しません C:\Documents and Settings\admin\My Documents\Visual Studio 2008>\Projects\template\template\Form1.cs 119 69 テンプレート

この行で

drawing.DrawString(text, font, Brushes.Red, (RectangleF)displayRectangle, format2);

PHP ではロジックは正しかったのですが、C# ではエラーが発生しますか?, C# でこのロジックをどのように実行していますか?

何か助けはありますか?( :D を実行して、まだ C# を学習しています)

4

7 に答える 7

12

範囲外にならないように、Rectangle定義をブロックの上に移動します。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));
}
于 2012-12-28T21:43:01.893 に答える
5

基本的に、それはそのようには機能しません:)

ifまたはelseブロック内で{と の間で定義された変数は}、そのブロックでのみ表示されます。ifそのため、ブランチで 1 回、ブランチで1 回、2 回定義することができました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);

さらに、C# コンパイラは、各分岐で変数が設定されていることを検出するのに十分なほどスマートであるため、使用法コードに到達すると確実に設定されます。ただし、すべてのブランチで設定されていない場合は、コンパイル エラーになります。

if (text.Length <= 80)
{
  displayRectangle = ...;
}
else if (text.Length > 40)
{

  displayRectangle = ...;
}
于 2012-12-28T21:43:11.050 に答える
4

displayRectangleif/else ロジックの外側で変数を定義できます。

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));
}

これで、この変数は外側のスコープに認識されます。

于 2012-12-28T21:43:17.667 に答える
3

現在のスコープ外で宣言されている変数は使用できません。displayRectangleの内側で宣言したif-clauseので、外側では「見えません」。

代わりに、外部で宣言し、内部で初期化します。

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-declarationで宣言されたローカル変数のスコープは、宣言が発生するブロックです。ローカル変数のlocal-variable-declaratorの前にあるテキスト位置でローカル変数を参照するのはエラーです。ローカル変数のスコープ内で、同じ名前の別のローカル変数または定数を宣言することはコンパイル時エラーです。

于 2012-12-28T21:43:53.523 に答える
1

変数宣言をブロックの外に移動します。

    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));
    }

問題は、コード内のdisplayRectangleのスコープです。ブロックの実行が終了するとすぐに、displayRectangleはブロック内で宣言されているため、完全に忘れられます。

于 2012-12-28T21:45:15.760 に答える
1

本当に簡潔にしたい場合は、IF/ELSEブロック全体を次のコード行に置き換えることができます。

var displayRectangle = new Rectangle(20, (text.Length > 80 ? 80 : 100), img.Width - 1, img.Height - 1);

于 2012-12-28T22:28:11.070 に答える
1

初期化が原因だと思います。

それがあるべき方法:

 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();
}
于 2012-12-28T21:47:11.313 に答える