2013-03-25 31 views
-2

我想只是將幾個方法的返回變量拉出來,而這些方法的設置完全類似於另一種方法。我想以一種對計算機有效的方式來執行此操作,因爲我正在編寫控制檯應用程序,因爲這是我目前知道如何使用的主要內容。原因是使用SQL連接到數據庫,但是我的連接將在連接測試中進行,然後連接測試將在主要方法中保持連接,所以我需要傳遞變量的幫助。這是相關的代碼。如何將一個變量從一個方法拉到主方法上?

//Create a method to get database name 
public static string databname() 
{ 
    Console.WriteLine("Enter the database name.\n"); 
    string dbasename = Console.ReadLine(); 
    Console.WriteLine(); 
    return dbasename; 
} 

//Create a method to get database password 
public static string databpass() 
{ 
    Console.WriteLine("Enter database password.\n"); 
    string dbasepass = Console.ReadLine(); 
    Console.WriteLine(); 
    return dbasepass; 
} 

//Create a method to get username 
public static string usernames() 
{ 
    Console.WriteLine("Enter access username.\n"); 
    string username = Console.ReadLine(); 
    Console.WriteLine(); 
    return username; 
} 

//Create a method to get user's password 
public static string pass() 
{ 
    Console.WriteLine("Enter access password.\n"); 
    string password = Console.ReadLine(); 
    Console.WriteLine(); 
    return password; 
} 

我想嘗試和傳遞變量上述方法在下面的地方,因爲我不知道如何在C#做。我已經嘗試過,並查閱了教程和代碼片段,到目前爲止,還沒有人爲我工作。

//Try to run program 
try 
{ 
    //Create display for user to enter info through the methods 
    string databaseName = databname(); 
    string databasePass = databpass(); 
    string username = usernames(); 
    string password = pass(); 
+0

您已經閱讀'通()的返回值'進入主程序?你還想做什麼? – Andomar 2013-03-25 21:30:04

+0

你想做什麼?將變量'databaseName','databasePass'等傳遞給另一個方法或類?對我來說很難理解問題是什麼。 – bas 2013-03-25 21:30:07

+0

對不起,不明白什麼阻止了你,你已經從控制檯輸入中得到了你需要的東西,你想根據你得到的內容創建一個連接字符串嗎? – ljh 2013-03-25 21:32:09

回答

0

下面是如何在的Main()使用out參數

示例代碼

{ 
... 
string name,pass; 
GetInputData(out name, out pass); 
... 

} 

獲取所有在一個方法:

public static void GetInputData(out string pass, out string name) 
{ 
Console.WriteLine("Enter name:"); 
name = Console.ReadLine(); 
Console.WriteLine("Enter pass:"); 
pass = Console.ReadLine(); 
} 
+0

謝謝!這與Java非常不同。 – 2013-03-25 21:53:00

+0

它說我必須在我的方法有回報,當我這樣做時,我嘗試像這樣返回: 返回名稱,通過; 它說它期望一個; 我試過像這樣: return name; 回傳; 它說最後一次退貨時無法訪問的代碼 – 2013-03-25 22:16:36

+0

你讀過關於C#的任何書嗎?我建議你閱讀,你不知道語法基礎知識。當然,你不能像你在評論中顯示的那樣做。用有問題的部分代碼編輯您的問題。 – 2013-03-26 11:41:57

2

你有有幾個選項可用於返回多個數據從一個方法,這是我認爲你應該考慮的選項的排序。

做一個複雜的返回類型

可以使一個複雜類型,表示你想,然後返回該類型的實例中的數據。在我看來,這是典型的拍攝模式。例如:

public class SomeType 
{ 
    public string Password { get; set; } 
    public string SomeOtherValue { get; set; } 
} 

而且你的方法,像這樣:

public SomeType pass() 
{ 
    SomeType instance = new SomeType(); 
    instance.Password = // get your password 
    instance.SomeOtherValue = // get another value 

    return instance; 
} 

設置全局/實例變量

內部到你的對象,你可以設置共享代碼中的變量,可以是隻要您處於相同的範圍級別即可閱讀。例如:

public class Sample 
{ 
    public static string _sharedVariable = string.Empty; 

    public static void DoSomething() 
    { 
     string result = DoSomethingElse(); 
     // Can access _sharedVariable here 
    } 

    protected static string DoSomething() 
    { 
     _sharedVariable = "hello world"; 
     return "sup"; 
    } 
} 

請參考輸入/輸出參數

您可以返回一個數據類型,並指定參數是被指定要返回的參數out參數/通過該方法改變。此方法實際上只應用於特定實例,方法的返回類型需要是特定類型,或者您試圖強制執行某種API約束。例如:

int outputGuy = 0; 

string password = pass(out outputGuy); 

而且你的方法看起來像這樣:

public string pass(out string outputGuy) 
{ 
    outputGuy = "some string"; // compiler error if you dont set this guy! 
    return // some password 
} 
+0

我想要一些更容易的工作,因爲我的工作是從數據庫表中提取記錄,如果我可以使我的方法工作,我將處理這些記錄。我喜歡最後一個例子,因爲它使得它更容易。 – 2013-03-25 22:43:51

相關問題