PC Review


Reply
Thread Tools Rate Thread

Add custom method to File Class

 
 
joecool1969@live.com
Guest
Posts: n/a
 
      17th Apr 2009
I would like to create a class that inherits the File Class so I can
add a method to it. Then my class will have all of the functionality
of the original File class with the addition of my custom method.

But it appears that the File Class is a static class and you cannot
inherit from a static class.

Is there some way this can be done?
 
Reply With Quote
 
 
 
 
Alberto Poblacion
Guest
Posts: n/a
 
      17th Apr 2009
<(E-Mail Removed)> wrote in message
news:991a5a9a-9a69-4fc3-a142-(E-Mail Removed)...
>I would like to create a class that inherits the File Class so I can
> add a method to it. Then my class will have all of the functionality
> of the original File class with the addition of my custom method.
>
> But it appears that the File Class is a static class and you cannot
> inherit from a static class.
>
> Is there some way this can be done?


Instead of inheriting, you could encapsulate the class inside your own.
In other words, write your own class, declare a File inside your class, and
add your method inside your class. You can either expose the File itself
through a public property of your class, or add methods in your class to
call the methods in the encapsulated File.

Alternatively, you could use the FileInfo class instead of the File
class. FileInfo provides just about the same functionality but uses instance
methods instead of static methods. You can still not inherit from it because
it is a sealed class, but you can add an Extension method (if you are using
C# 3.0), which may be adequate for your needs.

 
Reply With Quote
 
Paul Shapiro
Guest
Posts: n/a
 
      17th Apr 2009
Lookup "extension methods" in the online help. You can add methods to an
existing .Net class without inheritance.

<(E-Mail Removed)> wrote in message
news:991a5a9a-9a69-4fc3-a142-(E-Mail Removed)...
>I would like to create a class that inherits the File Class so I can
> add a method to it. Then my class will have all of the functionality
> of the original File class with the addition of my custom method.
>
> But it appears that the File Class is a static class and you cannot
> inherit from a static class.
>
> Is there some way this can be done?


 
Reply With Quote
 
Alberto Poblacion
Guest
Posts: n/a
 
      17th Apr 2009
"Paul Shapiro" <(E-Mail Removed)> wrote in message
news:ul4%23K$(E-Mail Removed)...
> Lookup "extension methods" in the online help. You can add methods to an
> existing .Net class without inheritance.


Can you use extension methods on a static class? I thought that you
always had to invoke the extension method on an instance of the class.

 
Reply With Quote
 
joecool1969@live.com
Guest
Posts: n/a
 
      17th Apr 2009
On Apr 17, 11:16*am, "Alberto Poblacion" <earthling-
quitaestoparacontes...@poblacion.org> wrote:
> <joecool1...@live.com> wrote in message
>
> news:991a5a9a-9a69-4fc3-a142-(E-Mail Removed)...
>
> >I would like to create a class that inherits the File Class so I can
> > add a method to it. Then my class will have all of the functionality
> > of the original File class with the addition of my custom method.

>
> > But it appears that the File Class is a static class and you cannot
> > inherit from a static class.

>
> > Is there some way this can be done?

>
> * * Instead of inheriting, you could encapsulate the class inside your own.
> In other words, write your own class, declare a File inside your class, and
> add your method inside your class. You can either expose the File itself
> through a public property of your class, or add methods in your class to
> call the methods in the encapsulated File.
>
> * * Alternatively, you could use the FileInfo class instead of the File
> class. FileInfo provides just about the same functionality but uses instance
> methods instead of static methods. You can still not inherit from it because
> it is a sealed class, but you can add an Extension method (if you are using
> C# 3.0), which may be adequate for your needs.


Thanks for the replies.

Specifically I woud like a method that takes two parameters, paths to
two files, and returns true if the files are identical or false if
there are any differences. Like File.Compare(path1, path2).
 
Reply With Quote
 
Dude
Guest
Posts: n/a
 
      17th Apr 2009
On Apr 17, 11:16*am, "Alberto Poblacion" <earthling-
quitaestoparacontes...@poblacion.org> wrote:
> <joecool1...@live.com> wrote in message
>
> news:991a5a9a-9a69-4fc3-a142-(E-Mail Removed)...
>
> >I would like to create a class that inherits the File Class so I can
> > add a method to it. Then my class will have all of the functionality
> > of the original File class with the addition of my custom method.

>
> > But it appears that the File Class is a static class and you cannot
> > inherit from a static class.

>
> > Is there some way this can be done?

>
> * * Instead of inheriting, you could encapsulate the class inside your own.
> In other words, write your own class, declare a File inside your class, and
> add your method inside your class. You can either expose the File itself
> through a public property of your class, or add methods in your class to
> call the methods in the encapsulated File.
>
> * * Alternatively, you could use the FileInfo class instead of the File
> class. FileInfo provides just about the same functionality but uses instance
> methods instead of static methods. You can still not inherit from it because
> it is a sealed class, but you can add an Extension method (if you are using
> C# 3.0), which may be adequate for your needs.


Since File is a Static class, I don't think you can extend it. I
think you are SOL here.
I agree with the suggestion to just extent FileInfo instead.
 
Reply With Quote
 
Dude
Guest
Posts: n/a
 
      17th Apr 2009
On Apr 17, 9:15*am, joecool1...@live.com wrote:
> I would like to create a class that inherits the File Class so I can
> add a method to it. Then my class will have all of the functionality
> of the original File class with the addition of my custom method.
>
> But it appears that the File Class is a static class and you cannot
> inherit from a static class.
>
> Is there some way this can be done?


I think your only option here (If you really want to do this) is
simply to create a new static class, and re-create the methods on
File. You could call

public static class MyFile
{
public static void AppendAllText(string path, string contents)
{
System.IO.File.AppendAllText(path, contents);
}
...
public static void MyMethod()
{
...
}
}

I just don't know that I see the reasoning behind this. What the
difference between calling

System.IO.File.MyMethod()
vs
Calling MyFile.MyMethod();

If it were possible, your new method would need to be static anyway.
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      20th Apr 2009
(E-Mail Removed) wrote:
> I would like to create a class that inherits the File Class so I can
> add a method to it. Then my class will have all of the functionality
> of the original File class with the addition of my custom method.
>
> But it appears that the File Class is a static class and you cannot
> inherit from a static class.
>
> Is there some way this can be done?


No.

And there are not really any point.

Since all the methods are static just make your MyFile class
and use File.Foo and MyFile.Bar - I see the different names
as an advantage.

Arne
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
call a mnuFileNew_Click method from a Main MDI class from another class (can be a MdiChild class.. )? M. G, Microsoft Dot NET Framework Forms 1 31st May 2006 06:28 AM
Custom Control - how to call another class's validation method Neville Lang Microsoft Dot NET Compact Framework 2 18th Aug 2005 01:34 PM
Specify which method is default in custom class =?Utf-8?B?QmluZw==?= Microsoft Excel Programming 1 23rd Dec 2004 10:33 PM
How to create new Custom method/Function wizard for your class? Gautham Jayaraman Microsoft VC .NET 0 23rd Aug 2004 07:59 AM
How to create new Custom method/Function wizard for your class? Gautham Jayaraman Microsoft Dot NET 0 23rd Aug 2004 07:58 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:41 PM.