[英]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:目前我已经尝试过:
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
I have defined laughtrack
in the class scope and im getting the same error as attempt 1.我在 class laughtrack
中定义了笑声,我得到了与尝试 1 相同的错误。
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.