New Posts New Posts RSS Feed: String field MaxLength
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

String field MaxLength

 Post Reply Post Reply
Author
mikewishart View Drop Down
Groupie
Groupie
Avatar

Joined: 26-Feb-2010
Location: Reno, NV
Posts: 49
Post Options Post Options   Quote mikewishart Quote  Post ReplyReply Direct Link To This Post Topic: String field MaxLength
    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


Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post 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;
}
Back to Top
mikewishart View Drop Down
Groupie
Groupie
Avatar

Joined: 26-Feb-2010
Location: Reno, NV
Posts: 49
Post Options Post Options   Quote mikewishart Quote  Post ReplyReply Direct Link To This Post Posted: 14-Sep-2010 at 12:00pm
That will work.  Thank you!
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down