How to trigger event when a variable's value is changed?
Seems to me like you want to create a property.
public int MyProperty
{
get { return _myProperty; }
set
{
_myProperty = value;
if (_myProperty == 1)
{
// DO SOMETHING HERE
}
}
}
private int _myProperty;
This allows you to run some code any time the property value changes. You could raise an event here, if you wanted.
Trigger an Event when a Variable Value Changes
Rather than using a field, or an auto property, you can specifically define the get/set behaviour of a property (with a backing field).
In this way you can then trigger actions/events whenever the value of that property changes. For example ...
private Status _myStatus;
public Status MyStatus
{
get { return _myStatus; }
set
{
if (value == _myStatus) return;
_myStatus = value;
// RAISE YOUR EVENT HERE
}
}
This example will allow an event to be raised whenever the value of MyStatus
changes. Obviously I've made an assumption that the Type
of MyStatus
is Status
so just change that to string
or whatever the type is.
In javascript, how to trigger event when a variable's value is changed?
There is no event which is raised when a given value is changed in Javascript. What you can do is provide a set of functions that wrap the specific values and generate events when they are called to modify the values.
function Create(callback) {
var isGreen = false;
var isRunning = false;
return {
getIsGreen : function() { return isGreen; },
setIsGreen : function(p) { isGreen = p; callback(isGreen, isRunning); },
getIsRunning : function() { return isRunning; },
setIsRunning : function(p) { isRunning = p; callback(isGreen, isRunning); }
};
}
Now you could call this function and link the callback to execute go():
var traffic = Create(function(isGreen, isRunning) {
if (isGreen && !isRunning) {
go();
}
});
traffic.setIsGreen(true);
Change in variable triggers an event
Yes this is possible. However, you'll need an Object Oriented approach.
You first need to define a class that raises the events you wish to hook into. Secondly you'll need a class that actually handles the event, since you can not use event handlers in a regular module.
Thirdly, in your regular module you can then just use these classes.
Here's a simple example:
Create a class module named "ClassWithEvent". Place the following code:
Public Event VariableChange(value As Integer)
Private p_int As Integer
Public Property Get value() As Integer
value = p_int
End Property
Public Property Let value(value As Integer)
If p_int <> value Then RaiseEvent VariableChange(value) 'Only raise on actual change.
p_int = value
End Property
Next, create the class that can handle the events raised by this class.
Name this Class Module "ClassHandlesEvent". Place the following code in it:
Private WithEvents SomeVar As ClassWithEvent
Private Sub SomeVar_VariableChange(value As Integer) 'This is the event handler.
Select Case value
Case 1:
MsgBox "here, 1!"
Case 2:
MsgBox "here, 2!"
Case Default:
'Do Nothing
End Select
End Sub
Public Property Get EventVariable() As ClassWithEvent
Set EventVariable = SomeVar
End Property
Public Property Let EventVariable(value As ClassWithEvent)
Set SomeVar = value
End Property
Next, in a regular module, instantiate your ClassWithEvent and pass this one as a property to the class that handles them.
Sub test()
Dim var As ClassHandlesEvent
Dim tst As ClassWithEvent
Set var = New ClassHandlesEvent
Set tst = New ClassWithEvent
var.EventVariable = tst
tst.value = 2 'A messagebox saying "Here, 2!" will pop-up
End Sub
Angular - trigger an event based on a variable change
You can use the EventEmitter
and @Output()
to achieve your goal.
Dropdown menu component:
import { Component, Output, EventEmitter, NgModule } from '@angular/core';
@Component({
selector: 'app-dropdown',
template: `
<div>
<select [(ngModel)]="selected" (ngModelChange)="onChange($event)">
<option *ngFor="let category of categories" [value]="category">{{category}}</option>
</select>
</div>`
})
export class DropdownComponent {
@Output() selectedCategory: EventEmitter<string> = new EventEmitter<string>();
categories = ["All categories", "A", "B", "C"]; //your categories list
selected: string = "All categories"; //default value
constructor() {
}
onChange(newvalue): void {
this.selectedCategory.emit(newvalue);
}
}
Here is the other component that will receive the selected value whenever it changes from the dropdown component
import {Component, NgModule} from '@angular/core'
@Component({
selector: 'app-main',
template: `
<div>
<app-dropdown (selectedCategory)='onChange($event)'></app-dropdown>
</div>
`,
})
export class MainComponent {
selected: string;
constructor() {
}
onChange(category):void {
this.selected = category;
//get your items list based on the selected category
}
}
Related Topics
Using Linq to Remove Elements from a List<T>
Angular Post Request Received in Net Core API as Null
How to Remove Empty Lines from a Formatted String
Restsharp Post Request - Body With X-Www-Form-Urlencoded Values
Convert Date Format of String to Datetime Mm/Dd/Yyyy When System Date Formate Is Dd/Mm/Yyyy
Decrypt M3U8 Playlist Encrypted With Aes-128 Without Iv
Post Json Array to MVC Controller
Disable Required Validation Attribute Under Certain Circumstances
How to Automatically Increment Numbers in C#
Get All Column Names of a Datatable into String Array Using (Linq/Predicate)
Pass Variable in Json String in C#
How to Ignore Convert to Nullable in Lambda Expressions
Asp.Net Core Form Post Results in a Http 415 Unsupported Media Type Response
How to Test If a Instance of a Class Is a Specific Generic Type
How to Change Cell Background Color in Wpf Datagrid
Display Label Text With Line Breaks in C#
Unicode Characters Replace from String Using C#
Download File from an ASP.NET Web API Method Using Angularjs