generic functions
-
I would like to know how to implement generic functions. I have a few classes: CdRecord, BookRecord, Record, and RecordManager. CdRecord inherits from Record. BookRecord inherits from Record. In my RecordManager class, I have a bunch of functions. Two of the functions in RecordManager read info from a table in an SQL DB. rough code: using... namespace MyProject { class RecordManager { List CDrecords = new List(); List BOOKrecords = new List(); public void readBooks() { // connect to DB // fill BOOKrecords with entries from table } public void readCds() { // connect to DB // fill CDrecords with entries from table } } } Now, once I've filled CDrecords and BOOKrecords, I need a function that will split them, and re-arrange the order of the records. I realise I can make one function that takes a List parameter and one function that takes a List parameter, but I'd prefer to have one function be able to handle them both. I also know that I can just send an extra parameter (int i for example) that indicates what type i am working with (if i == 1, BookRecord list, etc). However, I don't like this method either. In the future, I may end up with many more classes that inherit from Record, that I'll have lists of that need to be split, and I don't want to have a function for each type because I may end up with 20 redundant functions. Similarly, I don't want to send an extra parameter because of readability (if i == 1, if i == 2, .... if i == 20). How do I make one function that is able to take a paramater of type List where T can be CdRecord or BookRecord? I have tried the following, and it does not work: public void SplitRecords(List records, int start, int end) { //some code for(int i = start; i <= end; i++) { //some code records[i].memberFunction(); //some code } } The reason it doesn't work is because "memberFunction()" is a function of Record (which is inherited by BookRecord and CdRecord) but "T" does not have a definition for "memberFunction()" How do I correct this? Could I be experiencing problems because I am trying to use classes I wrote with generics, rather than int/short/etc.? Thanks in advance for any assistance!
-
I would like to know how to implement generic functions. I have a few classes: CdRecord, BookRecord, Record, and RecordManager. CdRecord inherits from Record. BookRecord inherits from Record. In my RecordManager class, I have a bunch of functions. Two of the functions in RecordManager read info from a table in an SQL DB. rough code: using... namespace MyProject { class RecordManager { List CDrecords = new List(); List BOOKrecords = new List(); public void readBooks() { // connect to DB // fill BOOKrecords with entries from table } public void readCds() { // connect to DB // fill CDrecords with entries from table } } } Now, once I've filled CDrecords and BOOKrecords, I need a function that will split them, and re-arrange the order of the records. I realise I can make one function that takes a List parameter and one function that takes a List parameter, but I'd prefer to have one function be able to handle them both. I also know that I can just send an extra parameter (int i for example) that indicates what type i am working with (if i == 1, BookRecord list, etc). However, I don't like this method either. In the future, I may end up with many more classes that inherit from Record, that I'll have lists of that need to be split, and I don't want to have a function for each type because I may end up with 20 redundant functions. Similarly, I don't want to send an extra parameter because of readability (if i == 1, if i == 2, .... if i == 20). How do I make one function that is able to take a paramater of type List where T can be CdRecord or BookRecord? I have tried the following, and it does not work: public void SplitRecords(List records, int start, int end) { //some code for(int i = start; i <= end; i++) { //some code records[i].memberFunction(); //some code } } The reason it doesn't work is because "memberFunction()" is a function of Record (which is inherited by BookRecord and CdRecord) but "T" does not have a definition for "memberFunction()" How do I correct this? Could I be experiencing problems because I am trying to use classes I wrote with generics, rather than int/short/etc.? Thanks in advance for any assistance!
You can limit what T can be, and so you'll have access to the methods of the base types from the T instance. where T : Record is what you need on the end of your generic class definition.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )