PC Review


Reply
Thread Tools Rate Thread

Action on setting value

 
 
jodleren
Guest
Posts: n/a
 
      8th Mar 2011
Hi

I have something like:

public bool Data;

which is fine - but I want so do something when set - is there a
simple solution for that?

public bool Data
{
get { return value ]
set { set value; DoSomething(); }
}

The value could be the same, the only "change" I want is the
"DoSomething"

Is that possible?
 
Reply With Quote
 
 
 
 
Jeff Gaines
Guest
Posts: n/a
 
      8th Mar 2011
On 08/03/2011 in message
<5cc159e4-abb9-4485-9140-(E-Mail Removed)>
jodleren wrote:

>Hi
>
>I have something like:
>
>public bool Data;
>
>which is fine - but I want so do something when set - is there a
>simple solution for that?
>
>public bool Data
>{
> get { return value ]
> set { set value; DoSomething(); }
>}
>
>The value could be the same, the only "change" I want is the
>"DoSomething"
>
>Is that possible?


What I do is have a private function called SetData:

private void SetData(bool value)
{
Do what's needed
}

and then:

public bool Data
{
get { return value ]
set { SetData( value);}
}



--
Jeff Gaines Wiltshire UK
Greater love hath no man than this, that he lay down his friends for his
life.
(Jeremy Thorpe, 1962)
 
Reply With Quote
 
Jeff Johnson
Guest
Posts: n/a
 
      8th Mar 2011
"jodleren" <(E-Mail Removed)> wrote in message
news:5cc159e4-abb9-4485-9140-(E-Mail Removed)...

> I have something like:
>
> public bool Data;
>
> which is fine - but I want so do something when set - is there a
> simple solution for that?
>
> public bool Data
> {
> get { return value ]
> set { set value; DoSomething(); }
> }
>
> The value could be the same, the only "change" I want is the
> "DoSomething"
>
> Is that possible?


Yes, you've just described "properties." The only thing you're missing is a
backing store for the property (which should NOT be named Data; I'm going to
call it Bob):

private bool _bob;
public bool Bob
{
get { return _bob; }
set
{
_bob = value;
DoSomething();
}
}


 
Reply With Quote
 
jodleren
Guest
Posts: n/a
 
      8th Mar 2011
On Mar 8, 5:32*pm, "Jeff Johnson" <i....@enough.spam> wrote:
> "jodleren" <sonn...@hot.ee> wrote in message
>
> news:5cc159e4-abb9-4485-9140-(E-Mail Removed)...
>
>
>
> > I have something like:

>
> > public bool Data;

>
> > which is fine - but I want so do something when set - is there a
> > simple solution for that?

>
> > public bool Data
> > {
> > * *get { return value ]
> > * set { set value; DoSomething(); }
> > }

>
> > The value could be the same, the only "change" I want is the
> > "DoSomething"

>
> > Is that possible?

>
> Yes, you've just described "properties." The only thing you're missing isa
> backing store for the property (which should NOT be named Data; I'm goingto
> call it Bob):
>
> private bool _bob;
> public bool Bob
> {
> * * get { return _bob; }
> * * set
> * * {
> * * * * _bob = value;
> * * * * DoSomething();
> * * }
>
> }


Thanks
I just hoped there would be an easier option.

But what about:

public List<string> JohnDone = new....

How would you catch changes in there?

components.JohnDone.Add("one more");
components.JohnDone[0] = "empty"


WBR
Sonnich
 
Reply With Quote
 
Jeff Johnson
Guest
Posts: n/a
 
      8th Mar 2011
"jodleren" <(E-Mail Removed)> wrote in message
news:c9dcbbe3-15f1-4dd5-b47e-(E-Mail Removed)...

> But what about:


> public List<string> JohnDone = new....


> How would you catch changes in there?


I've never looked into the BindingList<T> that Pete mentioned, so personally
I'd create my own collection class, delegate most of its functionality to
the List<T> that I use as the backing store, and add code to methods that
change the list, like Add, Clear, Insert, and Remove to do whatever it is I
want to do when the list changes.


 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      8th Mar 2011
On 08-03-2011 11:17, jodleren wrote:
> On Mar 8, 5:32 pm, "Jeff Johnson"<i....@enough.spam> wrote:
>> "jodleren"<sonn...@hot.ee> wrote in message
>> news:5cc159e4-abb9-4485-9140-(E-Mail Removed)...
>>> I have something like:

>>
>>> public bool Data;

>>
>>> which is fine - but I want so do something when set - is there a
>>> simple solution for that?

>>
>>> public bool Data
>>> {
>>> get { return value ]
>>> set { set value; DoSomething(); }
>>> }

>>
>>> The value could be the same, the only "change" I want is the
>>> "DoSomething"

>>
>>> Is that possible?

>>
>> Yes, you've just described "properties." The only thing you're missing is a
>> backing store for the property (which should NOT be named Data; I'm going to
>> call it Bob):
>>
>> private bool _bob;
>> public bool Bob
>> {
>> get { return _bob; }
>> set
>> {
>> _bob = value;
>> DoSomething();
>> }
>>
>> }

>
> I just hoped there would be an easier option.


That is an easy option!

> But what about:
>
> public List<string> JohnDone = new....
>
> How would you catch changes in there?
>
> components.JohnDone.Add("one more");
> components.JohnDone[0] = "empty"


Make the field private, if you need a property then
make only a get that return a readonly list and add
methods that do the modifications of the list.

That is good encapsulation.

Arne

 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      8th Mar 2011
On 08-03-2011 10:32, Jeff Johnson wrote:
> "jodleren"<(E-Mail Removed)> wrote in message
> news:5cc159e4-abb9-4485-9140-(E-Mail Removed)...
>> I have something like:
>>
>> public bool Data;
>>
>> which is fine - but I want so do something when set - is there a
>> simple solution for that?
>>
>> public bool Data
>> {
>> get { return value ]
>> set { set value; DoSomething(); }
>> }
>>
>> The value could be the same, the only "change" I want is the
>> "DoSomething"
>>
>> Is that possible?

>
> Yes, you've just described "properties." The only thing you're missing is a
> backing store for the property (which should NOT be named Data; I'm going to
> call it Bob):
>
> private bool _bob;
> public bool Bob
> {
> get { return _bob; }
> set
> {
> _bob = value;
> DoSomething();
> }
> }


Note that in some cases it can be important whether you assign
or call DoSomething first.

Arne

 
Reply With Quote
 
Jeff Johnson
Guest
Posts: n/a
 
      9th Mar 2011
"Arne Vajhøj" <(E-Mail Removed)> wrote in message
news:4d76be7c$0$23753$(E-Mail Removed)...
> On 08-03-2011 10:32, Jeff Johnson wrote:
>> "jodleren"<(E-Mail Removed)> wrote in message
>> news:5cc159e4-abb9-4485-9140-(E-Mail Removed)...
>>> I have something like:
>>>
>>> public bool Data;
>>>
>>> which is fine - but I want so do something when set - is there a
>>> simple solution for that?
>>>
>>> public bool Data
>>> {
>>> get { return value ]
>>> set { set value; DoSomething(); }
>>> }
>>>
>>> The value could be the same, the only "change" I want is the
>>> "DoSomething"
>>>
>>> Is that possible?

>>
>> Yes, you've just described "properties." The only thing you're missing is
>> a
>> backing store for the property (which should NOT be named Data; I'm going
>> to
>> call it Bob):
>>
>> private bool _bob;
>> public bool Bob
>> {
>> get { return _bob; }
>> set
>> {
>> _bob = value;
>> DoSomething();
>> }
>> }

>
> Note that in some cases it can be important whether you assign
> or call DoSomething first.


Of course. It might also be important to only call DoSomething if the value
of the property is changing, so an if test might be needed. And so on....


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BCM..cannot complete last action...setting up offline DB RT Microsoft Outlook BCM 4 3rd Mar 2008 06:48 PM
Action setting - hyperlink vs. run program =?Utf-8?B?RXBpbm4=?= Microsoft Powerpoint 3 7th Jul 2006 05:40 PM
Action Setting and Timings =?Utf-8?B?VGltIEphbnNvbg==?= Microsoft Powerpoint 2 4th Jul 2006 12:16 AM
my action setting button is not available to use. =?Utf-8?B?S2ltYmVybHk=?= Microsoft Powerpoint 1 5th Oct 2005 03:01 PM
action setting stays gray =?Utf-8?B?RGlhbmU=?= Microsoft Powerpoint 3 27th Sep 2005 03:07 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:01 PM.