Hello,

I'm wondering if I should take this personally.

I haven't quote grasped this whole "class" concept yet, but I'm working on it.

When following the instructions below, the "test" portion of this code: "test.variable"

has an underlined error that says that "test" is not declared.

If i remove the "test" word then the ".variable" shows not declared.

I'm using Visual Studios 8 if that makes any difference.

I have a pre-existing variable in a project that I would like to monitor for changes.

Any suggestions?

Re: Detect variable change.
Make your variable a class with an event.
Make your class:
VB.NET Syntax (Toggle Plain Text)

Public Class myVar

      Private mValue As Integer

      Public Event VariableChanged(ByVal mvalue As Integer)

       

      Public Property Variable() As Integer

      Get

      Variable = mValue

      End Get

      Set(ByVal value As Integer)

      mValue = value

      RaiseEvent VariableChanged(mValue)

      End Set

      End Property

      End Class

Public Class myVar Private mValue As Integer Public Event VariableChanged(ByVal mvalue As Integer) Public Property <strong class="highlight">Variable</strong>() As Integer Get <strong class="highlight">Variable</strong> = mValue End Get Set(ByVal <strong class="highlight">value</strong> As Integer) mValue = <strong class="highlight">value</strong> RaiseEvent VariableChanged(mValue) End Set End Property End Class
Now in your main form create a textbox and button:
VB.NET Syntax (Toggle Plain Text)

Public Class Form1

      Private WithEvents test As New myVar

       

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

      test.Variable = CInt(TextBox1.Text)

      End Sub

       

      Private Sub VariableChanged(ByVal NewValue As Integer) Handles test.VariableChanged

      MessageBox.Show(NewValue)

      End Sub

      End Class

My understanding is pretty limited, but when if it was my code, I would say to put a sub routine for New() in the class code, so as to define the parameters of the new object when you declare it in code.

I would think of it as you are saying "Make a new object called 'test'. Make it just like a regular old 'myvar'." However in your class above, your not telling it how to make it like myvar, since there's no constructor method.

I'm just a newbie though, and perhaps a more seasoned programmer could chime in.

Never take anything personally. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.