如何从其他类访问数组 [英] How do I access an array from other class

查看:83
本文介绍了如何从其他类访问数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

namespace TrainStation
{
    class Station
    {
        string[] redLine = new string[] { "Yew Tee", "Kranji", "Marsiling","Woodlands", "Admiralty", "Sembawang", "Yishun", "Kathib", "Yio Chu Kang", "Ang Mo Kio", "Bishan", "Braddel"
        ,"Toa Payoh","Novena","Newton","Orchard","Somerset","Dhoby Ghaut","Raffles Place","Marina Bay","Marina South Pier"};

        string[] greenLine = new string[] { "Tuas Link"
        ,"Tuas West Road","Tuas Crescent","Gul Circle","Joo Koon","Pioneer","Boon Lay","Lakeside","Chinese Garden","Clementi","Dover","Buona Vista","CommonWealth","Queenstown","Redhill","Tiong Bahru","Outram Park","Tanjong Pagar",
        "Raffles Place","Bugis","Lavender","Kallang","Aljunied","Paya Lebar","Eunos","Kembangan","Bedok","Tanah Merah","Simei","Tampines","Pasir Ris"};

        string[] terminalStations = new string[] { "Jurong East", "City Hall" };

        string[] trainColor = new string[59];


        
    }
}







我想从main方法访问数组。



我尝试过:



我搜索过google并跟着它直到,






I want to access the arrays from the main method.

What I have tried:

I have searched google and followed it till,

Station newClassObj = new Station();
string[] newStation = newClassObj.redLine();





但它给了我一个错误非可调用成员'Station.redLine'不能像方法一样使用。



But it gives me an error "Non-invocable member 'Station.redLine' cannot be used like a method."

推荐答案

你试图用方法签名来引用它,只需删除括号:

You are trying to refer to it with a method signature, just remove the parentheses thus:
string[] newStation = newClassObj.redLine;



尽管添加方法可能更好将条目作为某种形式的列表返回。使用一个只是为了容纳几个数组的类并不是必需的。


Although it might be better to add a method to return the entries as some form of list. Using a class just to hold a few arrays is not really necessary.


这篇关于如何从其他类访问数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆