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 October 26th, 2011, 04:14 AM
Authorized User
 
Join Date: Sep 2011
Posts: 24
Thanks: 7
Thanked 0 Times in 0 Posts
Default Ch10 this keyword page 256

Can someone explain this keyword in page 256 step by step.I am lot confused
 
Old October 26th, 2011, 07:13 AM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

Can you give more details on what you want to know? Currently I'm on the road and don't know what's exactly on page 256. You want to know what a constructor initializer is used for?
A constructor is clear?
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
 
Old October 27th, 2011, 12:38 AM
Authorized User
 
Join Date: Sep 2011
Posts: 24
Thanks: 7
Thanked 0 Times in 0 Posts
Default this keyword

public void do something()
{
MyTargetClass myObj=new MyTargetClass();


myObj.DoSomethingWith(this)


}



also,,,,

In another example in samepage
in property named Somedata

return this.someData;

Please explain the use of these.
 
Old October 27th, 2011, 01:10 AM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

The keyword "this" is always used to reference the current instance.

Code:
public class Foo
{
   public void DoSomething()
   {
      MyTargetClass myObj = new MyTargetClass();
      myObj.DoSomethingWith(this);
   }
   public void Bar()
   {
   }
}

public class MyTargetClass
{
   public void DoSomethingWith(Foo foo)
   {
      foo.Bar();
   }
}
The DoSomething method is an instance method of the Foo class. this is used within members of the class to reference the current instance of the class.
The DoSomethingWith method of the MyTargetClass class is declared to receive an object of type Foo as its parameter. Thus within the DoSomething method the current instance that is of type Foo can be passed to the DoSomethingWith method, and within the DoSomehingWith method every public member of Foo can be accessed.

That's very similar with properties:
Code:
private string someData;
public string SomeData
{
   get
   {
      return this.someData;
   }
   set
   {
      this.someData = value;
   }
}
SomeData is an instance property of a class. With the this keyword you access the current instance. With this.someData you access the field someData that is defined as an instance field.

In this scenario you could also write it this way:
Code:
private string someData;
public string SomeData
{
   get
   {
      return someData;
   }
   set
   {
      someData = value;
   }
}
Using the this keyword just gives a better readability to show an instance member is used instead of a local variable. The this keyword must be used in cases if there is a local variable with the same name to define the instance member is used instead of the local variable.

Hope this helps.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
 
Old October 27th, 2011, 01:35 AM
Authorized User
 
Join Date: Sep 2011
Posts: 24
Thanks: 7
Thanked 0 Times in 0 Posts
Default

[code]
public class Foo
{
public void DoSomething()
{
MyTargetClass myObj = new MyTargetClass();
myObj.DoSomethingWith(this);
}
public void Bar()
{
}
}

public class MyTargetClass
{
public void DoSomethingWith(Foo foo)
{
foo.Bar();
}
}

Please explain the parts marked bold
also Foo and MyTargetClass are seperate classes.Is it possible to call methods and properties from other classes.
 
Old October 27th, 2011, 01:43 AM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

Quote:
Originally Posted by arun_babu_a View Post
Code:
public class MyTargetClass
{
   public void DoSomethingWith(Foo foo)
   {
      foo.Bar();
   }
}
Please explain the parts marked bold
also Foo and MyTargetClass are seperate classes.Is it possible to call methods and properties from other classes.
DoSomethingWith is a method of the class MyTargetClass that does not return any value and thus is declared void, and requires a parameter of type Foo. The access modifier is public and thus it can be invoked from any type.

And:
you are always calling methods of other classes.

Here you invoke the static method WriteLine of the Console class.
Code:
Console.WriteLine("boo");
Here you invoke the ToString method of the Object class:
Code:
string s = obj.ToString();
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
 
Old October 27th, 2011, 01:49 AM
Authorized User
 
Join Date: Sep 2011
Posts: 24
Thanks: 7
Thanked 0 Times in 0 Posts
Default this keyword

Then whats the need for inheritance,simply make all classes public.Right?

Or
Making public is applicable in the current context only
 
Old October 27th, 2011, 01:58 AM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

There's a separate chapter that covers access modifiers in this book.
private: the member can only be accessed within the class itself
protected: the member can be accessed from the class and derived types
public: the member can be accessed from any types

Also, there are internal and protected internal access modifiers.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
 
Old October 27th, 2011, 04:05 AM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default Inheritance

Quote:
Originally Posted by arun_babu_a View Post
Then whats the need for inheritance,simply make all classes public.Right?
I've just seen that you ask what's the need for inheritance......
No, you can't get rid of inheritance by making all classes public!
If the class is declared public it can be accessed from everywhere, and it can be used as a base class. If a class is declared as internal it can only be used as a base class within the same assembly.
Deriving from a base class gives you a is-a relation, e.g. with a base class Animal and a derived class Dog. A Dog is a Animal, and deriving it from Animal it gets every member of the base class.

You should really read chapter 8 for information on inheritance.
__________________
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:
arun_babu_a (October 27th, 2011)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch10 - page 354 step 7 - No Function ken evans BOOK: Beginning ASP.NET 4 : in C# and VB 7 January 30th, 2012 07:51 PM
Page 126: missing keyword construct (implements)? WayneHeym BOOK Programming Interviews Exposed: Secrets to Landing Your Next Job 2nd Ed ISBN: 978-0-470-12167-2 0 June 4th, 2011 02:07 PM
Page 503: keyword SELECT missing dingemans BOOK: Beginning T-SQL with Microsoft SQL Server 2005 and 2008 ISBN: 978-0-470-25703-6 1 October 6th, 2009 01:12 PM
Unable to assign string length of more than 256 rahulpokharna SQL Server 2000 3 January 12th, 2006 04:58 PM
Calculator Using Postback - page 256 ukbrit Dreamweaver (all versions) 1 November 2nd, 2004 05:04 PM





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