Visual Studio .NET - Code not working properly

Asked By aman on 28-Jul-11 04:27 AM
hi all,

the below code is for custom attribute

private void button1_Click(object sender, EventArgs e)

{

clsCustomer obj = new clsCustomer();

obj._strCustomerCode = textBox1.Text;

obj._strCustomerName = textBox2.Text;

IFormatter formatter = new BinaryFormatter();

Stream stream = new FileStream("E:\\CustomerAttribute.bin", FileMode.Create, FileAccess.Write, FileShare.None);

formatter.Serialize(stream, obj);

stream.Close();

textBox1.Text = "";

textBox2.Text = "";

}

}

[Serializable]

public class clsCustomer

{

public string _strCustomerCode;

public string _strCustomerName;

public string strCustomerCode

{

get { return _strCustomerCode; }

set

{

if (_strCustomerCode.Length < 5)

{

_strCustomerCode = value;

}

else

{

Console.WriteLine("The Length of Code Should be less than 5");

}

}

}

public string strCustomerName

{

get { return _strCustomerName; }

set

{

if (_strCustomerName.Length < 10)

{

_strCustomerName = value;

}

else

{

Console.WriteLine("The Length of Name Should be less than 10");

}

}

}

}



the validation does not occuring please help
Asked By aman on 28-Jul-11 04:35 AM

public string strCustomerCode

{

get { return _strCustomerCode; }

set

{

if (_strCustomerCode.Length < 5) the error //Object reference not set to an instance of an object.

{

_strCustomerCode = value;

}

else

{

Console.WriteLine("The Length of Code Should be less than 5");

}

}

}

Jitendra Faye replied to aman on 28-Jul-11 04:35 AM
Change your code like this-

private void button1_Click(object sender, EventArgs e)

{

clsCustomer obj = new clsCustomer();

obj.strCustomerCode = textBox1.Text;

obj.strCustomerName = textBox2.Text;

IFormatter formatter = new BinaryFormatter();

Stream stream = new FileStream("E:\\CustomerAttribute.bin", FileMode.Create, FileAccess.Write, FileShare.None);

formatter.Serialize(stream, obj);

stream.Close();

textBox1.Text = "";

textBox2.Text = "";

}

}

[Serializable]

public class clsCustomer








{

private string _strCustomerCode;

private string _strCustomerName;

public string strCustomerCode

{

get { return _strCustomerCode; }

set





{

if (_strCustomerCode.Length < 5)

{

_strCustomerCode = value;

}

else



{

Console.WriteLine("The Length of Code Should be less than 5");

}

}

}

public string strCustomerName

{

get { return _strCustomerName; }

set




{

if (_strCustomerName.Length < 10)

{

_strCustomerName = value;

}

else



{

Console.WriteLine("The Length of Name Should be less than 10");

}

}

}

}


Irfan Khan replied to aman on 28-Jul-11 06:00 AM

Gets or sets the maximum number of characters the user can type or paste into the text box control. Every textbox has a MaxLength property you could test for. Text boxes have a .Length API call which will tell you how many characters are currently in the text box. It looks something like this:

C# Syntax

If(textBox1.Text.Length >= 50)

{

                    MessageBox.Show("Invalid input. Value must be less than 50 characters");

}

Else

{

                    //Acceptable input length

}

Example

The following code example uses the derived class, TextBox, to create a text box that is used to accept a password. This example uses the http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.charactercasing.aspx property to change all characters typed to uppercase and the MaxLength property to restrict the password length to eight characters. This example also uses the http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.textalign.aspx property to center the password in the http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.aspx control.

Public Sub CreateMyPasswordTextBox()

    ' Create an instance of the TextBox control.

    Dim textBox1 As New TextBox()

    ' Set the maximum length of text in the control to eight.

    textBox1.MaxLength = 8

    ' Assign the asterisk to be the password character.

    textBox1.PasswordChar = "*"c

    ' Change all text entered to be uppercase.

    textBox1.CharacterCasing = CharacterCasing.Upper

    ' Align the text in the center of the TextBox control.

    textBox1.TextAlign = HorizontalAlignment.Center

End Sub