Action on setting value

  • Thread starter Thread starter jodleren
  • Start date Start date
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?
 
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);}
}
 
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 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
 
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.
 
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
 
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
 
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....
 
Back
Top