Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hai,

Can anybody explain me whats the difference between Properties and normal Fields in csharp. If u show some example then it would be more better...

And i got some error here while assigning null to a integer property....
C#
private int a;
public int A
{
    get {
        return a;
    }
    set {
        a = value;
    }
}
private int b;
public int B
{
    get { return b; }
    set { b = value; }
}

Assigning
*********
C#
Class1 c1 = new Class1

c1.A= int.parse(tb1.text)  // Here text box is empty so getting error

c1.B=int.parse(tb2.text)   

Can anybody explain me regarding this.....

Thanks in advance...
Posted
Updated 24-Sep-10 4:27am
v2

To understand how a property is different from a field, imagine you have derived a class from a Label control, to add it the capability to change the foreground color of the shown text. Your code will be similar to the following:

C#
public class ColoredLabel : Label
{
   private Color _Foreground = Color.Black;

   ...

   public Color Foreground
   {
      get
      {
         return _Foreground;
      }
      set
      {
         _Foreground = value;
         Invalidate();
      }
   }

   ...

}


As you can see, using a property allow you to do more than simply get and/or set the value of a field.

About the error that you are getting, see the documentation of the Int32.Parse Method (String)[^]: as it could fails and throws an exception in some cases, you have to add exception-handling to your code to make it robust:

C#
try
{
   c1.A = int.Parse(tb1.Text);
}
catch (FormatException e)
{
   // The text in the TextBox doesn't match an int
   ...
}
catch (OverflowException e)
{
   // The number in the TextBox is out of the admitted range for an int
   string text = tb1.Text;
   text.Trim();
   if (text.StartsWith("-"))
      c1.A = int.MinValue;
   else
      c1.A = int.MaxValue;
}
catch (Exception e)
{
   // Something else goes wrong
   ...
}
 
Share this answer
 
v2
In your sample above private int a; is a field while public int A is a property; the property contains a setter and/or getter, and may also include validation or other supporting code. A field is merely a variable.

In your assignment statement you receive an error because null is not an integer and so cannot be assigned to c1.A. Later versions of .NET introduce the concept of nullable variables.
 
Share this answer
 
Use the int.TryParse method. It returns a bool that indicates if it was a valid integer so that you don't have to rely on the try/catch mechanism (there's really no point in throwing an exception when you can otherwise easily enough verify/react to the expected results.
 
Share this answer
 
v2
Comments
Sauro Viti 24-Sep-10 11:05am    
I'm with you! It's easier to use TryParse than handling an exception; I proposed the try/catch mechanism but only because it allow to distinguish between overflow and other failure reasons.
you cannot parse empty value to int, thats why its throwing error.

use int.parse(tb1.text == string.empty? "0" : tb1.text)

make sure that if tb1.text is not empty, should return int value for parsing
 
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