2017-10-27 3 views
-3

私はまだC#を学んでいますが、私は年中休暇を取っています。仕事に戻ってきて、シニアが彼に行く前に年次休暇:else文をconditionを含む1つのメソッドに置き換えます。

public string GetBasketTotalPrice(string basketLocation) 
{ 
    var basketTotalPrice = _driver.FindElements(CommonPageElements.BasketTotalPrice); 
    if (basketLocation.ToLower() == "top") 
     return basketTotalPrice[0].Text.Replace("£", ""); 
    else 
     return basketTotalPrice[1].Text.Replace("£", "");   

} 

private int GetElementIndexForBasketLocation(string basketLocation) 
{ 
    return basketLocation == "top" ? 0 : 1; 
} 

私の代わりにあればelse文を使用して、彼は私がGetElementIndexForBasketLocationの彼の新しい方法を使用したいと仮定しています。

私の質問は、単にこの変更を実装する方法ですか?

おかげ

+1

'var index = GetElementIndexForBasketLocation(basketLocation.ToLower());戻り値basketTotalPrice [index] .Text.Replace( "£"、 ""); ' – FCin

+0

ヒント:GetElementIndex ...から返された0または1は、basketTotalPriceにどのように関係しますか? *新しい*番号は追加されませんでした。 – user2864740

答えて

1

それはあなたが探しているものを完全に明確ではないのですが、あなたは、このようなコードの何かを手直しすることができます:彼はあなたを提供する方法がToLower()を呼び出していないように見えます

public string GetBasketTotalPrice(string basketLocation) 
    { 
     var basketTotalPrice = _driver.FindElements(CommonPageElements.BasketTotalPrice); 
     int index = GetElementIndexForBasketLocation(basketLocation); 
     return basketTotalPrice[index].Text.Replace("£", "");   
    } 

    private int GetElementIndexForBasketLocation(string basketLocation) 
    { 
     return basketLocation.ToLower() == "top" ? 0 : 1; 
    } 
1

をそれをクライアントに任せてその作業を行い、それは道路を過ちるミスにつながる可能性があります。

ToLowerの代わりにstring.EqualsStringComparison.OrdinalIgnoreCaseを使用することも考えられます。

文字列のメソッドを呼び出す前にヌルチェックを追加することをお勧めします。NullReferenceExceptionの代わりにArgumentNullExceptionを投げることができます。

public string GetBasketTotalPrice(string basketLocation) 
{ 
    var basketTotalPrice = _driver.FindElements(CommonPageElements.BasketTotalPrice); 

    return basketTotalPrice[GetElementIndexForBasketLocation(basketLocation)] 
     .Text.Replace("£", ""); 
} 

private int GetElementIndexForBasketLocation(string basketLocation) 
{ 
    if (basketLocation == null) throw new ArgumentNullException(nameof(basketLocation)); 
    return basketLocation.Equals("top", StringComparison.OrdinalIgnoreCase) ? 0 : 1; 
} 
関連する問題