Question

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

If im doing in the loop for example : FrameNumber +=; it will count me the number of frames for example 6. But i want to count how many times the frames of X showed up and how many frames of Y.

I tried ot it like this:

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;

But thats not working not a good way.

Maybe i need to make the counting of X and Y in the end after the variables tt and yy ? I want to count how many times the loop did the variable tt and how many times it did the variable yy. Just puting int variable and make ++ will give me the overall frames i need to seperate them to X and Y. So if i have for example 6 frames so X will be 3 and Y will be 3.

Was it helpful?

Solution

EDIT

for this code to work

  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;

you need to extract number something like this

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

Prev Ans

Good way to do is

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

check that string is convertable or not

OTHER TIPS

Convert.Int32() isn't a Val() method, in other words, it won't take just the numbers from any string. When using Convert.Int32(), the entire string has to be numeric, for example:

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

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

If I understand correctly, you're trying to convert something to an int and count at the same time (here).

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; 

The above could better be written like:

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;

Or - Which would be even more clear; do your math operation first, then use the result in your string.Format() call;

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

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

However, framesNumberX and framesNumberY would be equal to eachother AND to the iterator i + 1. This is where I'm starting to lose the purpose of the variables. It would help if you could clarify in pseudo-code what exactly you're trying to accomplish:

public void MyMethod()
{
   ... code here

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


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

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top