如何使用课程

时间:2013-01-11 22:03:12

标签: class c#-4.0

我有一个应用需要为输入源的每一行提取有关各种实体(供应商,工作人员等)的一些信息。我认为这是class的定义,但我现在正在努力。

class Vendor
{
    public int ID { get; set; }
    public string Name { get; set; }

    public Vendor(int vendorID)
    {
        using (CPASEntities ctx = new CPASEntities())
        {

            tblVendor v = (from vndr in ctx.tblVendors
                           where vndr.ID == vendorID
                           select vndr).FirstOrDefault();
            if (v == null)
            {
                ID = 0;
                Name = null;
            }
            else
            {
                ID = v.ID;
                Name = v.Vendor_Name;
            }
        }
    }

它位于主程序的相同名称空间中。现在我想实例化该类并将其提供给供应商ID,这样它就会使我可以使用这两个属性(黑盒子,等等......)

所以,我试过了:

vendor v= vendor(VendorID);

然后,我试过了:

vendor v = new vendor(VendorID);

我甚至将其作为静态类并尝试了上述两种方法。我确实在考虑这个问题。

有人能指出我正确的方向吗?


代码(早期阶段......):

using CPAS_EM;
using Excel = Microsoft.Office.Interop.Excel;
using SpreadsheetGear;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ProcTimesheets
{    
    public static class Vendor

{
    public int ID { get; set; }
    public string Name { get; set; }

    public Vendor(int vendorID)
    {
        using (CPASEntities ctx = new CPASEntities())
        {

            tblVendor v = (from vndr in ctx.tblVendors
                           where vndr.ID == vendorID
                           select vndr).FirstOrDefault();
            if (v == null)
            {
                ID = 0;
                Name = null;
            }
            else
            {
                ID = v.ID;
                Name = v.Vendor_Name;
            }
        }
    }
}
public static class Worker
{....}
static class LaborRate
{....}
class Program
{
    static void Main(string[] args)
    {
        XmlNodeList timesheets = TSList();
        foreach (XmlNode doc in timesheets)
        {
            string link = @"http://moss.mava.xxx.com/" + doc.Attributes["ows_FileRef"].InnerText.Split('#')[1];             // Leaves out the "//" at the beginning
            Timesheet tsheet = new Timesheet(link);
            tsheet.TimesheetID = Convert.ToInt32(doc.Attributes["ows_ID"].InnerText);
            tsheet.WeekStartDate = Convert.ToDateTime(doc.Attributes["ows_WeekStart"].InnerText);
            tsheet.CurrentStatus = doc.Attributes["ows_TimesheetStatus"].InnerText;
            tsheet.ApproverName = doc.Attributes["ows_Approver"].InnerText;
            tsheet.VendorFullName = doc.Attributes["ows_Vendor"].InnerText.Split('#')[1];               // strips some crap at the beginnining
            tsheet.Creator = doc.Attributes["ows_Author"].InnerText.Split('(')[1].Split(')')[0];         // Strips out only the NT user name
            tsheet.CreateDate = doc.Attributes["ows_Created"].InnerText;                                 // Leaves it in string format...never used as a date
            tsheet.Modifier = doc.Attributes["ows_Editor"].InnerText.Split('(')[1].Split(')')[0];       // Strips out only the NT user name
            tsheet.ModDate = doc.Attributes["ows_Modified"].InnerText.Split('(')[1].Split(')')[0];      // Leaves it in string format...never used as a date
            tsheet.OverrideStatus = doc.Attributes["ows_OverrideStatus"].InnerText;
        }
    }
    static XmlNodeList TSList()
    {
        //  This function returns an XML list of all Documents in the library with a status that needs to be audited.
        //  It uses the URL and Library Name found in the project property settings             
       ....
    }
}
public class Timesheet
{
    private enum NotifyType
    {
        General,
        Approver,
        OverrideApprover,
        BadWO
    }

    private Excel.Worksheet xlSH;                               // Local variables
    private Excel.Range xlRange;                                //
    private Excel.Application xlApp;                            //  
    private Excel.Workbooks xlWBS;                              //
    private Excel.Workbook xlWB;                                //

    public int RowCount { get; set; }                           // Row count in the "Used Range" (including the header row)
    public int ColCount { get; set; }                           // Column count in the "UsedRange"
    public int TimesheetID { get; set; }                        // Set from the SP Document Property
    public int VendorID                                         // "Read-only" property derived from the VendorFullName Document Property
    {
        get
        {
            return Convert.ToInt32(VendorFullName.Split('(')[1].Split(')')[0]); // Retrieves the parenthesized Vendor ID
        }
    }

    public DateTime WeekStartDate { get; set; }                 // Set from the SP Document Property
    public DateTime WeekEndDate                                 // "Read-only" property 
    {
        get
        {
            return WeekStartDate.AddDays(6);                    //(always returns WeekStartDate+6)
        }
    }

    public string CurrentStatus { get; set; }                   // Set from the SP Property unchanged
    public string ApproverName { get; set; }                    // Set from the SP Property unchanged
    public string ApproverEmailAddress
    {
        get
        {
            return ApproverName + "@xxx.com";
        }
    }
    public string VendorFullName { get; set; }                  // Set from the SP Property unchanged
    public string link { get; set; }                            // Derived from the SP Property
    public string Creator { get; set; }                         // NT user name of the SP Document creator
    public string CreatorEmailAddress
    {
        get
        {
            return Creator + "@xxx.com";
        }

    }
    public string CreateDate { get; set; }                      // straight from ows_Created -- left as a string
    public string Modifier { get; set; }                        // NT user name of the SP Document ows_Modifier
    public string ModDate { get; set; }                         // straight from ows_Modified -- left as a string
    public string OverrideStatus { get; set; }
    public string VendorShortName
    {
        get
        {
            return VendorFullName.Split('(')[0].Trim();        // Strips off trailing (), and trims it up
        }
    }

    List<string> WorkbookErrors = new List<string>();        

    public bool IsValid
    {
        get                                   // This property is set by "validating" the worksheet
        {
            vendor v = new Vendor(VendorID);
            bool returnvalue = true;
             // Don't forget spreadsheetgear is zero based.....
            for (int row = 0; row < RowCount; row++)
            {

            }
            return returnvalue;
        }
    }                                // "Read-only" property
    private void NotifyWorkbookError(List<string> MsgLst)
    {  ....  }
    private void Notify(NotifyType nType, List<string> MsgLst)
    {....}
    private string GetHTML(int MessageID)
    {....}
    public Timesheet(string wbPath)
    {
        link = wbPath;
        bool validDocument = OpenWorkbook();
        // Vendor curVndr = new Vendor(VendorID);
        //  Test the headings to make sure the workbook is valid
        if (validDocument)
        {

        }
        else
        {

        }

        xlWB.Close();
        xlWBS.Close();
        xlApp.Quit();


        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRange);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSH);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWB);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWBS);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
        GC.Collect();
        GC.WaitForPendingFinalizers();

    }
    bool OpenWorkbook()
    {
        xlApp = new Excel.Application();
        xlApp.Visible = false;
        xlWBS = xlApp.Workbooks;
        xlWB = xlWBS.Open(link);

        if ((xlWB.ReadOnly == true) || (ValidHeadings(xlRange) == false))
        {
            WorkbookErrors.Add("This workbook is read-only. Someone has it open for writing. No processing can be completed");
            return false;                                           // Read-only Workbooks or WBs with bad heading row are not auditable
        }
        xlSH = xlWB.Worksheets[Properties.Settings.Default.Timesheet_WorkSheetName];
        xlRange = xlSH.UsedRange;
        RowCount = xlRange.Rows.Count;
        ColCount = xlRange.Columns.Count;
        return true;
    }
    bool ValidHeadings(Excel.Range range, int headingrow = 1)
    {....}
    static bool ValidRow(Excel.Range row, int vendorID)
    {
        bool returnvalue = false;


        return returnvalue;
    }
}

}

2 个答案:

答案 0 :(得分:1)

我怀疑你的类不是真的在同一名称空间中,或者它在另一个项目中,并且你的主项目没有对该项目的引用。

如果Vendor类与您的程序的主要入口点位于同一名称空间中,那么您只需执行以下操作:

Vendor v = new Vendor(VendorID);

答案 1 :(得分:1)

C#区分大小写。如果您的班级是Vendor(使用大写字母V),那么您可以使用

创建一个对象
int id = ...; // your vendor Id goes here
Vendor v = new Vendor(id);