0

I realize this might be spoon feeding but I've already tried many other ways to do this but to no avail. I've already researched a lot including this one which I think might be the right way "variable/property changed event in vb.net" but I cant get it to work the way I want it . Im new to coding and I'm trying to add an on change event on my variable so here's my sample.

Public Class FormMode
    Public Property ViewMode As Boolean
    Public Property EditMode As Boolean
    Public Property NewMode As Boolean
End Class

So basically I have forms and each of them has their own FormMode. The form modes can only be true 1 at a time i. e.

ViewMode = True
EditMode = False
NewMode = False

Or

ViewMode = False
EditMode = True
NewMode = False

Or

ViewMode = False
EditMode = False
NewMode = True

The User can and will change the mode of the form. What I want is if they change ViewMode = True the EditMode and NewMode must be automatically be set to false. Same case to the other two. Please note that they will change to value from another form. Also I want to be able to look at if the form is in what form (ViewMode/EditMode/NewMode). Am I doing this the right way or should I try a different approach? Please bear with my noobish and redundant question as I already tried my best to dothis but unfortunately failed. Thank a lot!

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • 1
    A) Rather than 3 booleans to indicate *one* state, consider an `Enum` (examine the ListView's View property) B) `INotifyPropertyChanged` is overkill. It is meant for a class to notify other things of a change. You dont need to notify yourself. If you still think 3 properties for one state is a good idea, dont use Auto properties and reset the others in the setters. C) Please read [Ask] and take the [tour] – Ňɏssa Pøngjǣrdenlarp Oct 07 '17 at 15:36
  • Thank you. I used the enum as you suggested and I managed to get it done. I wasnt aware of that before now I know better. Thanks a lot! – John Rivera Oct 07 '17 at 15:58
  • Please read [Ask] and take the [tour] – Ňɏssa Pøngjǣrdenlarp Oct 07 '17 at 16:01

2 Answers2

0

I used enum as suggested and managed to fit it in my needs .Thanks

Public Class Form1
    Public Property TFormMode As FormMode
    Public Enum FormMode
       ViewMode
       EditMode
       NewMode
    End Enum

    Public Function IsView() As Boolean
        TFormMode = FormMode.ViewMode
    End Function

    Public Function IsNew() As Boolean
        TFormMode = FormMode.NewMode
    End Function

    Public Function IsEdit() As Boolean
        TFormMode = FormMode.EditMode
    End Function

    Public Sub UpdateFormMode(Mode As FormMode)
        TFormMode = Mode
    End Sub
End Class

I still made the 3 boolean just so its easier to check if its in a certain mode or not.

0

The code you posted as an answer won't work as you expect. Each time that you check to see if IsView is true, your code will actually set TFormMode to FormMode.ViewMode and wont actually return a true or false boolean - the same is the case for IsNew and IsEdit

What you should do is use the code below. I'll explain with a walkthrough.

Declaring _TFormMode as a private variable gives your class a variable to work with internally and it isn't directly visible to the outside world

Your Enum is fine as it is.

Declaring TFormMode as the public property gives the code outside your class a safe way of interacting with the private field _TFormMode. This way, using the Set and Get code blocks, you can add any validation code or return whatever information you like. This makes you UpdateTFormMode sub redunant.

The function IsView will now return the result of the expression _TFormMode = FormMode.ViewMode.

A longer version of

Return _TFormMode = FormMode.ViewMode

would be

If _TFormMode = FormMode.ViewMode Then
    Return True
Else
    Return False
End If

The remaining functions do the same thing, but test against the other Enum values

Public Class Form1
        Private _TFormMode As FormMode

        Public Enum FormMode
            ViewMode
            EditMode
            NewMode
        End Enum

        Public Property TFormMode As FormMode
            Set(value As FormMode)
                _TFormMode = value
            End Set
            Get
                Return _TFormMode
            End Get
        End Property


        Public Function IsView() As Boolean
            Return _TFormMode = FormMode.ViewMode
        End Function

        Public Function IsNew() As Boolean
            Return _TFormMode = FormMode.NewMode
        End Function

        Public Function IsEdit() As Boolean
            Return _TFormMode = FormMode.EditMode
        End Function

    End Class
David Wilson
  • 4,369
  • 3
  • 18
  • 31