views:

53

answers:

3

Hello everyone!

I would like to use the same code for copying files to an FTP server, and through USB. I've already written some code, which as of now uses functions from the System.IO namespace.

To minimize code duplication, I've listed the functions which I need from the system.IO namespace, and I was thinking of designing an interface exposing similar functions, and then two classes, one for FTP file operations, and one for local ones.

I have a few questions about Interfaces though:

  1. Could design my interface so that the File.* and Directory.* organisation be preserved (so that routines declared in the interface follow the same hierarchy than in the IO namespace)?
  2. Is there a way, for local operations, to just map functions from the IO namespace to the corresponding interface functions (writing something like Sub IO.File.Delete Implements IOInterface.File_Delete)?
  3. Can I declare something like Dim IOHandler As IOInterface, assuming that IOInterface is the name of my interface? Can I then write both IOHandler = New LocalIOHandler and IOHandler = New FTPIOHandler, assuming LocalIOHandler and FTPIOHandler are my two classes implementing the IOInterface?

Thanks!
CFP.

A: 

1- For the 2nd point

You can do this aa by implementing interface explicitly.

2- For your third point

Yes , you can cast to an interface if it implemented by no of classes

saurabh
+1  A: 

You can't change an existing class so that it implements your own interface. You can however design your own interface where your methods return classes that already exist - like the ones in the System.IO namespace.

You can also implement your own class that wraps the System.IO classes so that your IOInterface.Delete maps to a FileInfo.Delete (please forgive the C# syntax, my VB is rusty):

public class MyFileClass : IOInterface
{
  public void DeleteFile(string fileName) 
  {
     new FileInfo(fileName).Delete();
  }
}

In response to #3: Yes. A variable of type IOInterface can hold any type that implements that interface. You can also later cast it to the specific type if you need to.

Rune Grimstad
Though not incorrect the downcast should be avoided and concidered a smell.
Rune FS
Agreed, but sometimes it is necessary and it is important to know that the stored object is still of the implementation type not only of the interface type.
Rune Grimstad
So people keep telling me :) but they are also almost all of them out of good examples of when. That said I Think you were right in mentioning the possibility here since it seems OP i very new to oo
Rune FS
+1  A: 

I can't help you with the first one i simply don't get what you mean with function hierachi but for the other two. No there's no syntax for that you'll have to implement a method that forwards the Call to the method. E.g. (pseudo cod I haven't written VB for years)

Sub open(path as string)
  Begin
    Return File.Open path
  End

For your 3rd you could say that that's the point (it's not the only point) of interfaces that the consuming code does not need to know the concrete type

Rune FS
I meant that in my interface I would define specify the need to have to child namespaces, File and Directory, which would hold the functions to respectively delete, create, etc; a file or a directory, as is done in the IO Namespace.
CFP
An interface resides in a namespace but can specify no limitations to which namespace declaring class lives in. Further File and Directory are _classes_ in the namespace System.IO.
Rune FS
So is there a way to have sort of subclasses in a directory?
CFP
I don't know what you mean with subclasses in a directory. There's no directories in the code (except for the class called directory). It's possible to have classes in classes
Rune FS
Woops, I'm tired. I mean, can an interface declare classes and subclasses? (So that implementations have to implement two classes, Directory And File (Then it would seem as though System.IO implements that interface)
CFP
You can _only_ declare what a class that implements the interface needs to implment when it comes to methods, properties and events. There's no way you can specify that a class needs to implement a class. The reason is simple a class cannot implement a class. It's true you can _declare a class inside another class but that's very different. Further what you describe sounds like an implementations detail and those should be kept hidden not something you require
Rune FS