textwriter Class Issues

jtokach

New member
Joined
Dec 13, 2006
Messages
3
Programming Experience
Beginner
Hi,
I'm developing this as a console app, but that shouldn't matter. I'm new to classes, so this is probably something simple. I want to create a class that uses the TextWriter Class. I thought that'd be simple enough, but when I try to create the instance with the New keyword, I keep getting the message 'New' cannot be used on a class that is declared 'MustInherit'

MSDN says to take out the New keyword, but then how do use the TextWriter class???

VB.NET:
[SIZE=2][COLOR=#0000ff]
Imports[/COLOR][/SIZE][SIZE=2] System
[/SIZE][SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System.IO
[/SIZE][SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System.Xml
[/SIZE][SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System.Xml.XPath
[/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] MIFWriter
[/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Shared[/COLOR][/SIZE][SIZE=2] Indentation [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Boolean
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Shared[/COLOR][/SIZE][SIZE=2] strfileName [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] txtW [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] TextWriter
[/SIZE][SIZE=2][COLOR=#0000ff]Shared[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Property[/COLOR][/SIZE][SIZE=2] Indent() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Boolean
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Get
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] (Indentation)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Get
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Set[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] Value [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Boolean[/COLOR][/SIZE][SIZE=2])
Indentation = Value
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Set
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Property
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] fileName [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])
strfileName = fileName
txtW = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] TextWriter(strfileName)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] Close()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Function
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] WriteStartComponent()
Console.WriteLine("Name: " & strfileName)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Function
End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Class
[/COLOR][/SIZE]
 
John,

Changing the instantiation to "New StreamWriter" worked, although I don't understand why. The tone of your reply implies that this is something basic that I should know. Can you elaborate or point me somewhere?

Also, should my pasted code be that butchered? I followed the links in your sig., but saw nothing other than adding
VB.NET:
 tags which are already there.
 
Thanks,
 
Jim
 
No, previous post was simply directing you to the TextWriter class documentation that explained some about this. (did you read the class remarks?). You can also follow links to mentioned classes and more info about them there. Here is the MustInherit explanation about base/abstract classes: http://msdn2.microsoft.com/en-us/library/aee8f02w.aspx
 
Basically the testwriter class is a simple class developed to use as a base class only. This happens sometimes when developers have generic routines that serve many purposes but require specific wrappers. Therefore, the StreamWriter and StringWriter classes wrap the routines in the textwriter class and use them in special ways. If this helps let me know, if not e-mail or Private message for details.
 
ImDaFrEaK;
Okay, thing are a bit clearer, but I still don't understand why I can't use the base class, but can use the wrapper class. Perhaps I don't understand or misinterpret what base classes are?
 
Read about Inheriting from Objects or Classes (both the same). I just wrote like a freakin essay for you and then hit something on my keyboard and lost it and I don't want to type it again. Maybe when I get the chance I will explain it more. Just e-mail me and I'll walk you through it slowly or hit me up on Y! messenger. Within 30 minutes to an hour I think I will have you understanding what a base class is. I'll have you build custom classes and inherit from them ect and let you see the results from it. Learning Inheritance believe it or not will help you understand your programming cycles much better and most definately increase your programming flexability. Expecially Form Inheritance which I use a lot.
 
It isn't a "base" class issue, it is an "abstract" class issue, which is a special case. TextWriter is the base and also for reasons declared MustInherit and the documentation explains that this mean the class is not allowed to be instantiated, that can be done only with inherited class.
 
I understand that but I am also trying to explain the situation in a way that he/she will understand. I don't think this person has a clear understanding of inheritance and if not they won'y understand what you're saying. But I am with you on this.
 
Back
Top