Thread: sprintf in C#?

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    173

    sprintf in C#?

    hi,
    Actually in VC++, for example,when I was loading mutiple bitmaps whose filenames are bitmap0,bitamp1;bitamp2,respectively:

    char filename[20];
    FILE * file;

    for(int i = 0; i< 3;i++)
    sprintf(filename, "bitmap%d",i);

    //////load bitmap function
    Load_Bitamp(*file,filename);

    so my question is that, is there any function like sprintf()
    like my previous question, if I want to create some number of labels, so I use such way :

    for(int i = 0; i< number;i++)
    {
    sprintf(filename, "button%d",i);
    ///load and create buttons using filename
    }

    thanx!
    Don't laugh at me,I am just a SuperNewbie.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    You can use the static Format method of class String:

    string s = String.Format( "Bitmap{0}.bmp", i );

    {0} is the placeholder for the first variable. No type formatting is required for easy output, as each object features a ToString method. Next placeholder would be {1} and so on.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    173
    thank you so much,

    if I want to create 10 labels,

    string label_str;
    for(int i = 1; i<11;i++)
    {
    label_str = String.Format( "label{0}", i );
    System.Windows.Forms.Label label_str;
    label_str = new System.Windows.Forms.Label();
    ...................
    }

    but label_str is a string type ,can't be convert to System.Windows.Forms.Label.

    so is that possible to change mylabel name ?
    System.Windows.Forms.Label mylabel;
    mylabel =????("label{0}",i);
    Don't laugh at me,I am just a SuperNewbie.

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    This is the wrong way to do it. You need an array of labels. You can dynamically create that array and then dynamically create each label in that array like you did. You cannot create variable names this way, variable names have to be fix when you compile the program.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sprintf overflows my buffer -- why?
    By Lasston in forum C Programming
    Replies: 26
    Last Post: 06-20-2008, 04:33 PM
  2. sprintf : garbage appended
    By yeller in forum C Programming
    Replies: 9
    Last Post: 12-17-2007, 10:21 AM
  3. sprintf in C and C++
    By usu_vlsi in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2005, 04:14 AM
  4. sprintf and sscanf
    By tommy69 in forum C Programming
    Replies: 10
    Last Post: 04-22-2004, 08:00 PM
  5. Sprintf
    By Trauts in forum C++ Programming
    Replies: 10
    Last Post: 01-15-2003, 01:35 PM