简体   繁体   English

在另一个 class C# 中创建一个 object?

[英]Create an object inside another class C#?

I'm taking my first steps into oop, which entails gutting my application and reworking it all to be 3-tiered and object oriented.我正在向 oop 迈出第一步,这需要破坏我的应用程序并将其全部重新设计为 3 层和面向 object。 Sigh .叹息 I've got a submission object, which should contain a customer object (as well as a broker and coverage object);我有一个提交 object,它应该包含一个客户 object(以及一个经纪人和覆盖对象); I want to store some datareader results from the db in the fields of each of the contained objects, but when I try to call up the Customer object with a new submission object, I get nothing.我想将数据库中的一些数据读取器结果存储在每个包含对象的字段中,但是当我尝试使用新的提交 object 调用客户 object 时,我什么也得不到。 VS doesn't recognize that Submission contains a Customer object. VS 无法识别提交包含客户 object。 I'm obviously missing some crucial points, so with that in mind, ideas?我显然错过了一些关键点,所以考虑到这一点,想法? Code below.代码如下。

//This is the Submission class here
public class Submission 
{
    public int SubmissionId {get;set;}
    public int Status { get; set; }
    public string StatusComment { get; set; }


    public class Customer
    {
        //public Customer() { }
        public int CustId { get; set; }
        public string CustName { get; set; }
        public string CustAddress { get; set; }
        public string CustState { get; set; }
        public string CustCity { get; set; }
        public int CustZip { get; set; }
        public int SicNaic { get; set; }

    }

    public object Customer();
}


//These lines throw an error:

Cannot reference a type through an expression.无法通过表达式引用类型。 VS doesn't recognize the call to the Customer object inside Submission by TempSubmission.Customer. VS 无法识别 TempSubmission.Customer 提交中对客户 object 的调用。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;


/// This query should selects the relevant data for a gridview on the presentation layer and stores in a list. 
/// Don't quite know how to bind it to the gridview yet, but that's a different question. 

public class SubmissionDatabaseService
{
    public List<Submission> GetAllSubmissions()
    {
        string Searchstring = "SELECT Submission.SubmissionId, Customer.CustName, Customer.CustCity, Customer.CustState, Broker.BroName, Broker.BroState, Broker.EntityType, Submission.Coverage, Status.Status FROM Submission INNER JOIN Broker ON Broker.BroId = Submission.BroId INNER JOIN Customer ON Customer.CustId = Submission.CustId INNER JOIN Status ON Status.StatusId = Submission.StatusId";
        string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;        
        SqlConnection conn = new SqlConnection(connectionString);

        SqlDataReader dr = null;

        try
        {
            conn.Open();

            SqlCommand Searchcmd = new SqlCommand(Searchstring, conn);

            dr = Searchcmd.ExecuteReader();
            List <Submission> lstSubmission;
            Submission tempSubmission;
            while (dr.Read())
            {
                tempSubmission = new Submission();
                tempSubmission.SubmissionId = dr.GetInt32(0);
                tempSubmission.Customer.CustName = dr.GetString(1);
                tempSubmission.Customer.CustCity = dr.GetString(2);
                tempSubmission.Customer.CustState = dr.GetString(3);
                tempSubmission.Broker.BroName = dr.GetString(4);
                tempSubmission.Broker.BroState = dr.GetString(5);
                tempSubmission.Broker.EntityType = dr.GetString(6);
                tempSubmission.SubmissionCoverage.Coverage = dr.GetInt32(7);
                tempSubmission.Status = dr.GetInt32(8);

                //Add rest of the fields
                lstSubmission.Add(tempSubmission);
            }
        }

        return lstSubmission;
    }
}

Ooo.. Where to start?哦..从哪里开始? Put your Customer class in its own file Customer.cs and your Submission class in its own file Submission.cs将您的客户 class 放在自己的文件 Customer.cs 中,将您的提交 class 放在自己的文件 Submission.cs 中

Then you can do a simple submission class like so:然后你可以像这样做一个简单的提交 class :

public class Submission 
{
        // consider implementing the below as properties to more finely control access
        public int SubmissionId;
        public int Status;
        public string StatusComment;
        public Customer SubmissionCustomer; // <-- this is null until you set it to a Customer object, either in the constructor or externally.

        public Submission() {
          // constructor
        }

}

Then read up on properties and constructors and sprinkle those in as you see fit.然后阅读属性和构造函数,并在你认为合适的时候把它们撒进去。 See:看:

http://msdn.microsoft.com/en-us/library/x9fsa0sw(v=vs.80).aspx http://msdn.microsoft.com/en-us/library/x9fsa0sw(v=vs.80).aspx

properties have a private/public pattern.属性具有私有/公共模式。 As for constructors:至于构造函数:

See:看:

http://msdn.microsoft.com/en-us/library/ace5hbzh.aspx http://msdn.microsoft.com/en-us/library/ace5hbzh.aspx

constructors are called whenever you create an instance (object) of a class.每当您创建 class 的实例(对象)时,都会调用构造函数。

I think what you want is for the Customer class to be outside of the Submission class and for the Submission class to contain an instance of Customer.我认为您想要的是客户 class 位于提交 class 之外,并且提交 class 包含客户实例。

public class Submission
{
    public Submission()
    {
        this.Customer = new Customer();
    }

    public int SubmissionId { get; set; }
    public int Status { get; set; }
    public string StatusComment { get; set; }
    public Customer Customer { get; set; }
}
public class Customer
{
    //public Customer() { }
    public int CustId { get; set; }
    public string CustName { get; set; }
    public string CustAddress { get; set; }
    public string CustState { get; set; }
    public string CustCity { get; set; }
    public int CustZip { get; set; }
    public int SicNaic { get; set; }
}

Just defining the Customer Object inside Submission does not make Submission contain an instance of Customer.仅在提交中定义客户 Object 不会使提交包含客户的实例。 For what you want to do, just bring Customer outside Submission and the define a Customer property inside Submission.对于您想要做的事情,只需将 Customer 带到 Submission 之外,然后在 Submission 中定义一个 Customer 属性。

You're trying to reference the actual Type Customer through tempSubmission .您正在尝试通过tempSubmission引用实际的Customer类型。 You'd have to instead reference an instance of Customer which would be exposed via a property or something similar.您必须改为引用Customer的实例,该实例将通过属性或类似的东西公开。

The problem is your customer type is not specific enought, it is defined as an Object and not a Customer.问题是您的客户类型不够具体,它被定义为 Object 而不是客户。

to achieve what you want to do you would have to add a new Member in this case a Property of type Customer to your Submission class, what you did with public object Customer();为了实现您想要做的事情,在这种情况下,您必须在您的Submission class 中添加一个Customer类型的新成员,您对public object Customer(); basically meams, Define a Method called Customer of type object.基本上,定义一个名为 object 类型的Customer的方法。

what you would want to do is define a property of type Customer called whatever, for example RelatedCustomer你想要做的是定义一个Customer类型的属性,称为任何东西,例如RelatedCustomer

Public Customer RelatedCustomer{get;set;}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM