Code Red > .NET

How to validate a property of a struct ?

<< < (2/4) > >>

kdub_nz:

also, for casting check for truncation

https://docs.microsoft.com/en-us/dotnet/api/system.byte.maxvalue?view=netframework-4.7.2 :

MickD:
I guess it depends on the context, for a colour value like this I'd just cap it at either end as the end result won't make much if any difference to the final colour and avoids a lot of unnecessary boiler plate to handle exceptions. Any value greater than 255 for Red won't make it any red-er :)

kdub_nz:
casting an  int (which happens to have a calculated value of 330) to a byte may give a result not expected

ie results in a .Red value of 74

added:
I s'pose at the end of the day "it all depends"

Grrr1337:

--- Quote from: MickD on January 05, 2019, 07:48:04 PM ---The best way to do this is by creating a 'ColorValue' class.

something like this

--- Code - C#: ---class ColorValue{..} 
It's deemed good practice wrap a primitive type like this when you would use it as a 'type' in your program and only use primitive types like int, string etc within methods for calculations (within a small scope and not public throughout your app).
You may also need to write ToString() and other interface methods etc as needed.

--- End quote ---

Thanks Mike!
Was looking exactly how it should be done.. since int is a non-nullable type, the compiler throws error if I try to set the int property to null.
Maybe instead of throwing an exception when the value is out of range, perhaps in this case would be more appropriate to return a negative int value?
Although the best behaviour would be to "throw an exception at runtime", like you mentioned in your next post. :)
BTW your example works fine even if it was defined as struct instead of class.



--- Quote from: kdub on January 05, 2019, 08:04:11 PM ---Could you just define the properties as byte ( Unsigned 8-bit integer ) ?

--- End quote ---

Thanks kdub, byte worked perfectly for this case -


--- Code - C#: ---struct TrueColor{   public byte Red;  public byte Green;  public byte Blue;}


--- Quote from: MickD on January 05, 2019, 08:13:44 PM ---
--- Quote from: kdub on January 05, 2019, 08:04:11 PM ---Could you just define the properties as byte ( Unsigned 8-bit integer ) ?

--- End quote ---

good point, would that truncate the value on both sides or throw an exception at runtime? For example, if a user enters a larger value at a prompt?

--- End quote ---

Would be nice if there was a runtime validation, upon a custom criteria - like in your example from reply #1.
Although even if it was possible, the compiler would be able to validate it only on a constant variable (because the value would be predefined) and not on an user input like Console.ReadLine();

My question started upon exploring structs more carefuly, before I was just using classes with properties and constructors.
But realised that instead of defining 5-6 Constructors to validate (all or some of) the properties, I could just use a struct, so I remembered John's cow
The problem was I didn't knew how to validate within the property (without a constructor).. in order to apply a range to the cow's weight.
But now I know, thanks to Mike's replly!


I was attempting to solve this exercise #12 from here, where the validation steps into the next level (having an unique ID for every employee)

--- Quote ---12. A company dealing with marketing wants to keep a data record of its employees.
Each record should have the following characteristic – first name, last name, age, gender (‘m’ or ‘f’) and unique employee number (27560000 to 27569999).
Declare appropriate variables needed to maintain the information for an employee by using the appropriate data types and attribute names.

--- End quote ---
So I guess that would mean wrapping the Employee struct within a Employess class, along with a List<Employee> to iterate through due the ID validation ?

(BTW I'm not changing my question, I'm expanding it)

MickD:
I see no reason to use a struct in this case, especially given the amount of different data types and validation required. You can then wrap the Employee in a 'Employees' class that would hold the list of Employee and would create the ID when adding a new Employee.

Something like this perhaps?

--- Code - C#: ---class Employee{    private _name;    // etc...    // need unsigned int as can't have negative id number    private uint _id;     int ID {        {get; set;}    }} class Employees {    private List<Employees> _employees = new List<Employees>();    // as ID is to be unique you don't need to reuse them so just keep a total    private uint _idCount;     public uint Add(Employee e){        // ... other init stuff here        e.ID = _idCount++;    }} 
I think structs are handy and lightweight when you need something like a coordinate or similar where there is a small group of similar data. If you need to do work on coordinates then it would be easier to create a class so you can do vector calculations etc.
You very rarely see structs in the wild :)

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version