2013-03-25 57 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

相关问题