-6
public void Save(string path , string fileName , PictureBox pb)                
{
    int framesNumberX = 0;
    int framesNumberY = 0;
    string fn;
    string t = Path.GetFileNameWithoutExtension(this.name);
    if (File.Exists(path + "\\" + "DATABASE" + "\\" + fileName + "\\" + t + ".txt"))
    {
        try
        {
            string f = Path.Combine(path + "\\" + "DATABASE" + "\\" + t + "\\" + fileName);
            File.Delete(f);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: Could not delete file from disk. Original error: " + ex.Message);
        } 
        fn = path + "\\" + "DATABASE" + "\\" + t + "\\" + fileName;
    }
    else
    {
        fn = path + "\\" + "DATABASE" + "\\" + fileName + "\\" + this.name + ".txt";
    }
    OptionsFile setting_file = new OptionsFile(fn);
    setting_file.SetKey("File Name", fn);
    setting_file.SetKey("Object Name", fileName);
    setting_file.SetKey("Animation Name", this.name);
    setting_file.SetKey("picturebox.Width", pb.Width.ToString());
    setting_file.SetKey("picturebox.Height", pb.Height.ToString());
    string[] xFrames = new string[wocl.Count];
    string[] yFrames = new string[wocl.Count];
    string X="";
    string Y="";
    for (int i = 0; i < wocl.Count; i++)
    {
        X  = string.Format("Frame_X_{0} ", i + 1);
        Y  = string.Format("Frame_Y_{0} ", i + 1);
        int c = Convert.ToInt32(X);
        int d = Convert.ToInt32(Y);
        framesNumberX += c;
        framesNumberY += d;
        for (int j = 0; j < wocl[i].Point_X.Count; j++)
        {
            xFrames[i] += string.Format("{0},", wocl[i].Point_X[j]);
            yFrames[i] += string.Format("{0},", wocl[i].Point_Y[j]);   
        }    
        string tt = xFrames[i].Trim(",".ToCharArray());
        string yy =  yFrames[i].Trim(",".ToCharArray());
        setting_file.SetKey(X, tt);
        setting_file.SetKey(Y, yy);
    }    
    setting_file.SetKey("Number Of Frames X", framesNumberX.ToString());                
}

例如,如果我在循环中做: FrameNumber +=; 它会计算我的帧数,例如 6。但我想计算 X 的帧出现了多少次,以及 Y 的帧出现了多少次。

我试过这样:

X  = string.Format("Frame_X_{0} ", i + 1);
Y  = string.Format("Frame_Y_{0} ", i + 1);
int c = Convert.ToInt32(X);
int d = Convert.ToInt32(Y);
framesNumberX += c;
framesNumberY += d;

但这不是一个好方法。

也许我需要在变量 tt 和 yy 之后对 X 和 Y 进行计数?我想计算循环执行变量 tt 的次数以及执行变量 yy 的次数。只需输入 int 变量并 make ++ 即可为我提供将它们分隔为 X 和 Y 所需的整体帧。因此,如果我有例如 6 个帧,那么 X 将为 3,Y 将为 3。

4

2 回答 2

1

Convert.Int32()不是一种Val()方法,换句话说,它不会只取任何字符串中的数字。使用Convert.Int32()时,整个字符串必须是数字,例如:

// doesn't work
string x = "My string 123";
int y = Convert.Int32(x);

// does work
string x = "123";
int y = Convert.Int32(x);

如果我理解正确,您正在尝试将某些内容转换为 int 并同时计数(此处)。

X  = string.Format("Frame_X_{0} ", i + 1); 
Y  = string.Format("Frame_Y_{0} ", i + 1); 
// blows up with an exception
int c = Convert.ToInt32(X); 
int d = Convert.ToInt32(Y); 
framesNumberX += c; 
framesNumberY += d; 

以上最好写成:

X  = string.Format("Frame_X_{0} ", i + 1); 
Y  = string.Format("Frame_Y_{0} ", i + 1); 

framesNumberX++; 
framesNumberY++;

// or (depending on what you need)
framesNumberX = i + 1;
framesNumberY = i + 1;

或者——这会更清楚;先做数学运算,然后在string.Format()通话中使用结果;

framesNumberX = i + 1;
framesNumberY = i + 1;

X  = string.Format("Frame_X_{0} ", framesNumberX); 
Y  = string.Format("Frame_Y_{0} ", framesNumberY); 

但是, framesNumberX 和 framesNumberY 将彼此相等并且与迭代器相等i + 1。这是我开始失去变量目的的地方。如果您可以在伪代码中阐明您到底想要完成什么,这将有所帮助:

public void MyMethod()
{
   ... code here

   // trying to `your explanation`
   ... code here


    // and here, I'm trying to `your explanation`
    ... code here

}
于 2012-07-18T12:42:13.020 回答
0

编辑

让这段代码工作

  X  = string.Format("Frame_X_{0} ", i + 1);
  Y  = string.Format("Frame_Y_{0} ", i + 1);
  int c = Convert.ToInt32(ExtractNumbers(X));//modfied code line
  int d = Convert.ToInt32(ExtractNumbers(Y));//modfied code line
  framesNumberX += c;  framesNumberY += d;

你需要提取这样的数字

static string ExtractNumbers( string expr )
{
 return string.Join( null,System.Text.RegularExpressions.Regex.Split( expr, "[^\\d]" ) );
}

上一页

好办法是

static void Main()
    {
    // Convert string to number.
    string text = "123";
    int num = -1;
      if( int.TryParse (text,out num))
    Console.WriteLine(num);
    }

检查字符串是否可转换

于 2012-07-18T12:19:08.100 回答