Results 1 to 8 of 8

Thread: [RESOLVED] [2.0] Coding Concept

  1. #1

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Resolved [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:
    1. public enum GetDataType
    2. {
    3.     _DataReader,
    4.     _DataSet,
    5.     _DataTable,
    6.     _DataCommand,
    7.     _DataAdapter
    8. }
    9.  
    10. public object ExecuteDb(string QueryString, GetDataType ss)
    11. {
    12.     try {
    13.         DbDataAdapter dap;
    14.         dap = new OleDb.OleDbDataAdapter(QueryString, myConnection);
    15.         tcomm.CommandText = QueryString;
    16.         //*************** Returning Adapter
    17.         if (ss == GetDataType._DataAdapter) {
    18.             return dap;
    19.         }
    20.         //**************** Returning Data Reader
    21.         if (ss == GetDataType._DataReader) {
    22.             OleDb.OleDbDataReader tread;
    23.             tread = tcomm.ExecuteReader();
    24.             return tread;
    25.         }
    26.         //*****************Returning DataSet
    27.         if (ss == GetDataType._DataSet) {
    28.             DataSet ds = new DataSet();
    29.             dap.Fill(ds);
    30.             return ds;
    31.         }
    32.         //*****************Executing Command
    33.         tcomm.ExecuteNonQuery();
    34.         //*****************Returning DataTable
    35.         if (ss == GetDataType._DataTable) {
    36.             DataTable datatab = new DataTable();
    37.             dap.Fill(datatab);
    38.             return datatab;
    39.         }
    40.         //*****************Executing Command
    41.         if (ss == GetDataType._DataCommand) {
    42.             tcomm.ExecuteNonQuery();
    43.         }
    44.     }
    45.    
    46.     catch (Exception ex) {
    47.         throw ex;
    48.     }

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2.0] Coding Concept

    so you want to say that my code is fine?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Coding Concept

    I didn't say that. What advantage does that code structure offer over five different methods? None.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    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

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Talking 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,

  8. #8
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: [2.0] Coding Concept

    Quote 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
  •  



Click Here to Expand Forum to Full Width