简体   繁体   中英

How do I access a var from another method without calling the method

Hello this is my fist real post on stack overflow so sorry if I didn't do this correctly:D

namespace foobar //not originally named foobar
{
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        int Spinrate = 2000;
        int Points = 1;
        int clickmulti = 1;
        public static object Public { get; private set; }

        public MainPage()
        {
            InitializeComponent();
            var gamemusic = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current;
            var laughtrack = CrossSimpleAudioPlayer.CreateSimpleAudioPlayer();
            gamemusic.Load("audioloop.mp3");
            laughtrack.Load("wslaugh.wav");
            gamemusic.Play();
        }

        async void bigger(object sender, EventArgs args)
        {

            Points += Spinrate + (50 * 2 * clickmulti);
            Pointdisplay.Text = Convert.ToString(Points);
            cmdisplay.Text = Convert.ToString(clickmulti);
            if (Spinrate >= 300)
            {
                Spinrate -= 50;
            }
            if (Spinrate <= 1000)
            {
                clickmulti = 2;
            }
            if (Spinrate <= 500)
            {
                clickmulti = 3;
            }
            await img.RelRotateTo(360, (uint)Spinrate);
            laughtrack.Play();
        }


    }

}

I am currently trying to figure out how to call without having to define as either a public (since when using var it can't for some reason) and without having to define var laughtrack = CrossSimpleAudioPlayer.CreateSimpleAudioPlayer(); in the method bigger since it creates a lot of latency in the program.

Currently I have tried:

  1. Making laughtrack a public var, but it errors out with:

Error CS0825 The contextual keyword 'var' may only appear within a local variable declaration or in script code foobar as the output

  1. I have defined laughtrack in the class scope and im getting the same error as attempt 1.

  2. I have added the declaration and call for laughtrack in the same method. It seems to cause a lot of latency and even if it isn't the cause, it still is loading it every single time the method is called.

Thanks to all who give this a shot!

you need to declare laughtrack at the class level (not inside a specific method) if you want it to be visible in other methods. But if you do this you can't use var , you have to know the actual type.

ISimpleAudioPlayer laughtrack;

public MainPage()
{
  InitializeComponent();
  var gamemusic = Plugin.SimpleAudioPlayer.CrossSimpleAudioPlayer.Current;
  laughtrack = CrossSimpleAudioPlayer.CreateSimpleAudioPlayer();
  gamemusic.Load("audioloop.mp3");
  laughtrack.Load("wslaugh.wav");
  gamemusic.Play();
}

Your first attempt was great and exactly what you need to do, however var isn't usable outside of functions. Just explicitly write the type of the variables!

When you create a method or let's simply call it a Function, all variables or nested functions you define inside of it are scoped to that particular function or block of code and will not be available outside that scope unless returned or thrown out by that function.

In your case, the standard approach is to define a struct with properties and methods. But, please consider how those properties are initiated. If that is through calling a function, then you will still need to call the function, but it allows for a Singleton behavior.

To further clarify, consider the concept of Pure Functional programming where a Pure Function takes some inputs and returns a single output. In OOP, the method can update global variables (there's no such thing in C#, but C++ and C have those) and scoped properties/variables inside a class or struct as well. Though, you cannot directly access its members.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM