Plance Development
Plance Development

Reputation: 45

Passing variables from private void to other private void C#

I'm working with a thermal camera and the provided SDK. But the problem is that I'm real beginner in .NET because normally I develop in PHP.

So what I'm trying to do is passing variables from a private void to another private void. For example, I'm trying to pass the variable ToPass from the void "OnDetect" to the void OnReferenceRegion.

private void OnDetect(CallbackEventArgument callbackArgument)
    {
       string ToPass = "This string needs to be passed";
    }

Here is the OnReferenceRegion void

    private void OnReferenceRegion(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        String completeString = "";
        
        completeString = ToPass;

    }

Upvotes: 1

Views: 2204

Answers (2)

Purity
Purity

Reputation: 331

There are 4 ways to "pass variables" between void-functions:

  1. Accessing fields within shared scope of both functions.
  2. Capturing variables with delegates or local functions.
  3. ByRef Parameters (ref, out, in).
  4. Pointers in an unsafe context.

In your case you probably want the first one, i.e. declaring a field/property in the containing class or any other scope those two functions have access to.

Upvotes: 3

Johnathan Barclay
Johnathan Barclay

Reputation: 20373

You can add a private field, which has class-wide scope and thus can be accessed by both methods:

class YourClass
{
    private string toPass; // Has class-wide scope

    private void OnDetect(CallbackEventArgument callbackArgument)
    {
       toPass = "This string needs to be passed";
    }

    private void OnReferenceRegion(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        string completeString = "";
        
        completeString = toPass;
    }
}

Upvotes: 3

Related Questions