Click here to Skip to main content
14,299,246 members
Rate this:
Please Sign up or sign in to vote.
Hi,

I have looked all over for this, the ins/outs of it are that i have a class with an abundance of properties, split by structures etc, instead of writing a class to handle all of the dictionarys/keypairs/subclass for when a 'Double' Value is updated.

So what i want to do is add a 'RaiseEvent' so when the value is set for example:

'Raise an Event here and i will use an event handler in my class to handle it
Dim dblTest as Double = 1.234


Dim dblTest as Double
'Raise Event when the below is done
Double = 1.234

I know you can't call
Dim WithEvents dblTest as Double

as this raises an error 'WithEvents' variables can only be typed as classes, interfaces, or type parameters with class constraints.
Public Class clsMyClass
    Public Structure MyStruct
        Public myVal As Double
    End Structure
    Public Structure MyStruct2
        Public myVal As Double
    End Structure
    Dim MyVar As New MyStruct
    Public Event MyCls_DoubleChangeEvent(ByVal dblValue As Double)

    Private Sub MyCls_HandleEvent() Handles MyClass.MyCls_DoubleChangeEvent
        'Do My Calculations
    End Sub

End Class


Really appreicate if someone can assist in adding an event handler to fire an event when a double value is updated as i dont want to have to add loads of properties to a Function/Sub, far too prone to errors and mishaps.

Regards
Dave
Posted
Comments
ZurdoDev 3-Dec-14 20:08pm
   
I don't know. It still seems like using properties would be the right way to do this.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

It looks like the title and the body of the question are different post.

Not only what is you say in the title is impossible, these words simply make no sense. Same goes to the combination of tags "inheritance", "types" and "override". "Override" is a central concept of OOP, it is only applicable to methods (including property getters/setters); and it also inapplicable (in .NET) to value types or their members.

As to the body of the question, this is, in contrast, not only possible, but trivial. I mean, you can do something like "Dim WithEvents dblTest as Double" (not exactly this way, of course). This is a simple combination of an event you should define in your class, and some double property. I hope you know how to define and invoke the events, or you can just read about it. Now, the property is something which looks exactly as the field for the code using it; it mimic reading and assignment syntax, but these operations can be backed with side effect. In your case, in particular, the side effect would be invocation of the event. In the event arguments, you can pass some data, such as old and new values of the properties at the moment of assignment.

All you need is this:
http://msdn.microsoft.com/en-us/library/ms172877.aspx[^],
http://msdn.microsoft.com/en-us/library/bc3dtbky.aspx[^].

[EDIT]

This is a simple code sample for you:
Public Class DoubleAssignedEventArgs
	Inherits System.EventArgs
	Friend Sub New(oldValue As Double, newValue As Double)
		Me.OldValue = oldValue
		Me.NewValue = newValue
	End Sub
	Public Property OldValue() As Double
		Get
			Return m_OldValue
		End Get
		Private Set
			m_OldValue = Value
		End Set
	End Property
	Private m_OldValue As Double
	Public Property NewValue() As Double
		Get
			Return m_NewValue
		End Get
		Private Set
			m_NewValue = Value
		End Set
	End Property
	Private m_NewValue As Double
End Class

Public Class MyClass
	Public DoubleValueModified As EventHandler(Of DoubleAssignedEventArgs)
	Public Property DoubleValue() As Double
		Get ' getter
			Return doubleValueField
		End Get
		Set ' setter
			Dim oldValue As Double = doubleValueField
			If value = oldValue Then
                                Return ' we don't want to do anything
                                       ' it the new value is the same
			End If
			doubleValueField = value
			If DoubleValueModified IsNot Nothing Then
				DoubleValueModified.Invoke(
                                    Me,
                                    New DoubleAssignedEventArgs(oldValue, value))
			End If
		End Set
	End Property
	Private doubleValueField As Double ' backing field for the property
End Class


I hope it's clear.

—SA
   
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100