tag:blogger.com,1999:blog-5611706923356552090.post5726579928765821693..comments2024-03-22T05:16:37.375-04:00Comments on C Razor Sharp / C# / .NET: Factory Pattern with Attributes. Get rid of the ugly switch / case.A.Friedmanhttp://www.blogger.com/profile/17438995939632438979noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-5611706923356552090.post-21444505464050775852009-07-02T09:16:34.111-04:002009-07-02T09:16:34.111-04:00You know, I never knew about the Enum.IsDefined(.....You know, I never knew about the Enum.IsDefined(..) method. I guess the only way for that to fail, is if you deliberately call the GetVehicle method like this:<br /><br />var v = GetVehicle((VehicleType)100); or some other crazy number...<br /><br />Not a bad suggestion, but if someone is calling that method like that, that's their problem :-P<br /><br />As for your second suggestion, I was debating last night if I should throw an exception or return null. You're probably right though, an Exception would make more sense....A.Friedmanhttps://www.blogger.com/profile/17438995939632438979noreply@blogger.comtag:blogger.com,1999:blog-5611706923356552090.post-22468766380140473542009-07-02T09:05:26.535-04:002009-07-02T09:05:26.535-04:00Your GetVehicle method should check the validity o...Your GetVehicle method should check the validity of the VehicleType property ex.<br /><br />if (!Enum.IsDefined(vehicle)) {<br /> throw new InvalidArgumentException();<br />}<br /><br />Also, consider throwing an exception instead of returning null if the attribute is not found.Unknownhttps://www.blogger.com/profile/08079758815113939759noreply@blogger.com