0

我有一个应用程序需要为输入源的每一行提取有关各种实体(供应商、工人等)的一些信息。我以为这是 a 的定义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;
    }
}

}

4

2 回答 2

1

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

如果 Vendor 类确实与程序的主入口点在同一个命名空间中,那么您所要做的只是:

Vendor v = new Vendor(VendorID);
于 2013-01-11T22:15:24.103 回答
1

C# 区分大小写。如果您的班级是Vendor(大写 V),那么您可以创建一个对象

int id = ...; // your vendor Id goes here
Vendor v = new Vendor(id);
于 2013-01-11T22:39:45.777 回答