在另一个C#类中创建对象? [英] Create an object inside another class C#?

查看:546
本文介绍了在另一个C#类中创建对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在迈向oop,这需要使我的应用程序胆怯并将其全部重新构建为3层且面向对象。 叹气。我有一个提交对象,其中应该包含一个客户对象(以及经纪人和承保范围对象);我想在每个包含的对象的字段中存储来自db的一些数据读取器结果,但是当我尝试使用新的提交对象调用Customer对象时,我什么也没得到。 VS无法识别提交中包含客户对象。我显然遗漏了一些关键点,因此有想法吗?下面的代码。

I'm taking my first steps into oop, which entails gutting my application and reworking it all to be 3-tiered and object oriented. Sigh. I've got a submission object, which should contain a customer object (as well as a broker and coverage 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. VS doesn't recognize that Submission contains a Customer 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:




无法通过表达式引用类型。 VS无法识别TempSubmission.Customer在Submission内部对Customer对象的调用。

Cannot reference a type through an expression. VS doesn't recognize the call to the Customer object inside Submission by TempSubmission.Customer.



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 .. 从哪儿开始?将您的Customer类放入其自己的文件Customer.cs中,并将您的Submission类放入其自己的文件Submission.cs中。

Ooo.. Where to start? Put your Customer class in its own file Customer.cs and your Submission class in its own file Submission.cs

然后,您可以执行一个简单的提交类,如下所示:

Then you can do a simple submission class like so:

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

属性具有私有/公共模式。至于构造函数:

properties have a private/public pattern. As for constructors:

请参阅:

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

构造函数。

这篇关于在另一个C#类中创建对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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