none
what is this code name and application (Value of x: {0}", x)

    Question

  • Hi All

    I am very new to coding, I am wondering how use below code and what is this method name?

    for (int x = 10; x < 15; x++) { Console.WriteLine("Value of x: {0}", x); }

    RESULT /* Value of x: 10 Value of x: 11 Value of x: 12 Value of x: 13 Value of x: 14 */

    I tried this but didn't seems work

     for (int i = 1; i <= 10; i++)
                {
                    StreamWriter File = new StreamWriter(string.Format(@"C:\Users\Katrin\Desktop\New folder\"{0},i.html", i.ToString()));
                    File.Write();
                    File.Close();

    
    


    • Edited by lves Wednesday, April 5, 2017 2:43 PM
    Wednesday, April 5, 2017 2:42 PM

Answers

  • The first block of code is writing out the values 10 - 14 to the console window.

    The code you're trying to write is counting from 1 to 10. Each time through the loop you're creating a new file with the given index. You then call Write but pass nothing so the file would be empty. Also note that you're using File uppercase which, in .NET, is generally only done with types. There is already a File type so if you're using System.IO then the code is going to get confusing as you're basically overriding the type that is already available which may mean some functionality (eg. File.Exists) won't work because the compiler will prefer your local File variable over the system provided File type. You should use camel casing for local variables and reserve Pascal casing for types only.

    It's unclear what you're trying to accomplish.

    for (int i = 1; i <= 10; i++)
    {
       using (var file = new StreamWriter(String.Format(@"C:\Temp\{0}.txt", i)))
       {
          //Write something out
          file.Write("hello");
       };
    };

    You mentioned method name but this looks like it is in your Main method which is the code that runs when your program runs so there really isn't any other method you're creating at this point.

    Michael Taylor
    http://www.michaeltaylorp3.net

    • Marked as answer by lves Wednesday, April 5, 2017 3:00 PM
    Wednesday, April 5, 2017 2:57 PM
    Moderator
  • Hi lves,

    If you want to use the index then just  use the "i" directly. Hope this helps you.

    for (int i = 1; i <= 10; i++) { using (var file = new StreamWriter(String.Format(@"C:\Temp\{0}.txt", i))) { //Write something out file.Write("acb"+"abc "+i+" abc");

    //file.Write(string.Format("acb" + "abc" + "{0}" + "abc", i)); }; };



    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]



    Thursday, April 6, 2017 12:53 PM
    Moderator
  • This is simple string concatenation:

    String Concat

    How to: Concatenate Multiple Strings (C# Programming Guide)

    Also you could look into this about String.Format method:

    String Format

    String.Format Method



    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]



    Thursday, April 6, 2017 1:11 PM
    Moderator
  • As an aside, using String.Format is now considered to be deprecated in the latest language changes. Instead you should prefer to use string interpolation. It boils down to the same String.Format call but, unlike SF, it validates that you are referencing valid values at compile time. This helps catch issues with using SF with invalid arguments and whatnot. Here's how I'd write it starting in the latest language version.

    using (var file = new StreamWriter(@$"C:\Temp\{i}.txt"))
    {
       file.Write($"acbabc {i} abc");
    };
    Notice the $ on the front of the string to indicate string interpolation should occur. Inside the string ordinals are replaced with any expression you want. Intellisense works for this so you get editor support and the compiler will verify the expression you specify is valid. It also becomes a lot easier to read and less code to write. It is compiler sugar though so if you look at the generated code it will have converted it to the SF call you originally had.

    • Marked as answer by lves Thursday, April 6, 2017 2:45 PM
    Thursday, April 6, 2017 1:43 PM
    Moderator
  • That code shouldn't even compile since you have a method decl in the middle. You are also trying to define a local variable the same as the method name which will cause confusion (on your part).

    private void txtFloor_TextChanged ( object sender, EventArgs e )
    {
       var control = sender as TextBox;
    
       int floor;
       if (Int32.TryParse(control.Text, out floor))
       {
          foreach (var index = 0; index < floor; ++index)
          {
             ...
          };
       };
    }

    • Marked as answer by lves Friday, April 7, 2017 6:58 AM
    Thursday, April 6, 2017 3:33 PM
    Moderator

All replies

  • The first block of code is writing out the values 10 - 14 to the console window.

    The code you're trying to write is counting from 1 to 10. Each time through the loop you're creating a new file with the given index. You then call Write but pass nothing so the file would be empty. Also note that you're using File uppercase which, in .NET, is generally only done with types. There is already a File type so if you're using System.IO then the code is going to get confusing as you're basically overriding the type that is already available which may mean some functionality (eg. File.Exists) won't work because the compiler will prefer your local File variable over the system provided File type. You should use camel casing for local variables and reserve Pascal casing for types only.

    It's unclear what you're trying to accomplish.

    for (int i = 1; i <= 10; i++)
    {
       using (var file = new StreamWriter(String.Format(@"C:\Temp\{0}.txt", i)))
       {
          //Write something out
          file.Write("hello");
       };
    };

    You mentioned method name but this looks like it is in your Main method which is the code that runs when your program runs so there really isn't any other method you're creating at this point.

    Michael Taylor
    http://www.michaeltaylorp3.net

    • Marked as answer by lves Wednesday, April 5, 2017 3:00 PM
    Wednesday, April 5, 2017 2:57 PM
    Moderator
  • Thank you, I am still learning this code.

    Besides, I am wondering can I put {0} in the index

    for (int i = 1; i <= 10; i++) { using (var file = new StreamWriter(String.Format(@"C:\Temp\{0}.txt", i))) { //Write something out file.Write("acb"+"abc\"{0}\"abc); };


    Thursday, April 6, 2017 12:46 PM
  • Hi lves,

    If you want to use the index then just  use the "i" directly. Hope this helps you.

    for (int i = 1; i <= 10; i++) { using (var file = new StreamWriter(String.Format(@"C:\Temp\{0}.txt", i))) { //Write something out file.Write("acb"+"abc "+i+" abc");

    //file.Write(string.Format("acb" + "abc" + "{0}" + "abc", i)); }; };



    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]



    Thursday, April 6, 2017 12:53 PM
    Moderator
  • Hi lves,

    If you want to use the index then just  use the "i" directly. Hope this helps you.

    for (int i = 1; i <= 10; i++)
    {
       using (var file = new StreamWriter(String.Format(@"C:\Temp\{0}.txt", i)))
       {
          //Write something out
          file.Write("acb"+"abc "+i+" abc");
       };
    };


    Thanks,
    Sabah Shariq


    Hi Sabah Shariq
    Could you please give me some reference or explain more about this "+i+" format
    Thursday, April 6, 2017 1:03 PM
  • This is simple string concatenation:

    String Concat

    How to: Concatenate Multiple Strings (C# Programming Guide)

    Also you could look into this about String.Format method:

    String Format

    String.Format Method



    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]



    Thursday, April 6, 2017 1:11 PM
    Moderator
  • As an aside, using String.Format is now considered to be deprecated in the latest language changes. Instead you should prefer to use string interpolation. It boils down to the same String.Format call but, unlike SF, it validates that you are referencing valid values at compile time. This helps catch issues with using SF with invalid arguments and whatnot. Here's how I'd write it starting in the latest language version.

    using (var file = new StreamWriter(@$"C:\Temp\{i}.txt"))
    {
       file.Write($"acbabc {i} abc");
    };
    Notice the $ on the front of the string to indicate string interpolation should occur. Inside the string ordinals are replaced with any expression you want. Intellisense works for this so you get editor support and the compiler will verify the expression you specify is valid. It also becomes a lot easier to read and less code to write. It is compiler sugar though so if you look at the generated code it will have converted it to the SF call you originally had.

    • Marked as answer by lves Thursday, April 6, 2017 2:45 PM
    Thursday, April 6, 2017 1:43 PM
    Moderator
  • using (var file = new StreamWriter(@$"C:\Temp\{i}.txt"))
    {
       file.Write($"acbabc {i} abc");
    };

    Need time to understand but the way thanks for your reply:)

    Besides, I am trying convert textbox string to int which named x and than put it in the For Loop however it seems didn t work

    private void txtFloor_TextChanged(object sender, EventArgs e) { string txtFloor_TextChanged; int x; x = int.Parse(txtFloor_TextChanged); private void button1_Click(object sender, EventArgs e)

    for (int i = 1; i <={x}; i++) { using (var file = new StreamWriter(String.Format(@"C:\Temp\{0}.txt", i))) { //Write something out file.Write("acb"+"abc "+i+" abc");


    Thursday, April 6, 2017 3:10 PM
  • That code shouldn't even compile since you have a method decl in the middle. You are also trying to define a local variable the same as the method name which will cause confusion (on your part).

    private void txtFloor_TextChanged ( object sender, EventArgs e )
    {
       var control = sender as TextBox;
    
       int floor;
       if (Int32.TryParse(control.Text, out floor))
       {
          foreach (var index = 0; index < floor; ++index)
          {
             ...
          };
       };
    }

    • Marked as answer by lves Friday, April 7, 2017 6:58 AM
    Thursday, April 6, 2017 3:33 PM
    Moderator
  • That code shouldn't even compile since you have a method decl in the middle. You are also trying to define a local variable the same as the method name which will cause confusion (on your part).

    private void txtFloor_TextChanged ( object sender, EventArgs e )
    {
       var control = sender as TextBox;
    
       int floor;
       if (Int32.TryParse(control.Text, out floor))
       {
          foreach (var index = 0; index < floor; ++index)
          {
             ...
          };
       };
    }

    Do you meant the variable local and convert text to for loop is the same method cannot combine to use?

    Therefore the code should be like this?

    private void txtFloor_TextChanged ( object sender, EventArgs e ) { var control = sender as TextBox; int floor;

     private void button1_Click(object sender, EventArgs e)

    if (Int32.TryParse(control.Text, out floor)) { foreach (var index = 0; index < floor; ++index)

    {  using (var file = new StreamWriter(String.Format(@"C:\Temp\{0}.txt", index)))


    Thursday, April 6, 2017 4:10 PM
  • No. You have private void button1_Click decl in the middle of private void txtFloor_TextChanged method body. That is an invalid syntax (you can declare nested functions but this isn't the syntax for it). The code I posted is the correct code for what you are trying to do.
    Thursday, April 6, 2017 4:14 PM
    Moderator
  • No. You have private void button1_Click decl in the middle of private void txtFloor_TextChanged method body. That is an invalid syntax (you can declare nested functions but this isn't the syntax for it). The code I posted is the correct code for what you are trying to do.

    Should the index of button1_Click place in ... like below code?

    If not how should the code like

    private void txtFloor_TextChanged ( object sender, EventArgs e )
    {
       var control = sender as TextBox;
    
       int floor;
       if (Int32.TryParse(control.Text, out floor))
       {
          foreach (var index = 0; index < floor; ++index)
          {
       using (var file = new StreamWriter(String.Format(@"C:\Temp\{0}.txt", index)))
       {
          //Write something out
          file.Write("acb"+"abc "+index+" abc");
          };
       };
    }

    Put index in txtFloor_TextChanged will be run directly which without checked button than run

    I want to run the process after checked button.

    Friday, April 7, 2017 7:20 AM
  • I don't follow, changing the text in a textbox has nothing to do with a button. Should all this code be executed when a button is clicked? If so then move it to the button click handler instead. You then have access to the button that was clicked via the sender parameter.

    But at this point I think you are asking a different question than what you originally posted and it is more related to Winforms and how to handle control events. I believe you should post a new question in the Windows Forms forums to ask about which event(s) to handle and how you might combine them.

    Friday, April 7, 2017 1:35 PM
    Moderator