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