PDA

Click to See Complete Forum and Search --> : [RESOLVED] [2.0] Coding Concept


shakti5385
Aug 20th, 2007, 08:00 AM
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

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;
}

jmcilhinney
Aug 20th, 2007, 08:38 AM
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?

shakti5385
Aug 20th, 2007, 02:27 PM
so you want to say that my code is fine?

jmcilhinney
Aug 20th, 2007, 05:38 PM
I didn't say that. What advantage does that code structure offer over five different methods? None.

shakti5385
Aug 21st, 2007, 01:43 AM
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

jmcilhinney
Aug 21st, 2007, 01:59 AM
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.

shakti5385
Aug 21st, 2007, 05:01 AM
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, :)

mar_zim
Aug 21st, 2007, 08:30 PM
make different function for returning the dataset, datareader and the datatable.


Exactly.