471,325 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

property in a class

Hi ,

Can i populate the class variables in following fashion?

public class abc
{
string name;

public string Name
{
set
{
name = value;
}
}
public abc()
{
}
}
abc myclass1 = new abc()
abc.Name="google";

or

should I always populate through constructor or any other method of
class abc?

thanks

Vijay
Jun 27 '08 #1
4 994
On May 28, 4:22 pm, vijaysambhe <vsamb...@gmail.comwrote:
Can i populate the class variables in following fashion?
<snip>

Absolutely - that's what most writable properties look like.
or

should I always populate through constructor or any other method of
class abc?
Well, if you want to create an immutable type you need to pass
everything to the constructor, but if you're okay with mutability,
there's nothing wrong with using a writable property.

Jon
Jun 27 '08 #2
Thanks Jon for your prompt reply.

regards
Vijay
On May 28, 4:31*pm, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On May 28, 4:22 pm, vijaysambhe <vsamb...@gmail.comwrote:
Can i populate the class variables in following fashion?

<snip>

Absolutely - that's what most writable properties look like.
or
should I always populate through constructor or any other method of
class abc?

Well, if you want to create an immutable type you need to pass
everything to the constructor, but if you're okay with mutability,
there's nothing wrong with using a writable property.

Jon
Jun 27 '08 #3
I think you made a typo below. It should be:

abc myclass1 = new abc();
myclass1.Name="google"; // apc.Name will not be correct...
"vijaysambhe" wrote:
Hi ,

Can i populate the class variables in following fashion?

public class abc
{
string name;

public string Name
{
set
{
name = value;
}
}
public abc()
{
}
}
abc myclass1 = new abc()
abc.Name="google";

or

should I always populate through constructor or any other method of
class abc?

thanks

Vijay
Jun 27 '08 #4
Can i populate the class variables in following fashion?
Of course, there is nothing wrong with that
>
or

should I always populate through constructor or any other method of
class abc?
It depends of the class, if the class needs to ALWAYS have a value for
a given property then you use yor constructor for that.
Also providing a constructor with parameters let you do something
like:
callAMethod( new MyClass( ......) );
without having to create an instance of MyClass (note that in 3.5 this
is no longer valid).

BTW, to a class like yours it's usually called to have a "chatty
interface"
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Marty McFly | last post: by
8 posts views Thread by Al | last post: by
14 posts views Thread by Dom | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.