Click here to Skip to main content
16,010,392 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to define a class (example below), where each variable defined must have a pre defined size so that when I use that variable to feed it a value; I want to make sure I do not assign a value larger than its pre defined size, and if the value assigned is smaller than the max length/size then I want to pad the variable with spaces, how do I do that? Thanks in advance.

// Using StringLength attribute did not do the trick (I think I am using it wrongly here).

C#
public class FormActivityDetails
{
    [Required]
    [StringLength(10)]
    public string CustomerID { get; set; }
    [Required]
    [StringLength(10)]
    public string DcID { get; set; }
    public string DcIDQual { get; set; }

}
Posted
Comments
Sergey Alexandrovich Kryukov 18-Jul-12 23:10pm    
Why? Why?!
And why this can be a problem? Well, truncate it, pad it, if you have nothing else do to...

So, is there something you don't know? How to write property setters or getters? How to read the attribute?
I don't say it's very easy; the code would be pretty long (a bit too long to put in the answer, especially if you want to use System.Reflection.Emit, and just Reflection without emitting can be much slower).
--SA
Member 8714829 19-Jul-12 12:18pm    
My question is how to set a max size for the strings defined in my class (FormActivityDetails). What I would do later on is pad or truncate the value if found to be different in size. Is there a link I can go to in order to look up your suggestion(s)?
[no name] 19-Jul-12 12:27pm    
This is the link to the string length attribute class, http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.stringlengthattribute.aspx#Y2230
Sergey Alexandrovich Kryukov 19-Jul-12 12:47pm    
I think it will work only if used in ProperyGrid or something like that. If you simply try to assign a value of greater length, it will be assigned. And it won't do this padding. The problem is different: I doubt that this requirement makes sense or worth the effort. Of course this is possible to achieve (through property setter and Reflection), but why? Most likely it does not based on anything but the bad habit of OP to work with some fixed data structures or something. Why? (Especially padding :-)
--SA
Sergey Alexandrovich Kryukov 19-Jul-12 12:41pm    
Asking again: why? You ignored my question, so why do you think someone will pay attention for yours?

Should I explain why I'm asking? Using the attribute and then truncating/padding takes some work and some performance concerns; and addressing those concerns would be a lot more work.

Now, it it very unlikely that your requirement makes any sense at all. And nobody wants to do too much of wasted work. I don't think that anyone would be helping you if you cannot provide convincing explanation of why would you need such weird thing.

--SA

1 solution

solved by using substring with defined start location and end location.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900