|
-
Aug 25th, 2007, 07:28 PM
#1
Thread Starter
Just Married
[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?
-
Aug 25th, 2007, 07:50 PM
#2
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.
-
Aug 25th, 2007, 10:30 PM
#3
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.
-
Aug 27th, 2007, 12:10 AM
#4
Thread Starter
Just Married
Re: [2.0] Optional
can we use one function and pass only blank for using it as a optional?
-
Aug 27th, 2007, 12:57 AM
#5
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:
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.
-
Aug 27th, 2007, 01:58 AM
#6
Thread Starter
Just Married
Re: [2.0] Optional
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
-
Aug 27th, 2007, 02:06 AM
#7
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.
-
Aug 27th, 2007, 02:16 AM
#8
Thread Starter
Just Married
-
Aug 27th, 2007, 05:15 AM
#9
Re: [RESOLVED] [2.0] Optional
What do you gain by making one function that is the size of four functions, anyway?
-
Aug 27th, 2007, 05:54 AM
#10
Thread Starter
Just Married
Re: [RESOLVED] [2.0] Optional
 Originally Posted by penagate
What do you gain by making one function that is the size of four functions, anyway?
May be reducing code
-
Aug 27th, 2007, 06:11 AM
#11
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?
-
Aug 27th, 2007, 06:43 AM
#12
Thread Starter
Just Married
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.
-
Aug 27th, 2007, 07:32 AM
#13
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);
}
-
Aug 27th, 2007, 07:57 AM
#14
Thread Starter
Just Married
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|