title: Create a base class with an abstract field that is implemented by a child
body: <p>I am writing a wrapper around the driver of a rare database. Specifically, I am implementing raw query code, so I naturally need as much speed as possible. This is going to be implemented in a large eCommerce solution.</p>
  
  <p>This has put me off dynamic types and casting. I want direct access to the actual data type.</p>
  
  <p>The functionality I need is to have a base parameter class with general fields, and a series of child classes with one specifically typed field each (one for each database type, which are int, long, double, string, byte[], object, and list, plus list of each of these types). The C# type should match the database type.</p>
  
  <p>This is not straightforward because in my implementation, the calling code needs to see the base type, but base types can't see a child's fields.</p>
  
  <p>Here is the code I have so far:</p>
  
  <pre><code>private abstract class GlobalQueryParam
  {
      public readonly string Low;
      // Val must be correctly typed
      public abstract object Val; // requires casting; don't want, but works
      protected string dbVal = "";        
  
      public string GetDBVal()
      {
          return this.dbVal;
      }
  
      public abstract bool SetDBVal(string value);
  
      public GlobalQueryParam(string low)
      {
          this.Low = low;
      }
  }
  
  public class GlobalQueryParamInt : GlobalQueryParam
  {
      /// &lt;summary&gt;
      /// Represents the current literal value. Used in traversal actions.
      /// &lt;/summary&gt;
      public override int Val;
  
      public int Parse(string dbVal)
      {
          return (this.Val = int.Parse(dbVal));
      }
  
      public override bool SetDBVal(string value)
      {
          if (value != "")
          {
              this.dbVal = value;
              return int.TryParse(value, out this.Val);
          }
  
          return false;
      }
  
      public GlobalQueryParamInt(string low = "") : base(low) { }
  }
  </code></pre>
  
  <p>In my implementation, I have to declare an array of the base class, which is throwing a spanner in the works.</p>
  
  <p>I am sure there is a better way to do this. I looked at interfaces, but they too need the type, and I couldn't get generics to work because of the array of base types sharing a common type.</p>
  
  <p>If I change the aforementioned array, I introduce other complexities which are circular in nature.</p>
  
  <p>What is the most efficient way to implement what I am looking for, or as close to it as possible?</p>
  
  <p><strong>EDIT</strong>
  Implementation (doesn't work with generics):</p>
  
  <pre><code>    GlobalQueryParam[] trail = new GlobalQueryParam[dataPos+1]; // DOESN'T WORK
      trail[0] = new GlobalQueryParamInt((this.TestParams[0].QueryLow - 1).ToString());
      trail[1] = new GlobalQueryParamInt((this.TestParams[1].QueryLow - 1).ToString());
      trail[2] = new GlobalQueryParamInt((this.TestParams[2].QueryLow - 1).ToString());
      trail[3] = new GlobalQueryParamInt((this.TestParams[3].QueryLow - 1).ToString());
      trail[4] = new GlobalQueryParamInt((this.TestParams[4].QueryLow - 1).ToString());
      trail[5] = new GlobalQueryParamInt((this.TestParams[5].QueryLow - 1).ToString());
      trail[6] = new GlobalQueryParamInt((this.TestParams[6].QueryLow - 1).ToString());
      trail[7] = new GlobalQueryParamInt((this.TestParams[7].QueryLow - 1).ToString());
  </code></pre>