Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class with proprties that haven't been set

Tags:

c#

oop

class

I am creating a class in C# which eventually will be part of a library that other users can use. A user of this class has to set some properties and then use a public method to retrieve the results. What shall I do when a user calls the method without setting all the properties? Throw exception and expect the user to catch it?

Thanks

like image 957
koumides Avatar asked Dec 01 '22 05:12

koumides


2 Answers

It might be better to create a constructor that takes the parameters, so that you are constructing an object instance that is in a usable state.

like image 187
Mitch Wheat Avatar answered Dec 04 '22 11:12

Mitch Wheat


Along with all the other options that have been mentioned you could have the properties set to default values. Then the user has to set them to something different if they don't want the default behavior. But really without knowing more about what your code is doing it's hard to say what the best option is.

like image 44
juharr Avatar answered Dec 04 '22 12:12

juharr