2013-05-21 83 views
0

希望有人能幫助我。我有一個方法(user_login)而不是提示用戶登錄,然後生成並返回一個發票和一個標記。C#保存對象值

問題是,如果用戶已經登錄(我將它保存在列表中),則不得調用svr.logonex方法。我應該從前一個實例中檢索值。我想我必須保存svr變量,但我不知道該怎麼做。任何幫助將不勝感激。由於

下面是代碼:

public EPS30Ora.LogonResult User_Login(input_params inputparams) 
     { 
      EPS30Ora.EPS30Svr svr = new EPS30Ora.EPS30Svr(); 
     if (Users.Exists(Username => Username.Username.Equals(inputparams.Username))) 
      { 
       List<input_params> selected = Users.Where(Username => Username.Username.Equals(inputparams.Username)).ToList(); 
       svr.PrepareByMoney(selected[0].ck, 1, inputparams.meter_number, 10, 0, ref cal); 
      } 
      else 
      { 

       Log_Res = svr.LogOnEx("EDEESTE", inputparams.VSID, inputparams.Username, inputparams.Password, "EPS30Ora", ref inputparams.ck); 

       if (Log_Res == EPS30Ora.LogonResult.psOK) 
       { 
        try 
        { 
         Users.Add(inputparams); 
         object tr_bl = svr.GetInvoiceData(inputparams.ck, svr.Confirm(inputparams.ck)); 
        } 
        catch (System.Runtime.InteropServices.COMException ex) 
        { 
         Trace.WriteLine("Se ha producido una excepción: " + ex); 

        } 
       } 


      } 

      return Log_Res; 
     } 
+0

我想你應該多提供一些背景資料。 – Guanxi

+0

您可以在svr.LogOnEx方法之前進行檢查,以檢查用戶是否已經登錄(通過檢查列表中的值)。你可以有if語句,if(Users.IsLoggedOn(inputParams.UserName)){//什麼也不做} else {//svr.LogOnEx}。如果你想跟蹤svr變量,你可以創建一個全局變量,並在上述方法的每次迭代中將其設置爲等於svr變量。通過這種方式,該方法的下一次迭代,您將能夠引用前一次迭代的全局變量和當前迭代的局部變量。 –

+0

非常感謝您的回答。我會嘗試if和全局變量並評論結果。 – user2386560

回答

1

聽起來像是你需要拉這樣的:

EPS30Ora.EPS30Svr SVR =新EPS30Ora.EPS30Svr();

不在Login方法中,並將其移入任何正在調用此方法的對象中,以便將此對象保持在更長的範圍內。否則當從方法返回時,對象被銷燬。

然後,您可以把它作爲一個參數進入這個方法,例如:

// Start user process 

EPS30Ora.EPS30Svr svr = new EPS30Ora.EPS30Svr(); 

if (Users.Exists(Username => Username.Username.Equals(inputparams.Username))) { 
// do something 
} 
else { 

} 

...

public LogOn(EPS30Ora.EPS30Svr svr, input_params inputparams) 
{ 
Log_Res = svr.LogOnEx("EDEESTE", inputparams.VSID, inputparams.Username, inputparams.Password, "EPS30Ora", ref inputparams.ck); 
} 
+0

優秀,我會嘗試它,並得到結果! – user2386560

+0

我會發布完整的代碼,也許它有助於更​​好地理解我做錯了什麼。 – user2386560

+0

我將EPS30Ora.EPS30Svr svr =新EPS30Ora.EPS30Svr()從LogonEx方法中取出並運行良好。這個問題解決了。非常感謝: – user2386560

0
namespace EPSInvoice_ServiceLibrary 
{ 

    public class EPS_Service : IEPS_Service 
    { 
     List<input_params> Users = new List<input_params>(); 
     List<token_data> token_data = new List<token_data>(); 
     public decimal cal; 


     public EPS30Ora.LogonResult Invoice(input_params inputparams) 
     { 
      EPS30Ora.EPS30Svr svr = new EPS30Ora.EPS30Svr(); 
      EPS30Ora.LogonResult Log_Res = new EPS30Ora.LogonResult(); 
      Trace.Listeners.Add(new TextWriterTraceListener(Console.Out)); 
      Trace.AutoFlush = true; 
      Trace.Indent(); 

      if (Users.Exists(Username => Username.Username.Equals(inputparams.Username))) 
      { 
       List<input_params> selected = Users.Where(Username => Username.Username.Equals(inputparams.Username)).ToList(); 
       Trace.WriteLine("Username: " + inputparams.Username + " ya está logeado " + selected[0].ck); 
       // svr.PrepareByMoney(selected[0].ck, 1, inputparams.meter_number, 10, 0, ref cal); 
      } 
      else 
      { 

       try 
       { 
        Log_Res = svr.LogOnEx("EDEESTE", inputparams.VSID, inputparams.Username, inputparams.Password, "EPS30Ora", ref inputparams.ck); 
        if (Log_Res == EPS30Ora.LogonResult.psOK) 
        { 
         Users.Add(inputparams); 
         Trace.WriteLine("Login OK. Username: " + inputparams.Username); 
         svr.PrepareByMoney(inputparams.ck, 1, inputparams.meter_number, 10, 0, ref cal); 
         object tr_bl = svr.GetInvoiceData(inputparams.ck, svr.Confirm(inputparams.ck)); 
         token_data.Add(new token_data() { transfer_number = (string)((object[])(((object[])(((object[])(((object[])(tr_bl))[4]))[1]))[0]))[0] }); 
         Trace.WriteLine("Transacción Finalizada. NTC obtenido: " + inputparams.ck); 
         Trace.Unindent(); 
        } 
       } 

       catch (System.Runtime.InteropServices.COMException ex) 
       { 
        Trace.WriteLine("Se ha producido una excepción: " + ex); 
       } 
      } 
     return Log_Res; 
     } 




    } 



}