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. Attributes on fields in a DataTable/DataRow

Attributes on fields in a DataTable/DataRow

Scheduled Pinned Locked Moved C#
helpcomtutorialquestionannouncement
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.
  • H Offline
    H Offline
    hpjchobbes
    wrote on last edited by
    #1

    I am using a DataRow to get and set values (based on the manually typed data table from How to Manually Create a Typed DataTable[^]. The fields in the datarow can have different attributes that I would need to track, the main one being a maximum number of characters for some strings, but different versions have different lengths. I was trying to use Attributes to help with this, but I am stuck with how to be able to set/determine the 'version' when going through the attributes. I'd like to be able to set the version on the dataset as a whole and have the fields of the data row be able to use that to determine how to get values. Here's how I setup my attribute class:

    [AttributeUsage(AttributeTargets.Property, AllowMultiple =true)]
    public class MaxLengthAttribute : Attribute
    {

    public string Version;
    public int MaxLength;
    
    public MaxLengthAttribute(string version, int max)
    {
       Version= version;
       MaxLength = max;
    }
    

    }

    Here's what the data row looks like and how I am envisioning this would work.

    public CustomerRow : DataRow
    {
    [MaxLength("1.0", 40)]
    [MaxLength("2.0", 45)]
    public string Name
    {
    get
    {
    if(base["Name"] == DBNull.Value) throw new NullException("Name is null.");
    return ((TruncatedString)base["Name"]).Value;
    }
    }
    }

    I created a class called TruncatedString that has a string and bool, which i am using as the data type for the field in the data row. In the TruncatedString I am trying to get the version of the data set and the max length attribute to find out how long my field can be:

    public class TruncatedString
    {
    public bool AutoTruncate;
    private string _Value;
    public string Value
    {
    get
    {
    if(AutoTruncate)
    {
    int maxLength = ???
    // How to get the version from the DataSet
    // And the MaxLength attribute for the field in the data row
    if(maxLength >= 0 && Value.Length > maxLength) return _Value.Substring(0, maxLength);
    }
    return _Value;
    }
    set { _Value = value; }
    }
    }

    I am not sure if Attributes are the best way to go with this set

    L 1 Reply Last reply
    0
    • H hpjchobbes

      I am using a DataRow to get and set values (based on the manually typed data table from How to Manually Create a Typed DataTable[^]. The fields in the datarow can have different attributes that I would need to track, the main one being a maximum number of characters for some strings, but different versions have different lengths. I was trying to use Attributes to help with this, but I am stuck with how to be able to set/determine the 'version' when going through the attributes. I'd like to be able to set the version on the dataset as a whole and have the fields of the data row be able to use that to determine how to get values. Here's how I setup my attribute class:

      [AttributeUsage(AttributeTargets.Property, AllowMultiple =true)]
      public class MaxLengthAttribute : Attribute
      {

      public string Version;
      public int MaxLength;
      
      public MaxLengthAttribute(string version, int max)
      {
         Version= version;
         MaxLength = max;
      }
      

      }

      Here's what the data row looks like and how I am envisioning this would work.

      public CustomerRow : DataRow
      {
      [MaxLength("1.0", 40)]
      [MaxLength("2.0", 45)]
      public string Name
      {
      get
      {
      if(base["Name"] == DBNull.Value) throw new NullException("Name is null.");
      return ((TruncatedString)base["Name"]).Value;
      }
      }
      }

      I created a class called TruncatedString that has a string and bool, which i am using as the data type for the field in the data row. In the TruncatedString I am trying to get the version of the data set and the max length attribute to find out how long my field can be:

      public class TruncatedString
      {
      public bool AutoTruncate;
      private string _Value;
      public string Value
      {
      get
      {
      if(AutoTruncate)
      {
      int maxLength = ???
      // How to get the version from the DataSet
      // And the MaxLength attribute for the field in the data row
      if(maxLength >= 0 && Value.Length > maxLength) return _Value.Substring(0, maxLength);
      }
      return _Value;
      }
      set { _Value = value; }
      }
      }

      I am not sure if Attributes are the best way to go with this set

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Preprocessor and compiler directives are one way of accomplishing what you want: [#if (C# Reference) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-if)

      "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

      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