2017-10-18 6 views
-2

のメソッド内でプロパティを使用する方法。私はインターネット上で検索していましたが、値が返されるメソッドの内部でプロパティが使用されていることがわかりません。正しい方法でメソッド内でプロパティを使用する方法のC#

public class OET 
{ 


    public int ShiftTime { get; set; } 
    public int BreakTime { get; set; } 
    public int DownTime { get; set; } 
    public int ProductionTarget { get; set; } 

    public int IdealRunRate { get; set; } 
    public int PrductionOneShift { get; set; } 
    public int RejectedProduct { get; set; } 

    public int planedProductionTime(int shift, int breaktime) { 

     shift = ShiftTime; 
     breaktime = BreakTime; 

     return shift - breaktime; 

    } 

私は「PlanedProductionTIme」メソッドから値を取得するプロパティを使用したいと思い、それが正しい上記のコードですか?次の2つのパラメータを渡したが、その後、あなたの計算でそれらを無視しているので

+0

[Msdn](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-properties)。 – Sinatr

+0

「return this.ShiftTime - this.BreakTime;」だけを使用してください。あなたのパラメータ(シフト、ブレークタイム)の必要はありません。 – Flocke

+0

"sift"と "breaktime"というローカル変数を使用することは関数ではありません。 –

答えて

1

あなたの例では、非常に明確ではありません。プロパティは次のようにアクセスする方法を持っている構文的な方法である - これは代わりに、メソッドのであることを

public int PlannedProductionTime 
{ 
    get { return ShiftTime - BreakTime; } 
} 

注:あなたの意図は、計算PlannedProductionTimeを返すプロパティを持っていたなら、それはこのように行くことができますプロパティ:

OET myOet =新しいOET(); int plannedProductionTime = myOet.PlannedProductionTime;

0

ない「取捨選択」の利用および「breaktime」ローカル変数には存在しない機能です。戻り値ShiftTime-BreakTimeを使用してください。

public int method2() { 
///here you are getting the peroperties value and doing calculations returns result. 
    return ShiftTime -BreakTime; 

} 

プロパティ値を設定する必要がある場合は、

public void method1(int shift, int breaktime) { 

     ShiftTime= shift ; 
    BreakTime = breaktime; 


    } 
0

あなたはそれでgetメソッドを定義することにより、計算するものとしてプロパティを定義することができます。

その他のソリューション - 別の関数を定義してget内で呼び出すことができます。クラス内の他の場所(プライベートクラスまたは外部クラスのクラス)で使用する必要がある、より複雑な計算をしたい場合に役立ちます。

public int PlanedProductionTime { get { return _calculatePlannedProductionTime(ShiftTime, BreakTime); } } 

private\public int _calculatePlannedProductionTime (int shift, int break) 
{ 
return shift - break; 
} 
関連する問題