Feature request: partial class constructors

Hello,
I’m working with partial classes to have code that is common to clients and server. That also let me have code that are different between clients and server, mainly different constructors/methods or with different parameters.
The problem is with the class constructors: if I want to have some class fields defined as ReadOnly, I have to initialize them directly in the class constructor. If I need to have common and different class fields for clients and server, I need to have at least 2 class constructors: one in the common partial class and one in the clients/server partial class. Unfortunately, the compiler doesn’t accept it for the moment.
So my request to accept more than one class constructor in a partial class and to just merge their code.
Patrick

hmm. how would it know in which order to merge the code, for one?

Hello Mark,
I don’t know exactly, but should it be a problem with class constructors ?
We already have a possibility to initialize directly class fields and the code is appended to the class constructor.
Patrick

You could use class initializers for this? I really don’t like having two code blocks of which I have no way of knowing in what it runs.

Hello Carlo,
why do you mean exactly with class initializers?
Do you mean the initialization of a field where it is declared?
Unfortunately the initialization code is sometimes too complex to define it in this manner.
In addition, if I define many fields this way, I also don’t have the control in which order they are initialized.

In the case of partial class constructors, you could force to declare them with the partial keyword, so that it is visible that there can be more than one.
You could also limit a class constructor to initialize only members defined in the same source file.
Patrick

Hi Patrick,

what would be the advantage over traditional class inheritance, where you can decide in the descendant class if a property is readonly or not? And intialize exactly what you want? Those partial classes are perfect for code-generated parts, but I never use them for anything else because they introduce two parts, which for code clarity you do not want… but maybe you have a good example where the partial class would be better than inheritance?

Hugo

Hugo,
I use them because I have code that is common to both web service part and clients part (2 Silverlight clients and 1 WPF client). The part of the class definition is in one file, the web service only part in a second one, the Silverlight only part in a third one and the WPF only part in another one.
For example, the code that deals with the SQL server is in the web service part, the code that deals with UI is in the client. There is also different security model between the always-connected Silverlight clients and the not-always-connected WPF client.
I think the code is more readable this way that using plenty of $IfDef in the source files.
To help navigate in the application, I have a help file that documents all the classes, fields, methods, …, even the private ones.
Patrick

Hi Patrick,

I understand your model, as I’m building the same :slight_smile: Only I use interfaces and a base type. I use interfaces to create a very well defined contact area between backend code which is the same for all platforms. As I reread your first post, it seems you did not fully separate your common code from the platform and client/server specific code (different parameters for methods). That means, that even if you solve the constructor problem, you will run into problems soon again. Consider solving the parameter problem by constructing a parameter object instead of separate parameters. This way the platform-code can decide which parameters to use, and which are not necessary to this platform.

I hope this gives you some useful pointers…

Even if you solve the constructor problem, you will run into problems soon again.

Hi Hugo,
there is no problem in the solution: it has been started 2 years ago and the client and server are working fine.
I only have a problem with the class constructor, where I have one specific class field to be initialized only on the web server part, because it has no sense to be defined on the clients. In the same class, I have a class field that must be initialized for clients and web server.
I have a workaround, but there would be simpler with partial class constructors.
Patrick