AlexCode Posted December 4, 2004 Posted December 4, 2004 Hi! When we have a class to serialize we must ensure these 2 fundamental issues: 1- Add the Serializable attribute. 2- Leave a Sub New() constructor Now, imagine that I need to protect this simple NEW() constructor, but still enable serialization... how can I do it?? I tryed to: Protected Sub New() But the Serialization Formatter throws an error... Thanks, Alex :p Quote Software bugs are impossible to detect by anybody except the end user.
*Gurus* Derek Stone Posted December 4, 2004 *Gurus* Posted December 4, 2004 Serializable classes have to have a default public constructor. No buts about it. Quote Posting Guidelines
AlexCode Posted December 5, 2004 Author Posted December 5, 2004 So there's no way I can protect a serializable class from beeing "constructed" with a default constructor? hummmmmmm sounds strange... and anoying at the same time... sucks! Alex :o Quote Software bugs are impossible to detect by anybody except the end user.
*Gurus* Derek Stone Posted December 5, 2004 *Gurus* Posted December 5, 2004 I don't see what difference it would make. What in particular are you trying to accomplish, or should I say "protect"? Classes are, after all, meant to be instantiated. Quote Posting Guidelines
AlexCode Posted December 5, 2004 Author Posted December 5, 2004 Yeah, classes need to be instatiated, but shurelly you have already created a class that must receave at leat a parameter on the constructor! If this parameter is essessial to the work of the class, and also that class must be serialisable, that means that a default (no parameters) constructors will be available to use but will generate errors if used! See what I mean? Alex :p Quote Software bugs are impossible to detect by anybody except the end user.
Wile Posted December 5, 2004 Posted December 5, 2004 Stop trying to use c++ tricks in VB :p. Besides, the best trick in c++ was to define it as private in the header, without implementing it ;) I think the serialize attribute requires that the object can be instantiated from another class without any parameters. So that really conflicts with what you want to do here, either no more serializable or change the check on the parameter needed for instantiation by using something like an initialize method or so. Quote Nothing is as illusive as 'the last bug'.
AlexCode Posted December 5, 2004 Author Posted December 5, 2004 I'm convinced but not satisfyed... Damn... Alex :p Quote Software bugs are impossible to detect by anybody except the end user.
*Gurus* Derek Stone Posted December 5, 2004 *Gurus* Posted December 5, 2004 I don't believe I've ever created a class that needed a parameter to be passed to its constructor. If the parameter is null the code throws a descriptive exception to the caller on invocation of a method (or less likely a property accessor) that requires the parameter. This is normal. Quote Posting Guidelines
Wile Posted December 5, 2004 Posted December 5, 2004 The advantage would be that the compiler performs the check and doesnt allow the code to build, instead of a run-time error that might be found with sufficient testing, but also might not be found until found by the end-user who is very good at finding the weirdest bugs ;). Quote Nothing is as illusive as 'the last bug'.
AlexCode Posted December 5, 2004 Author Posted December 5, 2004 Finally someone who understands me... :D This may sound a little nonsence if you think about classes for our own use, but if we're talking about classes from a public control, any end user may instatiate the class the wrong way, not knowing that's wrong untill the exception arrises... Then the debugging may be somehow hard to find for somehone who doesn't know how that really works... I think that at least the Protected declaration should work... Alex :p Quote Software bugs are impossible to detect by anybody except the end user.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.