Hi all :wave:
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?
Printable View
Hi all :wave:
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?
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.
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.
can we use one function and pass only blank for using it as a 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.That is all you can do. You simply cannot have multiple optional parameters and omit some and not others.C# Code:
public void SomeMethod(string arg1) { this.SomeMethod(arg1, true); // true is the default for arg2. } public void SomeMethod(string arg1, bool arg2) { // ... }
VB.NET Code:
Public Function ReturnDataTable(Optional ByVal Fields As String = "", Optional ByVal Condition As String = "") As DataTable Try Dim DataTable As New DataTable Dim QueryString As String 'When Field AND Condition Both Blank If Fields.Equals(String.Empty) And Condition.Equals(String.Empty) Then QueryString = "SELECT * FROM PartyOrder " DataTable = DataBaseHelper.ExecuteDataTable(QueryString) 'will execute a datatable Return DataTable End If 'When Condition Is Blank If Not Fields.Equals(String.Empty) And Condition.Equals(String.Empty) Then QueryString = "SELECT " & Fields & " FROM PartyOrder " DataTable = DataBaseHelper.ExecuteDataTable(QueryString) ''will execute a datatable Return DataTable End If 'When Field Is Blank If Fields.Equals(String.Empty) And Not Condition.Equals(String.Empty) Then QueryString = "SELECT * FROM PartyOrder " & Condition & "" DataTable = DataBaseHelper.ExecuteDataTable(QueryString) 'will execute a datatable Return DataTable End If 'When Field And Condition Both Given If Not Fields.Equals(String.Empty) And Not Condition.Equals(String.Empty) Then QueryString = "SELECT " & Fields & " FROM PartyOrder " & Condition & "" DataTable = DataBaseHelper.ExecuteDataTable(QueryString) 'will execute a datatable Return DataTable End If Catch ex As DbException Throw New Exception("DB Exception ", ex) Catch exx As Exception Throw New Exception("ExecuteTable Exception ", exx) End Try End Function
Now look above function thsi is in vb.net
so you want to say make four function in c# instead of one
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.
Thanks
What do you gain by making one function that is the size of four functions, anyway?
May be reducing code:)Quote:
Originally Posted by penagate
Well, here's an idea:
That's less code, isn't it?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); }
Thanks
but if I want to return a datareader too then use 8 function ?
Read This AlsoCode://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); }
can i use 8 function?
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);
}
Thanks Super Moderator after checking I will tell you that what is going on there