Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
4.75/5 (4 votes)
See more:
Hi as you may probably know in T-SQL one can assign a length for string/text type columns, for example nvarchar(100). I want to assign a maximum length for my strings in C# classes.

SQL
public class Customer
{
    public string Name { get; set; }
    public string Address { get; set; }
}


How can I assign a maximum length for strings?

Thank you!
Posted
Updated 16-Jan-11 10:54am
v3
Comments
Manfred Rudolf Bihy 16-Jan-11 15:20pm    
You should consider marking the answers below as "Accepted" as they provided you with a working solution.
This is the way people are rewared here at CP for giving solutions to problems others post here.
If an answer is good vote (5) for it and if it solves your problem also mark it as accepted.
Thanks for your attention!
#realJSOP 16-Jan-11 15:58pm    
You should also consider NOT texting. You have plenty of time to type the entire word "you", and "for" instead of 4.

Ok,

You can Make a class for it,

C#
public class LenghtString
{
    public int Lenght { get; set; }

    private string _Value;
    public string Value
    {
        get
        {
            return _Value;
        }
        set
        {
            _Value = value;
            if (_Value.Length > Lenght && Lenght != -1)
            {
                _Value = _Value.Substring(0, Lenght);
            }
        }
    }

    public LenghtString()
        : this(-1)
    {
    }

    public LenghtString(int lenght)
        : this(lenght, string.Empty)
    {
    }

    public LenghtString(string value)
        : this(1, value)
    {
    }

    public LenghtString(int lenght, string value)
    {
        Lenght = lenght;
        Value = value;
    }
}


Now, define your properties as LenghtString :

SQL
public class Customer
{
    public LenghtString Name { get; set; }
    public LenghtString Address { get; set; }
}



So, You may test it :


C#
Customer c = new Customer()
{
   Name = new LenghtString(20,"Shahin Khorshidia"),
   Address = new LenghtString(50, "Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla ...")
};

Console.WriteLine("{0} {1}",c.Name.Value, c.Address.Value);



Good Luck
 
Share this answer
 
Comments
mariazingzing 16-Jan-11 15:11pm    
Thank you for your perfect explanation.
Manfred Rudolf Bihy 16-Jan-11 15:20pm    
Good answer! 5+
fjdiewornncalwe 16-Jan-11 16:39pm    
+5. Very good answer...
o1010wanabe 16-Jan-11 17:01pm    
I'm starting to like classes. here's a 5 for brain synapsis connection value.
thatraja 16-Jan-11 22:50pm    
Perfect answer
Place the logic inside the property.

public string Name
{
  get { return this._name; }
  set 
  { 
        if(value.Length > MAXLEN) 
           this._name = value.Substring(0, Math.Min(value.Length, MAXLEN));
        else this._name = value;
      }
}


I hope this works for you.
 
Share this answer
 
Comments
mariazingzing 16-Jan-11 15:10pm    
Thank u for fast answering.
Manfred Rudolf Bihy 16-Jan-11 15:21pm    
Good call! 5+
fjdiewornncalwe 16-Jan-11 16:40pm    
+5.. Wow. Shahin's answer was my first thought, this was my second. You guys are reading my mind. Well Done.
thatraja 16-Jan-11 22:49pm    
Good answer Abhishek

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