Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2012/5.0 > C# 2012/5.0 General Discussion
| Search | Today's Posts | Mark Forums Read
C# 2012/5.0 General Discussion Discussions about the C# 2012 language and tool not related to any specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2012/5.0 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 29th, 2014, 03:52 AM
Authorized User
Points: 130, Level: 2
Points: 130, Level: 2 Points: 130, Level: 2 Points: 130, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2012
Posts: 29
Thanks: 14
Thanked 0 Times in 0 Posts
Default class returned from inside when and why

please see this piece of coding , given in page 119 C#4 (2010) Professional Wrox Book
Code:
ublic class Shape
{
public double Width { get; set; }
public double Height { get; set; }
public override string ToString()
{
return String.Format("Width: {0}, Height: {1}", Width, Height);
}
}

public class Rectangle: Shape
{
}
public interface IIndex < out T >
{
T this[int index] { get; }
int Count { get; }
}
public class RectangleCollection: IIndex<Rectangle>
{
private Rectangle[] data = new Rectangle[3]
{
new Rectangle { Height=2, Width=5}, //  please see below
new Rectangle { Height=3, Width=7},
new Rectangle { Height=4.5, Width=2.9}
};
public static RectangleCollection GetRectangles()
{
return new RectangleCollection();
}
public Rectangle this[int index]
{
get
{
if (index < 0 || index > data.Length)
throw new ArgumentOutOfRangeException("index");
return data[index];
}
}
public int Count
{
get
{
return data.Length;
}
}
}

please locate this area

public class RectangleCollection: IIndex<Rectangle>
{
//etc

public static RectangleCollection GetRectangles()
{
return new RectangleCollection();
}
// etc 
}

"RectangleCollection" is a class name and we are returning this class from inside the class

RectangleCollection itself.

Could you please explain the physical scenarios (and when and why) when we need to return an instance of the class from inside the class itself.

which chapter should I read here?
 
Old March 14th, 2014, 12:25 AM
Friend of Wrox
Points: 528, Level: 8
Points: 528, Level: 8 Points: 528, Level: 8 Points: 528, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
Default

The method that is returning the the RectangleCollection is static, and is what is know as a factory method. It is used to create instances of RectangleCollections.

It must be referenced and used without an instance of a RectangleCollection because it is declared static.
Like so
Code:
var reccol = RectangleCollection.GetRectangles();

Last edited by mmorgan30; June 4th, 2014 at 10:38 PM..
The Following User Says Thank You to mmorgan30 For This Useful Post:
amigo1 (May 20th, 2014)
 
Old May 20th, 2014, 10:55 AM
Authorized User
Points: 130, Level: 2
Points: 130, Level: 2 Points: 130, Level: 2 Points: 130, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2012
Posts: 29
Thanks: 14
Thanked 0 Times in 0 Posts
Default

Are yo teacher Morgan Skinner?
thank you
regards




Similar Threads
Thread Thread Starter Forum Replies Last Post
Retrieving data elements from inside an ArrayList Class shiftingspanner Visual C++ 0 May 17th, 2010 01:00 AM
Accessing and using an ActiveX from inside a HTML page hosted inside a WebBrowser con adyrotaru C# 2005 2 June 25th, 2009 04:21 PM
How to call normal "write" function inside a class jonen C++ Programming 3 May 25th, 2006 02:19 PM
OCIEnvCreate returned -1 amagoosla ADO.NET 3 June 9th, 2005 06:27 AM
Capturing toolbar events inside a dialog class pazzuzu Visual C++ 0 October 13th, 2004 01:35 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.