Class 如何使用类

Class 如何使用类,class,c#-4.0,Class,C# 4.0,我有一个应用程序,需要为输入源的每一行提取有关各种实体(供应商、工人等)的一些信息。我原以为这是类的定义,但现在我在挣扎 class Vendor { public int ID { get; set; } public string Name { get; set; } public Vendor(int vendorID) { using (CPASEntities ctx = new CPASEntities()) {

我有一个应用程序,需要为输入源的每一行提取有关各种实体(供应商、工人等)的一些信息。我原以为这是
类的定义,但现在我在挣扎

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;
            }
        }
    }
它位于主程序的同一名称空间中。现在我想实例化这个类,并为它提供一个vendor
ID
,这样它就可以为我提供这两个属性(黑盒,有点…)

所以,我试着:

vendor v=供应商(VendorID)

然后,我试着:

vendor v=新供应商(VendorID)

我甚至让它成为一个静态类,并尝试了以上两种方法。我肯定我想得不对

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


代码(早期阶段…):

使用注册会计师;
使用Excel=Microsoft.Office.Interop.Excel;
使用电子表格;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间时间表
{    
公共静态类供应商
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共供应商(int供应商ID)
{
使用(cpassentities ctx=new cpassentities())
{
tblVendor v=(来自ctx.tblVendor中的vndr
其中vndr.ID==vendorID
选择vndr.FirstOrDefault();
如果(v==null)
{
ID=0;
Name=null;
}
其他的
{
ID=v.ID;
名称=供应商名称;
}
}
}
}
公共静态类工作者
{....}
静态类劳动率
{....}
班级计划
{
静态void Main(字符串[]参数)
{
XmlNodeList时间表=TSList();
foreach(时间表中的XmlNode文档)
{
字符串链接=@“http://moss.mava.xxx.com/“+doc.Attributes[“ows_FileRef”].InnerText.Split(“#”)[1];//省略开头的“/”
时间表tsheet=新时间表(链接);
tsheet.TimesheetID=Convert.ToInt32(文档属性[“ows_ID”].InnerText);
tsheet.WeekStartDate=Convert.ToDateTime(doc.Attributes[“ows\U WeekStart”].InnerText);
tsheet.CurrentStatus=doc.Attributes[“ows_TimesheetStatus”]。InnerText;
tsheet.ApproverName=文档属性[“ows\U Approver”]。InnerText;
tsheet.VendorFullName=doc.Attributes[“ows_Vendor”].InnerText.Split(“#”)[1];//一开始就去掉一些垃圾
tsheet.Creator=doc.Attributes[“ows_Author”].InnerText.Split(“(”)[1].Split(“)”)[0];//仅删除NT用户名
tsheet.CreateDate=doc.Attributes[“ows_Created”].InnerText;//将其保留为字符串格式…从未用作日期
tsheet.Modifier=doc.Attributes[“ows_编辑器”].InnerText.Split(“(”)[1].Split(“)”)[0];//仅删除NT用户名
tsheet.ModDate=doc.Attributes[“ows_Modified”].InnerText.Split(“(”)[1].Split(“)”)[0];//将其保留为字符串格式…从未用作日期
tsheet.OverrideStatus=doc.Attributes[“ows\U OverrideStatus”]。InnerText;
}
}
静态XmlNodeList TSList()
{
//此函数用于返回库中所有文档的XML列表,其中包含需要审核的状态。
//它使用在项目属性设置中找到的URL和库名称
....
}
}
公共课时间表
{
私有枚举类型
{
将军,
批准人,
重写验证程序,
巴德沃
}
私有Excel.Worksheet xlSH;//局部变量
私有Excel.Range xlRange//
私有Excel.Application xlApp;//
私有Excel.工作簿xlWBS//
私有Excel.xlWB工作簿//
public int RowCount{get;set;}//“已使用范围”(包括标题行)中的行数
public int ColCount{get;set;}//在“UsedRange”中的列计数
public int TimesheetID{get;set;}//从SP Document属性设置
从VendorFullName文档属性派生的public int VendorID//“只读”属性
{
得到
{
返回Convert.ToInt32(VendorFullName.Split(“(”)[1].Split(“)”)[0]);//检索带括号的供应商ID
}
}
public DateTime WeekStartDate{get;set;}//从SP Document属性设置
public DateTime WeekEndDate//“只读”属性
{
得到
{
返回WeekStartDate.AddDays(6);//(始终返回WeekStartDate+6)
}
}
公共字符串CurrentStatus{get;set;}//SP属性中的set未更改
公共字符串ApproverName{get;set;}//SP属性中的set未更改
公共字符串批准者电子邮件地址
{
得到
{
返回ApproverName+“@xxx.com”;
}
}
公共字符串VendorFullName{get;set;}//SP属性中的set未更改
从SP属性派生的公共字符串链接{get;set;}//
公共字符串创建者{get;set;}//NT SP文档创建者的用户名
公共字符串CreatorEmailAddress
{
得到
{
返回创建者+“@xxx.com”;
}
}
公共字符串CreateDate{get;set;}//直接从ows_创建--左为字符串
公共字符串修饰符{get;set;}//NT SP Document ows\u修饰符的用户名
公共字符串ModDate
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;
    }
}
Vendor v = new Vendor(VendorID);
int id = ...; // your vendor Id goes here
Vendor v = new Vendor(id);