簡體   English   中英

如何在不調用該方法的情況下從另一個方法訪問 var

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

您好,這是我關於堆棧溢出的第一個真實帖子,如果我沒有正確執行此操作,非常抱歉: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();
        }


    }

}

我目前正在嘗試弄清楚如何調用而不必定義為公共(因為使用var時由於某種原因它不能)並且不必定義var laughtrack = CrossSimpleAudioPlayer.CreateSimpleAudioPlayer(); 在方法中bigger ,因為它會在程序中產生很多延遲。

目前我已經嘗試過:

  1. 使laughtrack成為公共變量,但錯誤如下:

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

  1. 我在 class laughtrack中定義了笑聲,我得到了與嘗試 1 相同的錯誤。

  2. 我已經添加了聲明並以相同的方法調用了laughtrack 它似乎會導致很多延遲,即使不是原因,它仍然會在每次調用該方法時加載它。

感謝所有為此嘗試的人!

如果您希望它在其他方法中可見,則需要在laughtrack級別(不在特定方法內)聲明笑聲 但是如果你這樣做你不能使用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();
}

您的第一次嘗試很棒,這正是您需要做的,但是var在函數之外無法使用。 只需明確寫出變量的類型!

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.

在您的情況下,標准方法是定義具有屬性和方法的結構。 但是,請考慮這些屬性是如何啟動的。 如果這是通過調用 function,那么您仍然需要調用 function,但它允許 Singleton 行為。

為了進一步闡明,考慮純函數式編程的概念,其中純 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. 但是,您不能直接訪問其成員。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM