C# 在C中如何将值从一个类发送到另一个类?
我在void中声明了字符串snapshotJson,并且我有私有的void LoadGameData,需要调用snapshotJson的值。声明snapshotJson公共不起作用,我假设是因为void。从我读到的内容来看,我应该使用getter和setter,但我不知道它们是如何工作的,我读过的每一篇解释它的指南都让它看起来非常简单,但它们解释得如此简单,我不明白我应该如何使用它,或者在使用get/set函数后如何调用该值 有人能解释一下我如何从一个类到另一个类获取变量吗?在我的代码中,LoadGameData无法调用snapshotJson的值,我不确定我缺少了什么C# 在C中如何将值从一个类发送到另一个类?,c#,C#,我在void中声明了字符串snapshotJson,并且我有私有的void LoadGameData,需要调用snapshotJson的值。声明snapshotJson公共不起作用,我假设是因为void。从我读到的内容来看,我应该使用getter和setter,但我不知道它们是如何工作的,我读过的每一篇解释它的指南都让它看起来非常简单,但它们解释得如此简单,我不明白我应该如何使用它,或者在使用get/set函数后如何调用该值 有人能解释一下我如何从一个类到另一个类获取变量吗?在我的代码中,Loa
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.IO;
using Firebase;
using Firebase.Unity.Editor;
using Firebase.Database;
using System;
public class DataController : MonoBehaviour
{
private RoundData[] allRoundData;
private PlayerProgress playerProgress;
[Serializable]
public class FirebaseStart : MonoBehaviour
{
public string snapshotJson { get; set; }
void Start()
{
// Set up the Editor before calling into the realtime database.
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://FIREBASEDATABASE");
// Get the root reference location of the database.
DatabaseReference reference =
FirebaseDatabase.DefaultInstance.RootReference;
FirebaseDatabase.DefaultInstance
.GetReference("allRoundData")
.GetValueAsync().ContinueWith(task => {
if (task.IsFaulted)
{
// Handle the error...
}
else if (task.IsCompleted)
{
// DataSnapshot snapshot = task.Result;
snapshotJson = JsonUtility.ToJson(task.Result);
}
});
}
}
// Use this for initialization
void Start ()
{
DontDestroyOnLoad(gameObject);
LoadGameData();
LoadPlayerProgress();
SceneManager.LoadScene("MenuScreen");
}
public RoundData GetCurrentRoundData()
{
return allRoundData [0];
}
public void SubmitNewPlayerScore(int newScore)
{
if (newScore > playerProgress.highestScore)
{
playerProgress.highestScore = newScore;
SavePlayerProgress();
}
}
public int GetHighestPlayerScore()
{
return playerProgress.highestScore;
}
// Update is called once per frame
void Update () {
}
private void LoadPlayerProgress()
{
playerProgress = new PlayerProgress();
if (PlayerPrefs.HasKey("highestScore"))
{
playerProgress.highestScore = PlayerPrefs.GetInt("highestScore");
}
}
private void SavePlayerProgress()
{
PlayerPrefs.SetInt("highestScore", playerProgress.highestScore);
}
public void LoadGameData()
{
GameData loadedData = JsonUtility.FromJson<GameData>(snapshotJson);
Console.WriteLine(snapshotJson);
allRoundData = loadedData.allRoundData;
}
LoadGameData方法无法从主方法访问它,因为它是该函数作用域的本地方法。但是,您可以使用以下代码将值从Main方法传递到LoadGameData:
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
private static void LoadGameData(String input) {
// Do something inside your input here.
}
public static void Main() {
Start();
}
public static void Start()
{
string snapshotJson = "Sample data";
// Same Class Call
LoadGameData(snapshotJson);
// Other Class Call
var otherClassInstance = new TestClass();
otherClassInstance.LoadGameData(snapshotJson);
}
}
public class TestClass {
public void LoadGameData(String input) {
// Do something inside your input here.
}
}
一个温和的提醒,如果您觉得答案有帮助,请点击复选框将其标记为已接受答案。这将有助于遇到与您遇到的atm相同问题的其他人。如果变量snapshotJson是在Start方法的主体内声明的,则只能在该方法内访问。如果希望该变量可以在类的其他方法中访问,则可以将其声明为成员变量。这就是它的样子。只有在需要在类实例之外访问snapshotJson的值时,才需要将其声明为公共属性
public class MyClass
{
string snapshotJson;
private void Start()
{
// Assign value to snapshotJson here
snapshotJson = "foo";
}
private void LoadGameData
{
// Use value of snapshotJson here
string s = snapshotJson;
}
}
假设您引用的是方法而不是类,或者更确切地说:在对象之间共享数据,这就是您可以做的。下面是一个游戏对象专用类的示例,SnaphopJson是一个公共属性,可以从任何其他对象访问和更改。将setter更改为private将确保只能从该类的对象之外的任何对象读取setter
public class Game
{
public string SnapshotJson { get; set; }
private void LoadGameData()
{
// load the json, e.g. by deserialization
SnapshotJson = "{}";
}
public void Start()
{
// access the content of your snapshot
var s = SnapshotJson;
}
}
您好@JohnZ12,我可以看到您是StackOverflow的新手,我非常欢迎您!如果你觉得男生的回答很有帮助,你可以使用他们答案分数上的上下图标来表示感谢。如果任何答案满足您的问题,请随时标记他们的答案,以帮助其他人解决同一问题。干杯,谢谢你的回答。然而,在我的情况下,void Start是我分配snapshotJson值的地方,后面的代码LoadGameData是我需要检索它的地方。另外,我不明白字符串输入在这段代码中的作用是什么?我是否创建了一个伪变量,然后将其赋值为snapshotJson?@JohnZ12,我通过将main替换为start更新了答案。如果你在你的问题上发布一些源代码,那会很有帮助。谢谢你,我更新了我的帖子来展示代码。我感谢你的帮助,我似乎仍然无法让它工作,但我更新了我的代码与我迄今为止。在代码中,尽管将snapshotJson公开,但我无法从LoadGameData中检索snapshotJson的值。如果我在DataController类而不是FirebaseStart类中声明snapshotJson,那么我无法在void Start中设置值。我能够让它工作,我意识到我不必要地嵌套了这些类。我现在遇到了另一个问题,但稍后我会提出另一个问题,因为这是可行的,我的问题已经得到了回答。非常感谢你。