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. Design Question (Class vs Structs)

Design Question (Class vs Structs)

Scheduled Pinned Locked Moved C#
questionvisual-studiodesignhelp
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.
  • S Offline
    S Offline
    student_rhr
    wrote on last edited by
    #1

    Hi Everyone, I have a "one to many" sort of situation. I am using a Key to get subset data. I started out using a class like this:

    public class Contract
    {
        public int ContractID {get;set;}
        public int CustomerID {get;set;}
        public string CustomerName {get;set;}
        public string Organization {get;set;}
        public int TermsID {get;}
    }
    //Then data mapper
    public class ContractDataMapper
    {
        public static List<Contract> GetContractData(int contractID)
        {
            List<Contract> returnValue = new List<Contract>();
            ... /*data retrieval logic*/
            while(reader.Read())
            {
                Contract contract = new Contract();
                contract.CustomerID = Convert.ToInt32(reader["CustomerID"]);
                contract.CustomerName = reader["CustomerName"].ToString();
                ...
                if(!returnValue.Contains(contract))
                    returnValue.Add(contract);
            }
            reader.Close();
            command.Dispose();
            return returnValue();
        }
    }
    

    But I soon realized that this is bad because I had to check to see if this ContractID has any TermsID associated with it and the way it currently is I would have to do something like:

    if(ContractDataMapper.GetContractData(1234)[0].TermsID > 0)
        // Do something
    

    Ideally I should have one class Contract and have other properties in a subclass. Now my question is ... do you think it would be better to have a struct or a subclass or something else?

    // Something like this
    Contract contract = new Contract(1234);
    List<SubsetData> subset = contract.SubsetData;
    

    I would appreciate your help.

    P 1 Reply Last reply
    0
    • S student_rhr

      Hi Everyone, I have a "one to many" sort of situation. I am using a Key to get subset data. I started out using a class like this:

      public class Contract
      {
          public int ContractID {get;set;}
          public int CustomerID {get;set;}
          public string CustomerName {get;set;}
          public string Organization {get;set;}
          public int TermsID {get;}
      }
      //Then data mapper
      public class ContractDataMapper
      {
          public static List<Contract> GetContractData(int contractID)
          {
              List<Contract> returnValue = new List<Contract>();
              ... /*data retrieval logic*/
              while(reader.Read())
              {
                  Contract contract = new Contract();
                  contract.CustomerID = Convert.ToInt32(reader["CustomerID"]);
                  contract.CustomerName = reader["CustomerName"].ToString();
                  ...
                  if(!returnValue.Contains(contract))
                      returnValue.Add(contract);
              }
              reader.Close();
              command.Dispose();
              return returnValue();
          }
      }
      

      But I soon realized that this is bad because I had to check to see if this ContractID has any TermsID associated with it and the way it currently is I would have to do something like:

      if(ContractDataMapper.GetContractData(1234)[0].TermsID > 0)
          // Do something
      

      Ideally I should have one class Contract and have other properties in a subclass. Now my question is ... do you think it would be better to have a struct or a subclass or something else?

      // Something like this
      Contract contract = new Contract(1234);
      List<SubsetData> subset = contract.SubsetData;
      

      I would appreciate your help.

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Probably a class.

      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