Hi Guys
NP52 Two objTwo = Two.New();
http://www.c-sharpcorner.com/UploadFile/rmcochran/instantiationFactory06232007153242PM/instantiationFactory.aspx
In the article (website address is given above) there is a following part:
In this scenario, when we want to "new-up" our class, we call it through the static method as follows:
Number objTwo = Two.New();
The following code would not compile because the New() factory method now returns a "Number" object and not a "Two" object:
Two objTwo = Two.New();
There is an error. Two objTwo = Two.New(); must come instead of Number objTwo = Two.New();
And Number objTwo = Two.New(); must come instead of Two objTwo = Two.New();
I wish to confirm whether I correct or not. Please confirm it.
Thank you
Posted Oct 11, 2007, 8:00 AM
Thank you, Alan
AlanPosted Oct 11, 2007, 5:14 AM
I think the article is in fact correct, Maha.
When instantiating a Two object, you now have to call the Two.New() method which returns a reference to a Number, not a Two. In other words, although its a Two object, its implicitly converted to a Number reference which is OK because Number is the base class.
You therefore need this line:
Number objTwo = Two.New();
The following line won't compile because there's no implicit conversion back from Number to Two:
Two objTwo = Two.New();
However, this will work at both compile time and runtime:
Two objTwo = (Two)Two.New(); //explicit conversion to Two