Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Liam Andersson
Liam Andersson
1,396 Points

RangeAttribute-class?

Hey guys! I have a question for you C# people out there! Do anyone know how to use the RangeAttribute-class? (https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.rangeattribute(v=vs.110).aspx)

I want to limit the user to only write the numbers 1-5 in my project and I quite didnt understand the RangeAttribute.

Egen.cs in https://w.trhou.se/4oxt1k8ddz

Thansk for any answer!

2 Answers

Steven Parker
Steven Parker
231,122 Points

Attributes like this are only for data-driven applications built on Visual Studio. You won't be able to use them in a simple console application built with the "mono" system.

But it's reasonably easy to test the input and require the user to keep trying while the answer is outside the range:

while (minutes < 1 || minutes > 5) {
    System.Console.WriteLine(minutes + " isn't an acceptable value, please try again!");
    minutes = double.Parse(System.Console.ReadLine());
}

Your code seems to have a test that would do the same thing after you fix its position in the code flow.

Liam Andersson
Liam Andersson
1,396 Points

So where exactly does the while statement inside the parentheses mean? And where am I supposed to put this code? I tried putting a similar code to yours outside my other while method but I got errors.

Steven Parker
Steven Parker
231,122 Points

The "while" statement means, "do this for as long as minutes is either less than 1 or greater than 5".

And you might put that loop right after the input is parsed and assigned to "minutes", around line 26 in the snapshot code.

Liam Andersson
Liam Andersson
1,396 Points

Nvm I might have figured it out