1

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:

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;
    }
}
Oscar Mederos
  • 29,016
  • 22
  • 84
  • 124
Ace Troubleshooter
  • 1,369
  • 6
  • 29
  • 43
  • try moving using declarations to the top – Grozz Jun 07 '11 at 19:29
  • Currently you have Customer defined as an inner-class of Submission which is not what I think you want. You want customer to be a top-level class alongside Submission and than have Submission hold a reference to an instance of Customer. – ribram Jun 07 '11 at 19:29

5 Answers5

6

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:

See:

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

constructors are called whenever you create an instance (object) of a class.

mikey
  • 5,090
  • 3
  • 24
  • 27
  • you added the members as public fields, properties are probably a better choice. – kay.one Jun 07 '11 at 19:35
  • Agreed, just trying to keep it simple to start ;) Brazos - use properties. – mikey Jun 07 '11 at 19:35
  • Thanks, @Mikey. That was what I needed. So much information out there, it's hard to find the right stuff when you don't know the right questions, you know? – Ace Troubleshooter Jun 07 '11 at 19:46
  • No problem. OT. but I had a similar problem finding a "shifter ribbon" for an old car I was fixing up.. Who knew it was called that? Stack Overflow is a great resource, I am going to start Spring framework and plan to ask lots of questions here in the process! – mikey Jun 07 '11 at 19:50
  • You can automate the creation of properties with VS by creating the private variable then right-clicking on it and selecting Refactor -> Encapsulate Field. Enjoy. – mikey Jun 07 '11 at 19:52
1

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.

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; }
}
ebrown
  • 811
  • 1
  • 8
  • 13
0

Just defining the Customer Object inside Submission does not make Submission contain an instance of Customer. For what you want to do, just bring Customer outside Submission and the define a Customer property inside Submission.

Alireza
  • 5,421
  • 5
  • 34
  • 67
0

You're trying to reference the actual Type Customer through tempSubmission. You'd have to instead reference an instance of Customer which would be exposed via a property or something similar.

Chris Walsh
  • 1,863
  • 17
  • 16
0

The problem is your customer type is not specific enought, it is defined as an Object and not a Customer.

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(); basically meams, Define a Method called Customer of type object.

what you would want to do is define a property of type Customer called whatever, for example RelatedCustomer

Public Customer RelatedCustomer{get;set;}
kay.one
  • 7,622
  • 6
  • 55
  • 74