简体   繁体   English

如何让C#类在其构造函数中实例化另一个类?

[英]How can I make a C# class instantiate another class in its constructor?

I created the following: 我创建了以下内容:

public class HttpStatusErrors
{
    public HttpStatusErrors()
    {
        this.Details = new List<HttpStatusErrorDetails>();
    }
    public string Header { set; get; }
    public IList<HttpStatusErrorDetails> Details { set; get; }
}

public class HttpStatusErrorDetails
{
    public HttpStatusErrorDetails()
    {
        this.Errors = new List<string>();
    }
    public string Error { set; get; }
    public IList<string> Errors { set; get; }
}

In my code I am using it like this: 在我的代码中我使用它像这样:

var msg = new HttpStatusErrors();
   msg.Header  = "Validation Error";           
   foreach (var eve in ex.EntityValidationErrors) {

      msg.Details. // Valid so far 
      msg.Details.Error // Gives the error below:

The Ide recognizes msg.Details as being valid but when I try to write the second line I get: Ide将msg.Details视为有效,但当我尝试编写第二行时,我得到:

Error   3   'System.Collections.Generic.IList<TestDb.Models.Http.HttpStatusErrorDetails>' 
does not contain a definition for 'Error' and no extension method 'Error' accepting a first 
argument of type 'System.Collections.Generic.IList<TestDb.Models.Http.HttpStatusErrorDetails>' 
could be found (are you missing a using directive or an assembly reference?)    
C:\K\ST136 Aug 16\WebUx\Controllers\ProblemController.cs    121 33  WebUx

Is there something I am doing wrong? 有什么我做错了吗? I thought the way I had this set up that new Lists would be created when the first class was created. 我认为我设置的方式是在创建第一个类时创建新的列表。

msg.Details returns a List object. msg.Details返回一个List对象。 List<T> does not have an Errors property. List<T>没有Errors属性。 You need to access a specific element in your list, and only then will you have your Errors property. 您需要访问列表中的特定元素,然后才会拥有Errors属性。

For example: 例如:

msg.Details[0].Error

In your code you might want to make sure that msg.Details contains elements before trying to access them, or better yet iterate over them in a foreach loop. 在您的代码中,您可能希望确保msg.Details在尝试访问它们之前包含元素,或者更好地在foreach循环中迭代它们。

Details is a collection. 细节是一个集合。 The property Error belongs to an item in the collection Details. 属性Error属于集合Details中的项目。 There is no property Error on that collection Details 该集合详细信息没有属性Error

you are tying to go to Details which is a IList<HttpStatusErrorDetails> . 你想要去一个IList<HttpStatusErrorDetails> Details

if you want to go through the items in that list u need go over them for example 如果你想要查看该列表中的项目,你需要仔细阅读它们

msg.Details[number].Errors

or 要么

foreach(HttpStatusErrorDetails err in msg.Details)
{
    err.Errors
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以从C#中另一个类的构造函数调用构造函数吗? - Can I call a constructor from the constructor of another class in C#? C#:如何实例化私有内部类的列表? - C#: How can I instantiate a List of private inner class? 如何使用简写C#方式实例化一个类? - How can I instantiate a class using the shorthand C# way? 如何在另一个 class 方法中使用 class 和构造函数并将它们存储到我的列表中? C# - How can I use a class and constructor in a another class, method and store them to my list? C# C#如何为类构造函数 - C# how to make constructor for class 如何使用构造函数创建C#类并使其返回字符串? - How can I make a C# class with a constructor and have it return a string? 我可以在没有构造函数或容器的情况下实例化类吗? - Can I instantiate a class without a constructor or container? C# 如何在字典中存储 class 构造函数 - C# How can I store class constructor in dictionary 如何在没有构造函数的情况下创建类的对象,C# - How can I create an object of a class with no constructor, C# 有一个带有C ++类的DLL,构造函数可以在其他C ++类中使用,如何让C#能够创建它的实例? - Having a DLL with C++ class thats constructor takes in other C++ class, how to make C# capable of creating its instance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM