Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 并非所有代码路径都为类中的布尔方法返回值_C#_Winforms_Visual Studio - Fatal编程技术网

C# 并非所有代码路径都为类中的布尔方法返回值

C# 并非所有代码路径都为类中的布尔方法返回值,c#,winforms,visual-studio,C#,Winforms,Visual Studio,我对编程非常陌生,正在尝试在VisualStudio中使用Windows窗体应用程序创建密码管理程序。基本上,人们可以在这个系统中添加、编辑和保存帐户 我添加了一个Account类,并在其中使用了addAccount方法。但它给了我一个错误,即不是所有代码路径都返回一个值。为什么会这样?请跟我慢慢走;我对这个很陌生 namespace thePass { class Account { int numAccount; string[] Accoun

我对编程非常陌生,正在尝试在VisualStudio中使用Windows窗体应用程序创建密码管理程序。基本上,人们可以在这个系统中添加、编辑和保存帐户

我添加了一个Account类,并在其中使用了addAccount方法。但它给了我一个错误,即不是所有代码路径都返回一个值。为什么会这样?请跟我慢慢走;我对这个很陌生

namespace thePass
{
    class Account
    {
        int numAccount;
        string[] AccountName;
        string[] Email;
        string[] Username;
        string[] Password;

        //constructors      
        public Account(string in_AccountName, string in_Email, string in_Username, string in_Password)
        {
            numAccount = 0;
            AccountName = new string[] { in_AccountName };
            Email = new string[] { in_Email };
            Username = new string[] { in_Username };
            Password = new string[] { in_Password };
                //NOT DONE KEEP GOING HERE CHECK NOTES FOR HELP maybe i leave as default?
        }

        public bool addAccount(string in_Account, string in_Email, string in_Username, string in_Password)
        {
            bool isFound = false;
            bool isAdded = false;
            for (int i = 0; i < numAccount; i++)
            {
                if (AccountName[i].CompareTo(in_Account) == 0)
                {
                    isFound = true;
                    break;
                }
                if (isFound == false)
                {
                    AccountName[numAccount] = in_Account;
                    numAccount++;
                    isAdded = true;
                }
                Email[numAccount] = in_Email;
                Username[numAccount] = in_Username;
                Password[numAccount] = in_Password;
                return isAdded;

            }
        }
namespace进程
{
类别帐户
{
整数计数;
字符串[]AccountName;
字符串[]电子邮件;
字符串[]用户名;
字符串[]密码;
//建设者
公共帐户(帐户名中的字符串、电子邮件中的字符串、用户名中的字符串、密码中的字符串)
{
numAccount=0;
AccountName=新字符串[]{in_AccountName};
Email=新字符串[]{in_Email};
用户名=新字符串[]{in_Username};
Password=新字符串[]{in_Password};
//没有完成继续在这里查看注释寻求帮助也许我会默认离开?
}
public bool addAccount(帐户中的字符串、电子邮件中的字符串、用户名中的字符串、密码中的字符串)
{
bool isFound=false;
bool isAdded=false;
对于(int i=0;i
addAccount
需要返回一个
bool
,但问题是您的循环可能永远不会执行。您的循环条件是
for(int i=0;i
;如果
numAccount
-1
开头会发生什么

您需要做的是将
isAdded
返回到循环外部。只需移动此行:

return isAdded;

到循环的结束括号下方。

addAccount
需要返回一个
bool
,但问题是您的循环可能永远不会执行。您的循环条件是
for(int i=0;i
;如果
numAccount
-1
开头会发生什么

您需要做的是将
isAdded
返回到循环外部。只需移动此行:

return isAdded;

到循环的结束括号下方。

您的返回在
for
结构中,根据条件,可能无法访问该结构。您可以通过执行以下操作来修复此问题:

    public bool addAccount(string in_Account, string in_Email, string in_Username, string in_Password)
    {
        bool isFound = false;
        bool isAdded = false;
        for (int i = 0; i < numAccount; i++)
        {
            if (AccountName[i].CompareTo(in_Account) == 0)
            {
                isFound = true;
                break;
            }
            if (isFound == false)
            {
                AccountName[numAccount] = in_Account;
                numAccount++;
                isAdded = true;
            }
            Email[numAccount] = in_Email;
            Username[numAccount] = in_Username;
            Password[numAccount] = in_Password;


        }
        return isAdded;
    }
public bool addAccount(帐户中的字符串、电子邮件中的字符串、用户名中的字符串、密码中的字符串)
{
bool isFound=false;
bool isAdded=false;
对于(int i=0;i

要认识到的重要一点是(只是添加这个,因为您声明自己是编程初学者),因为您确信这样一个条件(给定您的逻辑)不会发生,编译器无法知道这一点。简单地说,编译器的任务是强制执行,无论数据中出现什么异常,您的方法都会以某种方式返回一个
bool
值,无论执行路径如何。

您的返回在
for
结构中,该结构取决于根据条件,可能无法访问它。您可以通过执行以下操作来解决此问题:

    public bool addAccount(string in_Account, string in_Email, string in_Username, string in_Password)
    {
        bool isFound = false;
        bool isAdded = false;
        for (int i = 0; i < numAccount; i++)
        {
            if (AccountName[i].CompareTo(in_Account) == 0)
            {
                isFound = true;
                break;
            }
            if (isFound == false)
            {
                AccountName[numAccount] = in_Account;
                numAccount++;
                isAdded = true;
            }
            Email[numAccount] = in_Email;
            Username[numAccount] = in_Username;
            Password[numAccount] = in_Password;


        }
        return isAdded;
    }
public bool addAccount(帐户中的字符串、电子邮件中的字符串、用户名中的字符串、密码中的字符串)
{
bool isFound=false;
bool isAdded=false;
对于(int i=0;i
要认识到的重要一点是(只是添加这个,因为您声明自己是编程初学者),因为您确信这样一个条件(给定您的逻辑)永远不会发生,编译器无法知道这一点。简单地说,编译器的任务是强制执行,无论数据中出现什么异常,您的方法都会以某种方式返回一个
bool
值,无论执行路径是什么。

public bool addAccount(string in_Account, string in_Email, string in_Username, string in_Password)
{
    ...
    for (int i = 0; i < numAccount; i++)
    {
        ...
        return isAdded;
    }
}
public bool addAccount(帐户中的字符串、电子邮件中的字符串、用户名中的字符串、密码中的字符串)
{
...
对于(int i=0;i
在循环内返回值,但在循环外不返回值,这是错误的

在代码中将}置于低位,然后在循环中返回land。 我认为应该是:

public bool addAccount(string in_Account, string in_Email, string in_Username, string in_Password)
{
    ...
    for (int i = 0; i < numAccount; i++)
    {
        ...
    }

    return isAdded;
}
public bool addAccount(账户中的字符串、电子邮件中的字符串、用户名中的字符串、st