【问题标题】:How to read properties of object stored in a class library [duplicate]如何读取存储在类库中的对象的属性[重复]
【发布时间】:2018-01-12 14:31:03
【问题描述】:

我是编程新手,经常遇到这个问题:

我使用 Visual Studios 2015 创建了一个 Windows 应用程序,并向其中添加了一个类库来存储这些类。该库在引用中链接到应用程序,并且屏幕顶部的 using:namespace 命令已写入,但我似乎无法使用类中对象的存储数据。

班级

public class Area
{
    //field -> properties of the object
    public int AreaID { get; set; }
    public string AreaTitle { get; set; }
    public string AreaDescription { get; set; }


    // constructor -> creates objects
    public Area (int id, string title, string description)
    {
        this.AreaID = id;
        this.AreaTitle = title;
        this.AreaDescription = description;
    }

    //method that uses constructor to create ('instantiate') objects
    public static void CreateArea()
    {
        Area home = new Area(1, "Home", "This is your home");
        Area area2 = new Area(2, "Field", "Youre at a field");
        Area area3 = new Area(3, "Mine", "Youre in a mine");
        Area area4 = new Area(4, "Market", "Youre at a market");
    }
}

用户界面代码

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        Area.CreateArea(); //to make shure the 4 objects exist
    }
    // just to test if button works, and when program stops working 
    public int counter = 1;

    public void btnImput_Click(object sender, EventArgs e)
    {
        lblTitle.Text = $"counter is {counter}";
        counter++;

    }

    public void btnCreate_Click(object sender, EventArgs e)
    {
        ReportAreaDiscription(home); //doesnt recognise home
        lblTitle.Text = home.AreaDescripting;  //doesnt work either

    }

    //im trying a method cosue just typing the text didnt work either
    public void ReportAreaDiscription(Area areadiscription)
    {
        lblDescription.Text = $"{areadiscription.AreaDescription}";
    }
}

/* 好的,这就是问题所在。为什么程序不能识别第 30 行的 home.AreaDescription? * 错误信息是 -> 当前上下文中不存在名称“home”

【问题讨论】:

  • 因为“home”是一个既没有声明也没有实例化的变量。我希望您想在表单中创建一个类型为 Area 的变量。您只是在 Area 上调用静态方法。就是这样。
  • home 被用作CreateArea 范围内的局部变量,并且在它之外没有范围。您需要在 btnCreate_Click 的范围内声明 home 才能使其正常工作。你还有 home.AreaDescripting 拼写错误
  • CreateArea 不应该是 Area 类的成员。如果您需要 Area 的实例,那么引用它们的变量应该在正确的范围内。在何处、何时以及由谁实例化它们取决于...
  • 新用户,请客气,至少问题已经形成了

标签: c#


【解决方案1】:

由于您是编程新手,为了找到一个简单的解决方案来解决您的问题,您应该查看 c# 中的访问修饰符。

https://docs.microsoft.com/fr-fr/dotnet/csharp/language-reference/keywords/access-modifiers

为了解释你的情况,你的变量

    Area home = new Area(1, "Home", "This is your home");
    Area area2 = new Area(2, "Field", "Youre at a field");
    Area area3 = new Area(3, "Mine", "Youre in a mine");
    Area area4 = new Area(4, "Market", "Youre at a market");

仅在您的 CreateArea 方法中可见。

为了从引用您的库的另一个类或另一个应用程序中访问变量或属性,您应该声明公共属性,如您的

public int AreaID { get; set; }
public string AreaTitle { get; set; }
public string AreaDescription { get; set; }

变量。

为了从引用库的应用程序访问库类的变量或属性,您应该使用公共访问修饰符。 根据您的目标,您应该使用内部、受保护或私有访问修饰符。

如果您查看响应顶部的链接,您会发现详细信息。

【讨论】:

  • 那些不是“全局变量”!它们是完全不同的公共属性!
  • 我更改了回复。你是对的。
猜你喜欢
  • 2019-11-13
  • 1970-01-01
  • 1970-01-01
  • 2019-10-17
  • 1970-01-01
  • 2020-01-30
  • 2015-04-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多