Results 1 to 14 of 14

Thread: [RESOLVED] [2.0] Optional

  1. #1

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Resolved [RESOLVED] [2.0] Optional

    Hi all
    As we have benefit in the VB.NET that we can use the optional parameter too in the function.

    what for the C# what is the solution to do?

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2.0] Optional

    Optional doesnt exist in C#. The only thing I can think of doing instead of using the Optional keyword is to create two overloads of the function, one with the argument and one without it.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2.0] Optional

    That's what the VB compiler does behind the scenes too. In fact it's recommended that you use overloads instead of the Optional keyword in VB.NET.

  4. #4

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2.0] Optional

    can we use one function and pass only blank for using it as a optional?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Optional

    You've already been given the answer. You must overload the method because optional parameters don't exist in C#. If you want to use a default value for a parameter then simply have one method call the other and pass the default value to that argument, e.g.
    C# Code:
    1. public void SomeMethod(string arg1)
    2. {
    3.     this.SomeMethod(arg1, true); // true is the default for arg2.
    4. }
    5.  
    6. public void SomeMethod(string arg1, bool arg2)
    7. {
    8.     // ...
    9. }
    That is all you can do. You simply cannot have multiple optional parameters and omit some and not others.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2.0] Optional

    VB.NET Code:
    1. Public Function ReturnDataTable(Optional ByVal Fields As String = "", Optional ByVal Condition As String = "") As DataTable
    2.         Try
    3.             Dim DataTable As New DataTable
    4.             Dim QueryString As String
    5.             'When Field AND Condition Both Blank
    6.             If Fields.Equals(String.Empty) And Condition.Equals(String.Empty) Then
    7.                 QueryString = "SELECT * FROM PartyOrder "
    8.                 DataTable = DataBaseHelper.ExecuteDataTable(QueryString) 'will execute a datatable
    9.                 Return DataTable
    10.             End If
    11.             'When Condition Is Blank
    12.             If Not Fields.Equals(String.Empty) And Condition.Equals(String.Empty) Then
    13.                 QueryString = "SELECT " & Fields & " FROM PartyOrder "
    14.                 DataTable = DataBaseHelper.ExecuteDataTable(QueryString) ''will execute a datatable
    15.                 Return DataTable
    16.             End If
    17.             'When Field Is Blank
    18.             If Fields.Equals(String.Empty) And Not Condition.Equals(String.Empty) Then
    19.                 QueryString = "SELECT * FROM PartyOrder " & Condition & ""
    20.                 DataTable = DataBaseHelper.ExecuteDataTable(QueryString) 'will execute a datatable
    21.                 Return DataTable
    22.             End If
    23.             'When Field And Condition Both Given
    24.             If Not Fields.Equals(String.Empty) And Not Condition.Equals(String.Empty) Then
    25.                 QueryString = "SELECT " & Fields & " FROM PartyOrder " & Condition & ""
    26.                 DataTable = DataBaseHelper.ExecuteDataTable(QueryString) 'will execute a datatable
    27.                 Return DataTable
    28.             End If
    29.         Catch ex As DbException
    30.             Throw New Exception("DB Exception ", ex)
    31.         Catch exx As Exception
    32.             Throw New Exception("ExecuteTable Exception ", exx)
    33.         End Try
    34.     End Function

    Now look above function thsi is in vb.net
    so you want to say make four function in c# instead of one

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Optional

    Look, the simple fact is that optional parameters are not supported in C#. It doesn't mattter how much you want them or how many times you ask the same question, they are not supported. You couldn't write four overloads for that method anyway because both arguments are the same type. Life is tough.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2.0] Optional

    Thanks

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] [2.0] Optional

    What do you gain by making one function that is the size of four functions, anyway?

  10. #10

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Smile Re: [RESOLVED] [2.0] Optional

    Quote Originally Posted by penagate
    What do you gain by making one function that is the size of four functions, anyway?
    May be reducing code

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] [2.0] Optional

    Well, here's an idea:
    Code:
    public DataTable ReturnDataTable(string[] fields)
    {
      return _CreateDataTable("SELECT " + String.Join(", ", fields) + " FROM PartyOrder");
    }
    
    public DataTable ReturnDataTable(string condition)
    {
      return _CreateDataTable("SELECT * FROM PartyOrder WHERE " + condition);
    }
    
    public DataTable ReturnDataTable(string[] fields, string condition)
    {
      return _CreateDataTable("SELECT " + String.Join(", ", fields) + " FROM PartyOrder WHERE " + condition);
    }
    
    private DataTable _CreateDataTable(string sql) { return DataBase.Helper.ExecuteDataTable(sql); }
    That's less code, isn't it?

  12. #12

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Smile Re: [RESOLVED] [2.0] Optional

    Thanks
    but if I want to return a datareader too then use 8 function ?


    Code:
      //4 FUNCTION FOR DATA TABLE
            public DataTable ReturnDataTable(string[] fields)
            {
                return _CreateDataTable("SELECT " + String.Join(", ", fields) + " FROM PartyOrder");
            }
    
            public DataTable ReturnDataTable(string condition)
            {
                return _CreateDataTable("SELECT * FROM PartyOrder WHERE " + condition);
            }
    
            public DataTable ReturnDataTable(string[] fields, string condition)
            {
                return _CreateDataTable("SELECT " + String.Join(", ", fields) + " FROM PartyOrder WHERE " + condition);
            }
    
            private DataTable _CreateDataTable(string sql) { return DataBase.Helper.ExecuteDataTable(sql); }
    
            //4 FUNCTION FRO DATA READER
            public System.Data.SqlClient.SqlDataReader ReturnDataReader(string[] fields)
            {
                return _CreateDataReader("SELECT " + String.Join(", ", fields) + " FROM PartyOrder");
            }
    
            public System.Data.SqlClient.SqlDataReader ReturnDataReader(string condition)
            {
                return _CreateDataReader("SELECT * FROM PartyOrder WHERE " + condition);
            }
    
            public System.Data.SqlClient.SqlDataReader ReturnDataReader(string[] fields, string condition)
            {
                return _CreateDataReader("SELECT " + String.Join(", ", fields) + " FROM PartyOrder WHERE " + condition);
            }
    
            private System.Data.SqlClient.SqlDataReader _CreateDataReader(string sql) { return DataBase.Helper.ExecuteDataReader(sql); }
    Read This Also
    can i use 8 function?
    Last edited by shakti5385; Aug 27th, 2007 at 07:25 AM.

  13. #13
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] [2.0] Optional

    I think the method is flawed if you want to do that. You should separate the functions for generating SQL and the functions for creating the datareader/datatable.

    For example:
    Code:
    public string BuildSqlSelect(string table)
    {
      return BuildSqlSelect(table, new string[] { "*" });
    }
    
    public string BuildSqlSelect(string table, string condition)
    {
      return BuildSqlSelect(table, new string[] { "*" }, condition);
    }
    
    public string BuildSqlSelect(string table, string[] fields, string condition = null)
    {
      return
        "SELECT " + String.Join(", ", fields) +
        " FROM " + table +
        (condition != null ? " WHERE " + condition : String.Empty);
    }
    
    // ...
    
    public DataReader CreateDataReader(string sql)
    {
      return DataBase.Helper.ExecuteDataReader(sql);
    }
    
    public DataTable CreateDataTable(string sql)
    {
      return DataBase.Helper.ExecuteDataTable(sql);
    }

  14. #14

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [RESOLVED] [2.0] Optional

    Thanks Super Moderator after checking I will tell you that what is going on there

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width