简体   繁体   English

如何在不调用该方法的情况下从另一个方法访问 var

[英]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您好,这是我关于堆栈溢出的第一个真实帖子,如果我没有正确执行此操作,非常抱歉: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();我目前正在尝试弄清楚如何调用而不必定义为公共(因为使用var时由于某种原因它不能)并且不必定义var laughtrack = CrossSimpleAudioPlayer.CreateSimpleAudioPlayer(); in the method bigger since it creates a lot of latency in the program.在方法中bigger ,因为它会在程序中产生很多延迟。

Currently I have tried:目前我已经尝试过:

  1. Making laughtrack a public var, but it errors out with:使laughtrack成为公共变量,但错误如下:

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

  1. I have defined laughtrack in the class scope and im getting the same error as attempt 1.我在 class laughtrack中定义了笑声,我得到了与尝试 1 相同的错误。

  2. I have added the declaration and call for laughtrack in the same method.我已经添加了声明并以相同的方法调用了laughtrack 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.如果您希望它在其他方法中可见,则需要在laughtrack级别(不在特定方法内)声明笑声 But if you do this you can't use var , you have to know the actual type.但是如果你这样做你不能使用var ,你必须知道实际的类型。

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.您的第一次尝试很棒,这正是您需要做的,但是var在函数之外无法使用。 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. 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.如果这是通过调用 function,那么您仍然需要调用 function,但它允许 Singleton 行为。

To further clarify, consider the concept of Pure Functional programming where a Pure Function takes some inputs and returns a single output.为了进一步阐明,考虑纯函数式编程的概念,其中纯 Function 接受一些输入并返回单个 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. 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.但是,您不能直接访问其成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不调用重写的情况下从基类中的另一个方法调用基虚方法? - How do I call a base virtual method from another method in the base class without calling the overrides? 在C#中如何从另一方法访问LINQ var数组? - In C# how do access my LINQ var array from another method? 如何将方法设为私有并从另一个类访问它? - How do I make a method private and access it from another class? 如何从其他方法访问控件 - How do I access my control from another method 如何从另一方法的一种方法引用“ var”? - How to reference a 'var' from one method in another? 如何从同一 class C# 中的另一种方法访问一种方法的变量? - How do I access the variable of one method from another method within the same class C#? 如何在没有实例化,静态变量或传递引用的情况下从另一个类/线程访问方法? - how do you access a method from another class / thread without instantiation, static variables or passing references? 使用LINQ调用存储过程,如何从方法中传回var? - Using LINQ calling a sproc, how can I pass a var back from a method? 如何在没有提交按钮的情况下访问Post方法 - How do I access a Post method without a submit button C#如何在不使方法静态的情况下从界面内的另一个类调用方法? - C# How do I call a method from another class inside of my interface without making the method static?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM