2016-04-15 9 views
-1

私の公的な静的な文字列でエラーが返されるValidateCreditScore(int creditscore、string status) "status"という名前のローカルまたはパラメータはこのスコープで宣言できません。囲むローカルスコープでローカルまたはパラメータを定義するために使用されます。別のクラスのifステートメントから文字列を返す

基本的に私はそのステートメントを返すので、「{0}は{1}、applicantName、applicant.statusです。」という私のプレースホルダーに置くことができます 私はステートメントに名前を言って、

私の公共の静的な文字列でも、私はすでにそれに信用スコアを渡しているからValidateCreditScore(int creditscore、string status)の文字列ステータスが必要ですか?私は何かを返すので、それが必要です...

using System; 

public class MortgageApplication 
{ 
public static void Main(string[] args) 
{ 
    int creditScore = 0; 
    string applicantName = ""; 
    char userInput = 'n'; 
    string status = ""; 

    Console.WriteLine("** CSCC Mortgage Company **"); 
    Console.Write("\nWould you like to run this program [y, n]? "); 
    userInput = Convert.ToChar(Console.ReadLine()); 

    while (!(userInput == 'n')) 
    { 
     Console.Write("Please enter the applicant's name: "); 
     applicantName = Console.ReadLine(); 
     Console.Write("Enter credit score: "); 
     creditScore = Convert.ToInt32(Console.ReadLine()); 

     try 
     { 
      Applicant applicant = new Applicant(applicantName, creditScore, status); 
      Console.WriteLine("{0} is {1}", applicantName,applicant.status); 
     } 

     catch (ArgumentException anyException) 
     { 
      Console.WriteLine(anyException.Message); 
     } 

     Console.Write("Would you like to run this program [y, n]? "); 
     userInput = Convert.ToChar(Console.ReadLine()); 

    } 

    Console.WriteLine("Please press the <enter> key to terminate the program."); 
    Console.ReadLine(); 
} 
} 

public class Applicant 
{ 
public Applicant(string name, int creditscore, string status) 
{ 
    Name = name; 
    CreditScore = creditscore; 
    status = ValidateCreditScore(creditscore, status); 
} 

public string Name { get; private set; } 
public double CreditScore { get; private set; } 
public string status { get; private set; } 

public static string ValidateCreditScore(int creditscore, string status) 
{ 
    try 
    { 
     if (creditscore <= 299 && creditscore >= 851) 
     { 

      throw new ArgumentException("Value does not fall within the expected range."); 

     } 

     if (creditscore <= 649) 
     { 
      string status = Convert.ToString("not accepted"); 
      return status; 
     } 

     if (creditscore >=650) 
     { 
      string status = Convert.ToString("accepted"); 
      return status; 
     } 

    } 

    catch (ArgumentException anyException) 
    { 
     Console.WriteLine(anyException.Message); 

    } 
    return "Not Accepted"; 
} 
} 

答えて

0

ValidateCreditScoreに渡さは、「ステータス」

public static string ValidateCreditScore(int creditscore, string status) 

命名されたが、その後、あなたは、後で同じ名前の別の変数を作成しよう:

if (creditscore <= 649) 
{ 
    string status = Convert.ToString("not accepted"); 
    return status; 
} 

は何か他のものへの変数の一つの名前を変更し、それが動作するはずです。

また、Convert.ToString()は不要で、返す前に文字列を変数に格納する必要はありません。したがって、上記の行は

if(creditscore <= 649) 
{ 
    return "not accepted"; 
} 

は最後に、それはあなたがそれを取り除くことができるように、でも機能で使用されていないValidateCreditScoreに渡される状態のように見えになります。

+0

私はあなたのお勧めを行いました。コンパイルしている間に、名前が入ってから空白になってしまいました。それがなぜ起こるのか理解できますか? – mvanderk10

+0

同じ名前付き変数の問題があるためです。 'Applicant'コンストラクタはステータス文字列を渡しますが、statusというクラスレベルの文字列プロパティも持っています。コンストラクタに設定されているコンストラクタは、コンストラクタに渡されるもので、コンストラクタが戻るときに範囲外になります。 'Applicant'コンストラクタからstatusパラメータを削除すると、正しいものが設定されます。 一方、私はスコープを検討することを提案します。あなたはおそらくグーグルで "C#variable scope"という良い記事を見つけることができます – spectacularbob

0

statusという名前の変数をメソッドに渡していますが、同じ名前の変数も宣言しようとしているため、このエラーが発生しています。あなたも、あなたがそうのようにコードを書き換えることができ、この方法で変数を宣言する必要はありません。

if (creditscore <= 649) 
    { 
     return "not accepted"; 
    } 

    if (creditscore >=650) 
    { 
     return "accepted"; 
    } 
0

あなたはあなたの方法ValidateCreditScoreのステータスは必要ありません。だから、この方法では、そのパラメータを削除し、ビルドエラーを修正...

return Convert.ToString("accepted"); 

または

return Convert.ToString("not accepted"); 

を: は、同じように変数を使用せずに文字列を返します。

関連する問題