2013-01-11 35 views
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; 
    } 
} 

}

+0

的對象,以便有什麼不工作,我唯一能看到錯誤代碼明智的應該是供應商,而不是廠商。 –

+0

你的課是正確的,其餘的我們不知道:) – spajce

+0

「供應商」是一個錯字... intellisense會固定那個。它位於與調用它的類相同的同一級別的命名空間中,但位於由「程序」調用的程序塊內。我會嘗試發佈更多。 –

回答

1

我懷疑,要麼你的類是不是真的在同一個名字,或者是在其他項目,您的主項目沒有提及該項目。

如果供應商類別是真的在同一個名字作爲你的主入口點程序,所有你需要做的有剛:

Vendor v = new Vendor(VendorID); 
1

C#是區分大小寫的。如果你的類是Vendor(大寫V),那麼你可以創建

int id = ...; // your vendor Id goes here 
Vendor v = new Vendor(id); 
+0

我是個白癡。我依靠intellisense來處理大寫字母的問題,但之後在糾正先前的錯誤之後沒有重新輸入名稱。當然,問題是非大寫的「供應商」類型。我爲我的密度道歉。我會在年齡上責怪它,雖然認識我的人會說它只會隨着年齡變得更糟......感謝所有人! –