Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. generic functions

generic functions

Scheduled Pinned Locked Moved C#
databasetutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dfn
    wrote on last edited by
    #1

    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!

    C 1 Reply Last reply
    0
    • D dfn

      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!

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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 )

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups