Click to See Complete Forum and Search --> : [RESOLVED] [2.0] Optional
shakti5385
Aug 25th, 2007, 07:28 PM
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?
Atheist
Aug 25th, 2007, 07:50 PM
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.
penagate
Aug 25th, 2007, 10:30 PM
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.
shakti5385
Aug 27th, 2007, 12:10 AM
can we use one function and pass only blank for using it as a optional?
jmcilhinney
Aug 27th, 2007, 12:57 AM
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.public void SomeMethod(string arg1)
{
this.SomeMethod(arg1, true); // true is the default for arg2.
}
public void SomeMethod(string arg1, bool arg2)
{
// ...
}That is all you can do. You simply cannot have multiple optional parameters and omit some and not others.
shakti5385
Aug 27th, 2007, 01:58 AM
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
jmcilhinney
Aug 27th, 2007, 02:06 AM
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.
shakti5385
Aug 27th, 2007, 02:16 AM
Thanks
penagate
Aug 27th, 2007, 05:15 AM
What do you gain by making one function that is the size of four functions, anyway?
shakti5385
Aug 27th, 2007, 05:54 AM
What do you gain by making one function that is the size of four functions, anyway?
May be reducing code:)
penagate
Aug 27th, 2007, 06:11 AM
Well, here's an idea:
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?
shakti5385
Aug 27th, 2007, 06:43 AM
Thanks
but if I want to return a datareader too then use 8 function ?
//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 (http://www.vbforums.com/showthread.php?t=484804)
can i use 8 function?
penagate
Aug 27th, 2007, 07:32 AM
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:
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);
}
shakti5385
Aug 27th, 2007, 07:57 AM
Thanks Super Moderator after checking I will tell you that what is going on there
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.