[.net] A simple question about get/set.... C# .NET

Started by
20 comments, last by Kaze 16 years, 11 months ago
Quote:Original post by gharen2
You're overlooking the fact that you have multiple references to the same instance.

The reference returned by the get property is a separate reference to the same instance. If you change the retrieved reference to something else, the original reference inside the class will still point to the original instance.

In your example, I could retrieve a reference to sfFoo, and set that reference to null. However, the reference to sfFoo inside the Foo class is still intact.

In my opinion this makes the const keyword unnecessary.

I don't think you quite understand the purpose of const. It's not to guarantee that a particular area of memory will remain constant across a certain period of time. Rather, it forces a distinction between mutator and nonmutator methods, which turns out to be really useful for uncovering a variety of common logic errors.
Advertisement
Quote:Original post by Sneftel
I don't think you quite understand the purpose of const. It's not to guarantee that a particular area of memory will remain constant across a certain period of time. Rather, it forces a distinction between mutator and nonmutator methods, which turns out to be really useful for uncovering a variety of common logic errors.


I'd say you're correct :)

Thanks
Quote:Original post by steven katic
pros and cons of good and bad programming aside you could make set private, thus:

class SomeFoo;
class Foo
{
private SomeFoo sfFOO = new SomeFoo();
public SomeFoo SomeFOO{ get{ return sfFOO ; } private set{ sfFOO = value; } }
}


this wouldn't help in this specific case...

Foo x = new Foo;x.SomeFOO.Value = 13453;


The OP wants to prevent such manipulation of the original object stored in the property. This, afaik, is not possible. I've ran into this problem once till today, but I could work it around by a little redesigning.
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

Quote:Original post by davepermen
this wouldn't help in this specific case...

Foo x = new Foo;x.SomeFOO.Value = 13453;


The OP wants to prevent such manipulation of the original object stored in the property. This, afaik, is not possible. I've ran into this problem once till today, but I could work it around by a little redesigning.

class SomeFoo{   public readonly Value;}class Foo{  private SomeFoo sfFOO = new SomeFoo();  public SomeFoo SomeFOO { get { return sfFOO; } } }}class test{  Foo x = new Foo;  x.SomeFOO.Value = 13453; <- A readonly field cannot be assigned to.}


[Edited by - Zanshibumi on May 24, 2007 6:28:52 AM]
Wouldn't marking it read only only allow variable asignment to be in the constructor, so would would be readonly everywhere.
Quote:Original post by Niksan2
Wouldn't marking it read only only allow variable asignment to be in the constructor, so would would be readonly everywhere.

Yes.

I think I fail to see the problem. [Edit: I think I saw the problem now]

public class SomeFoo    {        internal int theValue;        public int Value { get { return theValue; } }    }    public class Foo    {        private SomeFoo sfFOO = new SomeFoo();        public SomeFoo SomeFOO { get { return sfFOO; } }         public void ChangeTheValue()        {            sfFOO.theValue = 33;        }    }//The test class is in another assembly.class test    {        public test()        {            Foo x = new Foo();            x.SomeFOO.Value = 13453; //error.        }    }


[Edited by - Zanshibumi on May 24, 2007 6:34:04 AM]
but using the internal keyword doesn't that mean it has to sit in its own assembly ?

The only way I can think of doing it and is very ugly is to have 3 classes, and in turn creates problems depending the depth on teh class with the data.. like so


		// exposed elsewhere		public class SomeFoo		{			protected int _value;			public int Value { get { return _value; } }		}		// local accessor		internal class SomeFooAccesor : SomeFoo		{			public SomeFoo SomeFOO  { get { return this; } }			public void Assign(int val)			{				this._value = val;			}		};		public class Foo		{			private SomeFooAccesor _foo = new SomeFooAccesor();			public SomeFoo SomeFOO { get { return _foo; } }			public void DoSomething()			{				_foo.Assign(12);			}		};		Foo x = new Foo();		x.SomeFOO.Value = 1; // compile error


As said before, using structs is better and having the data duplicated should be temporary, and still has the member depth problem, so if you have in the structure a reference memebr you'll still have the problem there, and with const, what you're wanting is a compile time error/warning I guess :/

It is a problem.
Quote:Original post by Niksan2
but using the internal keyword doesn't that mean it has to sit in its own assembly?

No, It has to be in the same assembly as the class that is going to access it.


This would be easier with an example of a situation where this kind of separation is needed. :)
Yeah that's what I meant, and agreed, depends on how it's going to be implimented to get a workaround for it.
Well that sums up my question :(. Yea there are some good ideas here, but unfortunatly it just seems the best way to do this is to create a clone and send it back, or make it a value type. This is frustrating. Any1 have a link to some discussion topics on this? or some petition :)? or info if microsoft will be releasing a new version of C#?

For now I will waste some space and send a clone. The item im using does not want to be a struct unfortunatly(I need it to take as a reference in most cases).

Thanks VERY MUCH,
Brad
--X

This topic is closed to new replies.

Advertisement