ProfK
ProfK

Reputation: 51104

How can an external event be known in one sub and not in another, in the same class?

My question is probably best illustrated by the image.

In Sub New, CompositionTarget.Rendering is well known, but stuck with my throwback to C# syntax. VB.NET is hard! Yet in CompositionTargetRendering, the compiler has never come across it before. It's declared in System.Windows.Media, which I certainly have in my imports.

Is my declaration of the handler incorrect or something? Or is Xenu messing with me>

Easiest way to explain.

Oh yes, the C# I'm translating from compiles just fine:

    CompositionTarget.Rendering += CompositionTargetRendering;
    private void CompositionTargetRendering(object sender, EventArgs e)
    {
        if (stopwatch.ElapsedMilliseconds > lastUpdateMilliSeconds + 5000)
        {
            viewModel.UpdateModel();
            Plot1.RefreshPlot(true);
            lastUpdateMilliSeconds = stopwatch.ElapsedMilliseconds;
        }
    }

Upvotes: 1

Views: 66

Answers (1)

Matt Wilko
Matt Wilko

Reputation: 27342

You don't add handlers in VB.NET with += you need to either:

Declare the object WithEvents and use the Handles keyword

Private WithEvents MyCompositionTarget As CompositionTarget

Private Sub CompositionTargetRendering() Handles MyCompositionTarget.Rendering
     'code for event here
End Sub

Or use AddHandler (which works in the same way as C# +=) (Don't use Handles keyword)

Private MyCompositionTarget As CompositionTarget

Publlic Sub New
    AddHandler MyCompositionTarget.Rendering, AddressOf CompositionTargetRendering
End Sub

Private Sub CompositionTargetRendering()
     'code for event here
End Sub

Upvotes: 1

Related Questions