Question Assigning the value of a label to a variable byref?

eddy556

Member
Joined
May 14, 2007
Messages
11
Programming Experience
1-3
I would like a label to always show the current value of a boolean. This will change a lot of times and would like the label to ALWAYS reflect this.

Is there some kind of way to have the label set to the boolean byref (so its always the same). Currently I'm assigning the label.text = boolean onLoad, so if the boolean changes after that its not reflected.
 
ByRef is completely irrelevant because it only applies to method arguments. Put simply, what you're asking for is impossible with fields, which is one of the main reason that .NET provides properties. The idea of a property is that from the outside it behaves like a field but from the inside it behaves like a method. That allows you to do things when someone gets or sets the property value. The two most common things to do are validation and raise an event. It's the event that is of interest to you.

Turn your Boolean field into a property and then access the property only. In the setter, raise an event to indicate that the value has changed. The form containing the Label can then handle that event and update the Label each time it's raised. To learn how to raise an event when the property changes, follow the Blog link in my signature and read my post on Defining & Raising Custom Events.
 
ByRef is completely irrelevant because it only applies to method arguments. Put simply, what you're asking for is impossible with fields, which is one of the main reason that .NET provides properties. The idea of a property is that from the outside it behaves like a field but from the inside it behaves like a method. That allows you to do things when someone gets or sets the property value. The two most common things to do are validation and raise an event. It's the event that is of interest to you.

Turn your Boolean field into a property and then access the property only. In the setter, raise an event to indicate that the value has changed. The form containing the Label can then handle that event and update the Label each time it's raised. To learn how to raise an event when the property changes, follow the Blog link in my signature and read my post on Defining & Raising Custom Events.

Many thanks.

I've actually being using custom events and handling quite a bit throughout my app however just as I was writing one for this purpose this morning I had a 'brainwave' and thought this would be possible. Is it not at all possible to set the property of a control to a pointer, rather than the value itself?
 
You can also use a Boolean application user setting and bind that to the control Text. When you change that My.Settings the control updates automatically.
How to: Create Application Settings Using the Designer
How to: Bind a Property to an Existing Application Setting Using the Designer

Nice one. This is basically doing what I suggested but with no code at all, because the system does pretty much everything for you. I guess the only issue with that might be that it takes the Boolean member out of its current type altogether, which may or may not be a problem. By renaming the existing field and then declaring your own property with the old field name, all existing code continues to work exactly as it is.
 
Whoops, didn't think of that. That will prevent you from selecting the setting in control designer, since it will only list settings of String type. Still you can add the binding in code and it will work properly as expected by converting the Boolean value to its respective String representation ("True"/"False" strings). To add the binding in code for example:
VB.NET:
Me.Label1.DataBindings.Add("Text", My.Settings, "boolsetting")
added note: this also works for two way control binding, for example an editable Textbox, where the input text is attempted converted to Boolean and either submitted or discarded (where textbox text reverts to previous valid value).
 
Anyways, some easy code to look at:

VB.NET:
Private prop_Boolvalue As Boolean = False 'Or default value
    Friend Property BoolValue As Boolean
        Get
            Return prop_Boolvalue
        End Get
        Set(ByVal value As Boolean)
            prop_Boolvalue = value
            label1.Text = value
        End Set
    End Property
 
Back
Top