C# 如何获取存储在数组中的类中的值?

C# 如何获取存储在数组中的类中的值?,c#,arrays,class,unity3d,C#,Arrays,Class,Unity3d,我正在统一制作一个C#脚本。我的意图是创建一个类Scenario,创建表示不同场景的类,然后将这些类存储在数组scenarioListAll中 该守则的(简化)版本如下: using System.Collections; using System.Collections.Generic; using UnityEngine; public class OverallManager2 : MonoBehaviour { public static object[] scenarioListA

我正在统一制作一个C#脚本。我的意图是创建一个类
Scenario
,创建表示不同场景的类,然后将这些类存储在数组
scenarioListAll

该守则的(简化)版本如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class OverallManager2 : MonoBehaviour
{

public static object[] scenarioListAll = new object[40];

public class Scenario
{
    public string scenarioDesc;
    public bool surprise;     // The 'surprise' bool I want to reference is defined here
    public string surpriseType;
    public int[] leftOption;
    public int[] rightOption;
    public int scenarioNumber;

    public Scenario(string st, bool sp, int[] l, int[] r, int n)
    {
        scenarioDesc = st;
        surprise = sp;
        leftOption = l;
        rightOption = r;
        scenarioNumber = n;
    }

    // I haven't used this, but I'm not sure if this matters so I'm including this too
    public Scenario(string st, bool sp, string spt, int[] l, int[] r, int n)
    {
        scenarioDesc = st;
        surprise = sp;
        surpriseType = spt;
        leftOption = l;
        rightOption = r;
        scenarioNumber = n;
    }
}


public static int[] getArray(int a, int b, int c, int d, int e, int f)
{
    int[] arr = new int[6] {a, b, c, d, e, f};
    return arr;
}


// Storing scenarios, am looking for the bool (2nd position)
public Scenario s1 = new Scenario("Test1", false, getArray(1, 1, 1, 1, 1, 1), getArray(1, 1, 1, 1, 1, 1), 1);
public Scenario s2 = new Scenario("Test2", true, getArray(1, 1, 1, 1, 1, 1), getArray(1, 1, 1, 1, 1, 1), 2);
public Scenario s3 = new Scenario("Test3", false, getArray(1, 1, 1, 1, 1, 1), getArray(1, 1, 1, 1, 1, 1), 3);

    void Awake()
    {
        // Store scenarios in object array
        scenarioListAll[0] = s1;
        scenarioListAll[1] = s2;
        scenarioListAll[2] = s3;

        for(int i = 0; i < 40; i++)
        {
            object temp = scenarioListAll[i];     // Trying to extract the object stored in the array in a temp object
            bool surpriseCheck = temp.surprise;     // I am having problems with this line
            if(surpriseCheck == true)
            {
                // Do something
            }
        }
    }
// Ignoring start and update since they're irrelevant in this context
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类全面管理2:单一行为
{
公共静态对象[]scenarioListAll=新对象[40];
公共类场景
{
公共字符串场景描述;
public bool should;//这里定义了我要引用的“should”bool
公共字符串类型;
公共int[]leftOption;
公共选择权;
公共int场景编号;
公共场景(字符串st、布尔sp、int[]l、int[]r、int-n)
{
scenarioDesc=st;
惊喜=sp;
leftOption=l;
右选项=r;
场景编号=n;
}
//我没有用过这个,但我不确定这是否重要,所以我也包括了这个
公共场景(字符串st、布尔sp、字符串spt、int[]l、int[]r、int n)
{
scenarioDesc=st;
惊喜=sp;
惊喜类型=标准贯入试验;
leftOption=l;
右选项=r;
场景编号=n;
}
}
公共静态int[]getArray(int a、int b、int c、int d、int e、int f)
{
int[]arr=新的int[6]{a,b,c,d,e,f};
返回arr;
}
//存储场景,我在寻找bool(第二位)
公共场景s1=新场景(“Test1”,false,getArray(1,1,1,1,1),getArray(1,1,1,1,1,1),1);
公共场景s2=新场景(“Test2”,true,getArray(1,1,1,1,1),getArray(1,1,1,1,1,1),2);
公共场景s3=新场景(“Test3”,false,getArray(1,1,1,1,1),getArray(1,1,1,1,1,1),3);
无效唤醒()
{
//在对象数组中存储场景
scenarioListAll[0]=s1;
scenarioListAll[1]=s2;
scenarioListAll[2]=s3;
对于(int i=0;i<40;i++)
{
object temp=scenarioListAll[i];//尝试提取临时对象中存储在数组中的对象
bool shopprisecheck=temp.shopping;//我这行有问题
如果(惊奇检查==真)
{
//做点什么
}
}
}
//忽略start和update,因为它们在此上下文中不相关
}
我想做的是检查新定义场景(例如,
s1
)中的
惊喜
元素是否为真。为此,我计划提取存储在数组
scenarioListAll
中的场景,然后从中提取
惊喜
组件。但是,我不知道如何做(例如,在上面显示的代码中,它返回编译器错误CS1061)

我想我也找不到关于这方面的任何文档,但我可能不了解一些东西。我正在自学,请容忍我的知识/表达能力差


谢谢你抽出时间。非常感谢您的帮助。

您遇到了编译问题,因为c#编译器不知道temp是一个场景,因为您将它声明为“对象”。如果您想循环浏览这些场景并检查它们是否令人惊讶,您可以使用以下方法:

 foreach(Scenario temp in scenarioListAll)
 {
        bool surpriseCheck = temp.surprise;     
        if(surpriseCheck == true)
        {
            // Do something
        }
 }
通过对迭代进行更多控制来完成相同任务的另一种方法是:

 for(int i = 0; i < scenarioListAll.Length; i++)
 {
        Scenario temp = scenarioListAll[i];
        bool surpriseCheck = temp.surprise;     
        if(surpriseCheck == true)
        {
            // Do something
        }
 }
for(int i=0;i

第一个版本的好处是,您不必担心超出数组的边界。正如Mike在下面添加的那样,您也可以使用var让编译器为您填写类型。

有时允许编译器为我们确定变量的类型是最简单的

在本例中,您已经指定变量
temp
的类型为
object
。现在,这很好,但是
场景
派生自
对象
,而对象是.Net环境中的最低级别类,不是
场景

var
关键字并不意味着声明的类型是“variable”类型,相反,它只是告诉编译器根据您正在执行的操作“填充”正确的类型。因此,要在您的案例中付诸实施,您可以这样做:

    for( var i = 0; i < 40; i++ )                   // notice the use of var here as well
    {
        var scenario = scenarioListAll[i];          // renamed temp to scenario
        // var surpriseCheck = scenario .surprise;  // var used but line not required
        if( scenario.surprise )
        {
            // Do something
        }
    }

有关
is
关键字的更多信息,请检查。

对象
没有
属性。也许您应该将数组定义为
publicstaticscenario[]scenarioListAll
temp
scenariotemp=scenarioListAll[i]?为什么在这些情况下使用
object
?对不起@Rufus L,我的错。我没有意识到我能够直接在
场景中使用数组。尽管如此,问题还是解决了,感谢您的支持。
foreach
远远不是语法上的甜头。他们是两个完全不同的东西。
for
语句定义初始值设定项、条件和迭代器部分,并在条件为true时执行代码块<另一方面,code>foreach
为实现System.Collections.IEnumerable或System.Collections.Generic.IEnumerable接口的类型的实例中的每个元素执行一个代码块。@Immersive这实际上是不正确的,将抛出一个
InvalidCastException
foreach
只是
IEnumerable
处理的快捷方式。@imsmn-Hmm,看起来是这样。我不确定我是从哪里得到这个主意的/我更新了这篇文章,因为我对它的语法错误。我想我把java和.net弄糊涂了
for( var i = 0; i < scenarioListAll.Length; i++ )
{
    // First, check to see if the object in the array cell is a Scenario.
    // If the item in the cell is a Scenario, check to see if surprise is set.
    if ( scenarioListAll[i] is Scenario scenario && scenario.surprise )
    {
        // Do something
    }
}