property in a class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vijaysambhe

    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="googl e";

    or

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

    thanks

    Vijay
  • Jon Skeet [C# MVP]

    #2
    Re: property in a class

    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

    Comment

    • vijaysambhe

      #3
      Re: property in a class

      Thanks Jon for your prompt reply.

      regards
      Vijay
      On May 28, 4:31 pm, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
      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

      Comment

      • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

        #4
        RE: property in a class

        I think you made a typo below. It should be:

        abc myclass1 = new abc();
        myclass1.Name=" google"; // apc.Name will not be correct...


        "vijaysambh e" 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="googl e";
        >
        or
        >
        should I always populate through constructor or any other method of
        class abc?
        >
        thanks
        >
        Vijay
        >

        Comment

        • Ignacio Machin ( .NET/ C# MVP )

          #5
          Re: property in a class

          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"

          Comment

          Working...