私の公的な静的な文字列でエラーが返される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";
}
}
私はあなたのお勧めを行いました。コンパイルしている間に、名前が入ってから空白になってしまいました。それがなぜ起こるのか理解できますか? – mvanderk10
同じ名前付き変数の問題があるためです。 'Applicant'コンストラクタはステータス文字列を渡しますが、statusというクラスレベルの文字列プロパティも持っています。コンストラクタに設定されているコンストラクタは、コンストラクタに渡されるもので、コンストラクタが戻るときに範囲外になります。 'Applicant'コンストラクタからstatusパラメータを削除すると、正しいものが設定されます。 一方、私はスコープを検討することを提案します。あなたはおそらくグーグルで "C#variable scope"という良い記事を見つけることができます – spectacularbob