Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 4.0 aka C# 2010 > BOOK: Beginning Visual C# 2010
|
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 August 8th, 2012, 03:20 PM
Authorized User
 
Join Date: Jul 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Question Chapter 21, Reading Dataa, ReadFile, GetDecoder()

Just when I think I'm beginning to understand OOP something confuses me. On page 693, ReadFile code snippet, I don't understand what is going on with the line

Decoder d = Encoding.UTF8.GetDecoder();

Encoding class is abstract which means it can't be instantiated. It contains members that are static, virtual, or abstract. Virtual members have implementation but may be overriden by inheriting classes, Abstract classes have abstract members which may have no implementation and by default are virtual. I have to supply the implementation. I beleve this to be correct so far.

My question is this. I can't instatiiate Encoding since it is abstract. When I initailly type Encoding, the intellisense does not even show GetDecoder() method until I type UTF8 property. Only when I type UTF8 does the intellisense give me access to GetDecoder() which is virtual. GetDecoder() returns a Decoder. How come I can't just use Encoding.GetDecoder()? Both UTF8 property and GetDeocder() method are public. Am I missing some critical understanding of OOP?
 
Old August 12th, 2012, 05:54 AM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

You nearly found the answer yourself ;-)

GetDecoder() is an instance method, that's why you need an instance of the Decoder class to invoke the method.
The UTF8 property returns an instance of Decoder (or a class that derives from Decoder).
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
 
Old August 12th, 2012, 10:33 PM
Authorized User
 
Join Date: Jul 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Instance method means it is not a static method, correct? If true GetDecoder() is a public virtual method that returns a System.Text.Decoder. The book never has us instantiate an instance of the Encoding class to access GetDecoder(). UTF8 is a static property that returns a System.Text.Encoding. Typing Encoding.UTF8 makes sense since UTF8 is static. Still not sure why GetDecoder method becomes available only after typing UTF8. I should never see GetDecoder since it is not static. UTF8 returning Encoding is almost like a circular reference.

Can you clarify more please. I just started reading Professional C# 4 and .NET.

Thanks.

Mike
 
Old August 13th, 2012, 01:09 AM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

Quote:
Originally Posted by mhanson View Post
Instance method means it is not a static method, correct? If true GetDecoder() is a public virtual method that returns a System.Text.Decoder. The book never has us instantiate an instance of the Encoding class to access GetDecoder(). UTF8 is a static property that returns a System.Text.Encoding. Typing Encoding.UTF8 makes sense since UTF8 is static. Still not sure why GetDecoder method becomes available only after typing UTF8. I should never see GetDecoder since it is not static. UTF8 returning Encoding is almost like a circular reference.

Can you clarify more please. I just started reading Professional C# 4 and .NET.

Thanks.

Mike
Yes, instance method is not a static method.

Let's split up the sample in multiple lines.
The UTF8 property returns an instance of the Encoding type. You don't have to do this on your own. More exact, UTF8 creates an instance of the UTF8Encoding class that derives from the base class Encoding. Here the UTF8Encoding instance is assigned to the variable e.
Code:
Encoding e = Encoding.UTF8;
Now you have an instance and can invoke instance members.
Code:
Decoder d = e.GetDecoder();
I hope this helps.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
 
Old August 15th, 2012, 03:20 PM
Authorized User
 
Join Date: Jul 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the assistance in understanding this problem.

I went back and looked at the MSDN documentation on Encoding.UTF8 property (http://msdn.microsoft.com/en-us/libr...ding.utf8.aspx). In the code it displays:

public static Encoding UTF8 { get; }

which means it is returning an Encoding object (class). But the text a few lines below says it returns a UTF8Encoding object (class). If it is returning a UTF8Encoding object this makes more sense. I wrote a small program and created the line

var d = Encoding.UTF8;

I paused the program is ran the cursor over d which is listed as UTF8Encoding object. The GetDecoder() method in the UTF8Encoding object (class) which is public and overridden. Is this correct?

I'm assuming the MSDN documentation is incorrect, UTF8 property is returning a UTF8ENcoding object (class), not a Encoding object (class). Is this correct?

In trying to make sure I understand this so I replaced the line

Decoder d = Encoding.UTF8.GetDecoder();


with

Decoder d = UTF8Encoding.UTF8.GetDecoder();


and got the same results. I'm I correct in my understanding?


Thanks again for the help.
 
Old August 20th, 2012, 02:22 AM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

It's returning an object of type UTF8Encoding, that's correct - as I've mentioned already. The MSDN documentation is still correct.

The UTF8 property is declared of type Encoding. Of course, an object of this type cannot be returned as the Encoding class is abstract. So the UTF8 property needs to return an object of a type that derives from the Encoding class, and this is UTF8Encoding. This is an implementation detail of the UTF8 property. All you need to know is it returns something that derives from Encoding, and thus fulfills the contract of the Encoding type, offering all properties and methods.

Such a property or method could return different types at runtime. For example, WebRequest.Create is declared to return WebRequest. WebRequest is abstract, and the Create method returns HttpWebRequest, FtpWebRequest, FileWebRequest, or a custom xxrequest depending on the string that is passed to the Create method.

Hope this helps.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 21 Try it out,page 718 dampyr BOOK: Beginning Visual C# 2010 2 December 15th, 2011 06:21 AM
Chapter 21 Try it out,page 693 dampyr BOOK: Beginning Visual C# 2010 0 December 11th, 2011 09:44 AM





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