constructor

Set Only On Deserialization

Problem: I have a class, say Foo, that implements an Id property. Foo must be serializable. Foo.Id should be initialized to a new GUID on initialization of Foo. Foo.Id should not be changable once it has been set. Deserialization will attempt to set the Foo.Id, so it must be made Public. Private _Id As String=system.Guid.NewGuid....

Dealing with multiple input parameters using wrapper class

As a sort of continuation of this, I have the following newbie question: What difference is there in building a wrapper class that expects lots of inputs parameters and inputting those parameters directly into the final constructor? Don't get me wrong, I think the multiple input parameter thing is pretty ugly, and I'm trying to get aro...

Delphi: Record constructor vs factory function

So what will be the preferred way of initializing records? With a 'factory function': TMyRecord = record valueX: integer; valueY: integer; end; function MyRecord(const AValueX, AValueY: integer): TMyRecord; begin result.valueX := AValueX; result.valueY := AValueY; end; var myrec: TMyRecord; begin myrec := MyRecord(1, 2); ...

C# Singleton with constructor that accepts parameters

I want to create a static class or singleton class that accepts a reference to another object in its constructor. Static classes are out, but I figured I could create a singleton that accepts parameters in its constructor. So far I haven't had any luck figuring out or googling the syntax. Is this possible? if so, how do I do it? Sorry ...

How to override member of base class after inheritance in C++

Hello all i have this : class A { public : A(int i ) : m_S(i) { m_Pa = new Foo(*this) ; } private : int m_S ; Foo* m_Pa; } and derived class class B : public A { public : B() : A (242) { // here i like to override the A class m_Pa member but...

How can I reference a constructor from C# XML comment?

Dear ladies and sirs. Is it possible to reference a constructor from a C# XML comment without resorting to the explicit prefixes (like M: or T:)? For instance, the following yields compilation warnings, because the compiler does not like ".ctor". Trying "PublishDynamicComponentAttribute.#ctor" is no good, "PublishDynamicComponentAttribu...

Get the name of caller (or method) that created the WCF Service instance?

as simple as i can: i have a WCF service with a private constructor. if i'm not wrong, constructors must be parameterless in WCF and the parameter i need is the caller's name (or caller's method name), so i can assign a few readonly fields. so, the question is: is there a way that i can get the caller's name -- or the method that invok...

Can I perform actions during object construction by modifying the Function prototype?

I'd like to augment the Function object to do more operations while constructing a new object. Is it possible? ...

Instantiate singleton object using Class.forName()?

I want to instantiate one instance of a class from the string name of the class. ( using Class.forName().newInstance(). ) Here's the problem: I want that instance to be a singleton.. I could do this using a singleton pattern, except that newInstance calls the default constructor for a class, and with a singleton, that constructor must ...

help with reflection + constructors

I have code I'm working on to instantiate a CRC algorithm dependent on a polynomial passed in, and a string s that contains "crc8" or "crc16" or "crc32". The classes CRC8, CRC16, and CRC32 all extend a class CRC and implement an interface HashAlgorithm. Each of them has a constructor CRCx(int polynomial). My problem is, I get this erro...

Is there any automated way to implement post-constructor and pre-destructor virtual method calls?

Hi all, Due to the well-known issues with calling virtual methods from inside constructors and destructors, I commonly end up with classes that need a final-setup method to be called just after their constructor, and a pre-teardown method to be called just before their destructor, like this: MyObject * obj = new MyObject; obj->Initiali...

What is the conventional way to assign default values to member variables in C#?

Which is more conventional in C#? class Foo { private string _first; private string _second; public Foo(string first) { _first = first; _second = string.Empty; } } or class Foo { private string _first; private string _second = string.Empty; public Foo(string first) { _first = first; } } ...

Overridable Methods In Constructors -Help to Fix

Hi all, Im attempting to use fxCop on a C# Project as kind of basis for C# coding standards. The project Im using is called S#arp Architecture and is freely available here: S#Arp Arch Now if I run fxCop (most things have been fixed already) I need to fix the CA2214 fxcop error for overridable methods in contructors. At the moment a p...

How to Avoid Calling Viritual Methods from a Base Constructor

I have an abstract class in a library. I'm trying to make it as easy as possible to properly implement a derivation of this class. The trouble is that I need to initialize the object in a three-step process: grab a file, do a few intermediate steps, and then work with the file. The first and last step are particular to the derived class....

What is the correct way to validate the arguments of a constructor

So I have a class with a single string parameter in its constructor: public MyClass(string name) { this.Name = name; } but I never want to worry about some one initializing an instance of MyClass with name that is NULL or a zero length string. What is the best was to validate the parameter? With a property or method you can always...

Initializing Class Fields at the Field Definition or in Class Contructor

Say I have a class with a field that needs to be initialized when the object is initialized. Such as list that need to be created before objects can be added/removed from it. public class MyClass1 { private List<MyOtherClass> _otherClassList; public MyClass1() { this._otherClasslist = new List<MyOtherClass>(); }...

Overridable methods in constructors with InitMembers()

I have carried the method here on almost all of the areas where I have had overridable methods and managed to fix them but there is one part where the method doesnt work in the same way on a different contexted piece of code: public Employee() { this.InitMembers(); } private void InitMembers() { // I...

How to handle incorrect values in a constructor?

Please note that this is asking a question about constructors, not about classes which handle time. Suppose I have a class like this: class Time { protected: unsigned int m_hour; unsigned int m_minute; unsigned int m_second; public: Time(unsigned int hour, unsigned int minute, unsigned int second); }; While I would wa...

Constructor of class in template

I have a object cache class like this: #include "boost/thread/mutex.hpp" #include "boost/unordered_map.hpp" template <typename type1, typename type2> class objectCache { public: objectCache() { IDCounter = 0; } ~objectCache() { for ( it=free_objects.begin() ; it != free_objects.end(); it++ ) delete (...

What does "public Weatherman(Integer... zips) {" mean in java

I am trying to read some Java code from a tutorial, I don't understand the line: public Weatherman(Integer... zips) { I don't understand what the ... represents if it was just (Integer zips) I would understand that there is a variable of class Integer called zips. But the ... are confusing me. ...