c# - Try catch not catching exception with input -


i creating blackjack game , far have made card class. card class works, when go test card class when test3 exception should catch because "x" not in values array reason doesn't catch , displays x instead of error message "invalid input". want happen value set accessor should search string[]values array , determine if value argument valid or not , if isn't throw new exception. not sure on how fix this. can't use enum values need use values array.

any appreciated

here have card class

class card {      public enum suit { hearts, spades, diamonds, clubs };      private suit _suit;     private string _value;      public card(suit suit, string value)     {          _suit = suit;         _value = value;     }      public suit suit     {                 {             //return member variable value             return _suit;         }         set         {             _suit = value;         }     }      private string[] values = { "a", "2", "3", "4", "5", "6", "7", "8", "9", "10", "j", "q", "k" };     public string value     {         { return _value; }         set         {             if (!values.contains(value))             {                 throw new argumentexception("invalid input");             }             _value = value;         }     } } 

here code testing with

try         {             card testcard1 = new card(card.suit.spades, "q");             console.writeline(testcard1.suit + " " + testcard1.value);             card testcard2 = new card(card.suit.diamonds, "10");             console.writeline(testcard2.suit + " " + testcard2.value);             card testcard3 = new card(card.suit.diamonds, "x");             console.writeline(testcard3.suit + " " + testcard3.value);          }         catch (argumentexception e)         {             console.writeline(e.message);         }         console.readline(); 

you assigning incorrect value via constructor backingfield. isnt bad thing todo, bypasses propertylogic.

public card(suit suit, string value) {      _suit = suit;     _value = value; // assignes value directly field, bypassing property-logic. } 

change this, easiest solution

public card(suit suit, string value) {      _suit = suit;     value = value; // use property here. } 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -