What are the differences between this?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi to all,

I would like to know what are the differences between

protected string mystring; (and after use that like this.mystring=anything

and this..
protected string mystrin
public string MyStrin

get{return mystring;
set{mystring=value;


Thanks in advance
Josema.
 
Josema said:
Hi to all,

I would like to know what are the differences between:

protected string mystring; (and after use that like this.mystring=anything)
this produces a protected string field. This field is accessable only in the
current class and its inheritors.
and this...
protected string mystring
public string MyString
{
get{return mystring;}
set{mystring=value;}
}

This creates a protected string field and a public property to access that
string. Practically this means that any class can access mystring(via
MyString).
 
Hi Josema,
protected string mystring;

allows _only_ derived classes of the enclosing class type
to access the mystring field. whereas the following
protected string mystring
public string MyString
{
get{return mystring;}
set{mystring=value;}
}

allows 'MyString' property to grant public access to
the 'mystring' protected field.
This means that all classes (not only derived ones) would be
able to modify/access the 'mystring' field through 'MyString'.

Regards,
Aravind C
 
Hi to all,

I would like to know what are the differences between:

protected string mystring; (and after use that like
this.mystring=anything)

and this...
protected string mystring
public string MyString
{
get{return mystring;}
set{mystring=value;}
}

Thanks in advance.
Josema.

Apart from that the second won't compile because you are missing a ; as
well as return mystring before mystring is valid will cause unwanted
things :P

The second will keep the control of the variable mystring private (you
should use private, not protected) while exposing limited control over the
content of it. In your example it doesn't make much difference, but
consider this.

private string mystring;

public string MyString
{
get{return mystring}
set
{
if(DoesNotContainUnwantedValues(value))
mystring = value;
}
}

This way your class has something to say about what you can change
mystring to (or MyString since mystring is invisible to the outside world).

thingy.MyString = goodword; // will be accepted
thingy.MyString = badword; // will be rejected
 
Josema said:
I would like to know what are the differences between:

protected string mystring; (and after use that like this.mystring=anything)

Here you can access mystring from the class itself and its inheriting
classes. The drawback is that mystring isn't "controlled", so i can
assign quite everything to mystring (null value too)
and this...
protected string mystring
public string MyString
{
get{return mystring;}
set{mystring=value;}
}

Here you wrap up mystring in a public (accessible to all) property and in
get/set methods you can choose whether to allow assignment of a specific value
or not, doing some computation and so on. But you unmask mystring to inherited
classes.

Maybe in the second example masking mystring as private is better, so also
inherited classes pass through the property. Bye
 
In addition to what everyone else has told you: If that member is on a
design time control, the field (first example) will not appear in the
Properties view, even if you make it public, but the property will.

Also, (might be a little off on this one). I believe that if you mark that
control serializable, and use default serialization (as opposed to
implementing ISerializable), the fields will not serialize, but the
properties will. Might be wrong on that, but I had thought that there was
something to that.
 
Back
Top