Print Page | Close Window

String field MaxLength

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2152
Printed Date: 13-Apr-2025 at 2:41am


Topic: String field MaxLength
Posted By: mikewishart
Subject: String field MaxLength
Date Posted: 13-Sep-2010 at 2:29pm
I'd like to make it a little easier to determine the maximum length of a string field rather than hardcoding it in a few places.  Is there any DevForce equivalent to the solution presented here?

http://stackoverflow.com/questions/748939/field-max-length-in-entity-framwork - http://stackoverflow.com/questions/748939/field-max-length-in-entity-framwork





Replies:
Posted By: DenisK
Date Posted: 14-Sep-2010 at 11:17am
Hi Mike;

I'm assuming that you want to get the MaxValue of the StringLengthVerifierAttribute. If so, we can create a function that can be passed any DevForce entity and its property, and using its type and property name, we can using Reflections to get its MaxValue of the StringLengthVerifierAttribute. Please see code samples below.

public void GetMaxStringLengthSample() {
      var maxStringLength = GetMaxStringLengthAttribute(typeof(Customer),                     
         Customer.PropertyMetadata.CompanyName.Name);
      
       Assert.IsTrue(maxStringLength == 40, "This should not fail");
}

public int GetMaxStringLengthAttribute(Type entityType, String propertyName) {
      var propertyInfo = entityType.GetProperty(propertyName);
      var attributes = (StringLengthVerifierAttribute)propertyInfo
        .GetCustomAttributes(typeof(StringLengthVerifierAttribute), false).First();

      return attributes.MaxValue;
}


Posted By: mikewishart
Date Posted: 14-Sep-2010 at 12:00pm
That will work.  Thank you!



Print Page | Close Window