Wrox Programmer Forums
|
BOOK: Beginning Visual C# 2010
This is the forum to discuss the Wrox book Beginning Visual C# 2010 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, ; ISBN: 9780470502266
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2010 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 November 11th, 2011, 03:12 PM
Authorized User
 
Join Date: Nov 2011
Posts: 34
Thanks: 14
Thanked 0 Times in 0 Posts
Default Chapter 10 Ch10Ex01

In following Class code:

public class MyClass
{
public readonly string name;
private int intval;

public int val
{
get
{
return intval;
}
set
{
if (value>=0 &&value<=10)
intval=value;
else throw (new ArgumentOutOfRangeException("val",value,"val must be assigned a value between 0 and 10"));
}
}
public override string ToString()
{
return "name:" + name + "\nval:" + val;
}

private MyClass() :this("default name")
{
}

public MyClass(string newname)
{
name = newname;
intval = 0;
}


}
}



is the purpose of last code block:


public MyClass(string newname)
{
name = newname;
intval = 0;
}

to define fields which are declared in first block code:

public class MyClass
{
public readonly string name;
private int intval;

or, something else?
 
Old November 13th, 2011, 01:36 PM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

Quote:
Originally Posted by dampyr View Post

is the purpose of last code block:

public MyClass(string newname)
{
name = newname;
intval = 0;
}

to define fields which are declared in first block code:

public class MyClass
{
public readonly string name;
private int intval;

or, something else?
The member of the class MyClass named MyClass has the same name as the class and thus is a constructor. See page 217 on constructors. From the book: "...you can provider your own, if required, which enables you to initialize ... your objects...
In the sample code you posted, the fields name and intval are initialized in the constructor.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
The Following User Says Thank You to ChristianNagel For This Useful Post:
dampyr (November 13th, 2011)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch10Ex01 Can someone explain this query arun_babu_a BOOK: Beginning Visual C# 2010 1 October 25th, 2011 02:46 AM
chapter 10 ex 1 poldeleeuw BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 2 December 23rd, 2010 07:15 AM
Chapter 10, listing 10-10-app kiwibrit BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 2 August 18th, 2009 04:21 AM
Chapter 10 JimSchubert BOOK: Ruby on Rails for Microsoft Developers 1 May 17th, 2009 11:35 PM





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