|
-
Aug 20th, 2007, 08:00 AM
#1
Thread Starter
Just Married
[RESOLVED] [2.0] Coding Concept
Hi all
just look that code I am using enum for returning a object, In the function I make five type of object but my question is how to do this with a single object.
Means if the enum type is datareader then it will create the object of datareader, if the enum type is datatable then it create the object of datatable, same for the dataset and so on.
look the code
C# Code:
public enum GetDataType
{
_DataReader,
_DataSet,
_DataTable,
_DataCommand,
_DataAdapter
}
public object ExecuteDb(string QueryString, GetDataType ss)
{
try {
DbDataAdapter dap;
dap = new OleDb.OleDbDataAdapter(QueryString, myConnection);
tcomm.CommandText = QueryString;
//*************** Returning Adapter
if (ss == GetDataType._DataAdapter) {
return dap;
}
//**************** Returning Data Reader
if (ss == GetDataType._DataReader) {
OleDb.OleDbDataReader tread;
tread = tcomm.ExecuteReader();
return tread;
}
//*****************Returning DataSet
if (ss == GetDataType._DataSet) {
DataSet ds = new DataSet();
dap.Fill(ds);
return ds;
}
//*****************Executing Command
tcomm.ExecuteNonQuery();
//*****************Returning DataTable
if (ss == GetDataType._DataTable) {
DataTable datatab = new DataTable();
dap.Fill(datatab);
return datatab;
}
//*****************Executing Command
if (ss == GetDataType._DataCommand) {
tcomm.ExecuteNonQuery();
}
}
catch (Exception ex) {
throw ex;
}
-
Aug 20th, 2007, 08:38 AM
#2
Re: [2.0] Coding Concept
You are expecting magic. An enum is just a set of numbers represented by text identifiers. Your enumerated values aren't DataSets or DataReaders or anything else other than numbers. All you can do with an enumerated value is check whether it's equal to something and then do something if it is. That means either a 'switch' statement or 'if' statements. You can't magically turn an enumerated value into an object of some other type. It just doesn't work that way. Like I said, enumerations are just handy wrappers for integers that convey additional meaning. Could you turn an integer into a DataSet?
-
Aug 20th, 2007, 02:27 PM
#3
Thread Starter
Just Married
Re: [2.0] Coding Concept
so you want to say that my code is fine?
-
Aug 20th, 2007, 05:38 PM
#4
Re: [2.0] Coding Concept
I didn't say that. What advantage does that code structure offer over five different methods? None.
-
Aug 21st, 2007, 01:43 AM
#5
Thread Starter
Just Married
Re: [2.0] Coding Concept
Thanks sir
Why not?
It is giving me what i want
if i want datareader then it give me that, same for the datatable and the dataset,
Sit what improvement needed please guide me
-
Aug 21st, 2007, 01:59 AM
#6
Re: [2.0] Coding Concept
I'm not saying that it doesn't give you what you want. What I'm saying is that it doesn't do any better job of giving you what you want than if you had different methods for returning a DataSet, DataTable, etc. The code is no more streamlined because of all the If statements and it's no easier to call that one method and pass the extra parameter than it is to call different methods without it.
-
Aug 21st, 2007, 05:01 AM
#7
Thread Starter
Just Married
Re: [2.0] Coding Concept
Thanks sir
So what should I do , make different function for returning the dataset, datareader and the datatable.
I am sure that you have already make a simple function for all this,
-
Aug 21st, 2007, 08:30 PM
#8
Re: [2.0] Coding Concept
 Originally Posted by shakti
make different function for returning the dataset, datareader and the datatable.
Exactly.
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
|