One way would be to place an exception in the constructor:
csharp
class Widget {
private string name;
private int grams;
public Widget(string name, int grams) {
if (grams < 1) {
throw new System.ArgumentException("Grams cannot be less than 1.");
}
this.name = name;
this.grams = grams;
}
Another would be to place an "IsValid" flag in the object.
You could also use something called a factory pattern, where another object provides instances for you and no other object can. Though you implied you didn't want that.
A variation on the factory is to allow the object itself to handle the creation on instances external to the constructor. This allows for some extra failover behavior, including just returning null. Here's an example of that:
csharp
class Widget {
private string name;
private int grams;
// private constructor
// no one other than me can create an instance
private Widget() { }
public string Name { get { return this.name; } }
public int Grams { get { return this.grams; } }
public override string ToString() {
return "Widget: " + this.name + ", " + this.grams + "g";
}
// one of the few times I'd ever use static
public static Widget GetNew(string name, int grams) {
// we don't have to throw errors
// just send back a null object.
if (grams < 1) { return null; }
Widget obj = new Widget();
obj.name = name;
obj.grams = grams;
return obj;
}
}