Jump to content

C# Tryparse to test atribute

JuztBe

Hello, iv'e heard that you can check if you have entered correct number in attributes. I'm close to doing something or I failed miserably(probably this one). If there anything besides tryparse would work - it's fine too.
What I'm trying to make: if I type in number,  attribute = value. If I type in letter attribute = 0
Hope this makes sense

 

{    public class TP{    public int Weight;     public  int number;     public int aWeight     {          get { return Weight; }         set         {             if (int.TryParse(Convert.String(Weight), out number))                 value = number;             else             {                  value = 0;             }         }     }

 

 

Edited by colonel_mortis
Added code tags

Laptop: Acer V3-772G  CPU: i5 4200M GPU: GT 750M SSD: Crucial MX100 256GB
DesktopCPU: R7 1700x GPU: RTX 2080 SSDSamsung 860 Evo 1TB 

Link to comment
Share on other sites

Link to post
Share on other sites

Not sure why you have Convert.String in there, that is the whole point of TryParse, to try to parse the text value into an int or other numerical value. Also, if the TryParse fails, it automatically changes whatever the out value is to zero so there is no need for an if else.

 

Edit: This is assuming you are getting your weight value through input.

Link to comment
Share on other sites

Link to post
Share on other sites

Since a property has a defined type, the compiler will stop you from using anything that can't automatically be assigned to that type. In your example, the property is an int. The compiler will stop you from assigning a string to it. There's no need for TryParse because it'll need to be a valid int to even build your code.

aWeight = "100"; // won't be able to be compiled

You could make a method for what you want to do

public void SetWeight(string value){    int number;    if (int.TryParse(value, out number))       Weight = number;    else       Weight = 0;}


Here's a little extra info about properties.

private int _weight;public int Weight{    get { return _weight; }    set { _weight = value; }}// or use an automatic property which is equivalent to the abovepublic int Weight { get; set; }

A property usually has a private field to hold the value, not a public field. This is because you wan't people to interact with the property, not the field. If they can interact with the field directly, its bypassing any get/set behaviour you defined in the property.

 

When it comes to the setter, value is already assigned to whatever is being assigned to the property.

Weight = 100;

In the above example, value will be set to 100. You can reassign it though, just remember you still need to assign it to the field like so.

_weight = value;
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×