Action on setting value

J

jodleren

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?
 
J

Jeff Gaines

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);}
}
 
J

Jeff Johnson

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();
}
}
 
J

jodleren

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
 
J

Jeff Johnson

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.
 
A

Arne Vajhøj

jodleren said:
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
 
A

Arne Vajhøj

jodleren said:
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
 
J

Jeff Johnson

Arne Vajhøj said:
jodleren said:
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....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top