locked
How to Break Long code lines in Visual Studio 2010 (C#) RRS feed

  • Question

  • User535084190 posted

    hi to all

    in vb this is easy just put  " _ &" but in C# I don't know how to do it

    thanks

    Tuesday, April 16, 2013 8:50 AM

Answers

  • User281315223 posted

    C# doesn't mind large gaps of white-space or new lines within it's code, unless to pertains to strings (otherwise you can just allow the code to wrap or press Enter to start a new line).

    In your current case, you are working with strings, so just make sure that you are wrapping each of the individual strings within the appropriate quotes and use the "+" character to append them as seen below : 

    SqlCommand sqlcmd = new SqlCommand("SELECT TopicsTitle,TopicContents,UserName,Avatar,NumberOfPost,uPoints,uType " +
                                         "FROM Topic, Registration " +
                                        "WHERE Topic.Category ='" + ilblTopic.Text + "'" +
                                          "AND LastPostDate ='" + DateTime.Parse("...") + "'");

    You may also want to consider using the String.Format() method to more easily pass in your arguments and avoid a ton of concatenated strings : 

    SqlCommand sqlcmd = new SqlCommand(String.Format("SELECT TopicsTitle,TopicContents,UserName,Avatar,NumberOfPost,uPoints,uType " +
                                                       "FROM Topic, Registration " +
                                                      "WHERE Topic.Category ='{0}' " + 
                                                        "AND LastPostDate ='{1}' ",ilblTopic.Text,DateTime.Parse("...")));

    When handling SQL Commands like this however, the safest bet is to use SqlCommand.Parameters to populate your parameters.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 16, 2013 8:53 AM
  • User-1360095595 posted

    Just continue your code on a new line. ore let the code simply wrap around at the end of the line. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 16, 2013 8:54 AM

All replies

  • User281315223 posted

    C# doesn't mind large gaps of white-space or new lines within it's code, unless to pertains to strings (otherwise you can just allow the code to wrap or press Enter to start a new line).

    In your current case, you are working with strings, so just make sure that you are wrapping each of the individual strings within the appropriate quotes and use the "+" character to append them as seen below : 

    SqlCommand sqlcmd = new SqlCommand("SELECT TopicsTitle,TopicContents,UserName,Avatar,NumberOfPost,uPoints,uType " +
                                         "FROM Topic, Registration " +
                                        "WHERE Topic.Category ='" + ilblTopic.Text + "'" +
                                          "AND LastPostDate ='" + DateTime.Parse("...") + "'");

    You may also want to consider using the String.Format() method to more easily pass in your arguments and avoid a ton of concatenated strings : 

    SqlCommand sqlcmd = new SqlCommand(String.Format("SELECT TopicsTitle,TopicContents,UserName,Avatar,NumberOfPost,uPoints,uType " +
                                                       "FROM Topic, Registration " +
                                                      "WHERE Topic.Category ='{0}' " + 
                                                        "AND LastPostDate ='{1}' ",ilblTopic.Text,DateTime.Parse("...")));

    When handling SQL Commands like this however, the safest bet is to use SqlCommand.Parameters to populate your parameters.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 16, 2013 8:53 AM
  • User-1360095595 posted

    Just continue your code on a new line. ore let the code simply wrap around at the end of the line. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 16, 2013 8:54 AM
  • User535084190 posted

    thanks very helpful

    this is much better now Cool

    string sqlcondition;
                sqlcondition = "Select TopicsTitle,TopicContents,UserName,Avatar, NumberofPost,uPoints," +
                "uType FROM Topic,Registration WHERE Topic.Category ='" + ilblTopic.Text +
                "'AND LastPostDate ='" + DateTime.Parse(ilblLastReplyTime.Text) + "'";
                
                sqlcmd1 = new SqlCommand(sqlcondition , con);
                adapter1 = new SqlDataAdapter(sqlcmd1 );


    Tuesday, April 16, 2013 9:05 AM
  • User306858990 posted

    C# is not line based, so you can split the statements anywhere but inside an identifier:

    public static void somemethod(

    int param1,

    int param2,

    int param3,

    more params etc...

    )


    Sunday, April 21, 2013 12:03 PM