Setting Objects Equal to an Object of Base Class

G

Guest

I am extending FileSystemInfo class so that I can Implement Icomparable. My
custom class looks like:

Class FSFileSystemInfo
Inherits FileSystemInfo : Implements Icomparable
Function CompareTo(ByVal o As Object) As Integer _
Implements IComparable.CompareTo

My issue comes when I try to set an instance of FSFileSystemInfo to an
object that is FileSystemInfo type. Such as:

Dim DirInfo As New DirectoryInfo("./")
Dim FSInfo() As FSFileSystemInfo
DirInfo = New DirectoryInfo(Server.MapPath(".\"))
FSInfo = DirInfo.GetFileSystemInfos("*-*.pdf") //THIS FAILS

I get a Cast Error when I do the previous statment and I am unsure how to
cast one object into another within VB.NET. Thanks for any help in advance.
 
S

Stephany Young

The DirInfo.GetFileSystemInfos method is returning an object of the
FileSystemInfo type whereas you are attempting to assign it to a vaiable of
the FSFileSystemInfo
type. You need to convert the FileSystemInfo object to a FSFileSystemInfo
object.

Try:

FSInfo = CType(DirInfo.GetFileSystemInfos("*-*.pdf"), FSFileSystemInfo)
 
G

Guest

Thanks Stephany,

I tried the following and still get an error.
FSInfo = CType(DirInfo.GetFileSystemInfos("*-*.pdf"), FSFileSystemInfo())
Since FSInfo is an array I assume I will have to convert the array of
FileSystemInfo objects returned by GetFileSystemInfos into an array of
FSFileSystemInfo Objects. However this still seems to yeild

Unable to cast object of type 'System.IO.FileSystemInfo[]'
to type 'FSFileSystemInfo[]'.
 
S

Stephany Young

Sorry ... Missed the array and ended up putting you crook.

I'm going to have to ask you at this stage, what sort of operations are you
going to be carrying out with your FSFileSystemInfo class.

The reason I ask is that implementing your class is going to be fraught with
difficulty for reasons that include:

- FileSystemInfo is the base class for the DirectoryInfo and
FileInfo classes.

- DirectoryInfo.GetFileSystemInfos returns an array that can be
a mixture of DirectoryInfo and FileInfo objects.

- You will not be able to deal with the returned DirectoryInfo and
FileInfo objects unless you create your own classes that derive
from FSFileSystemInfo.

If you are looking to get a list of files in a directory that match a given
pattern and sort them, then you do not need to go to all this trouble.

Instead you can use the shared Directory.GetFiles(string, string) overloaded
method to return a string array of the matching files and then use one of
the shared Array.Sort overloaded methods to sort the aray, with or woithout
an IComparer as required.


FacultasNetDeveloper said:
Thanks Stephany,

I tried the following and still get an error.
FSInfo = CType(DirInfo.GetFileSystemInfos("*-*.pdf"), FSFileSystemInfo())
Since FSInfo is an array I assume I will have to convert the array of
FileSystemInfo objects returned by GetFileSystemInfos into an array of
FSFileSystemInfo Objects. However this still seems to yeild

Unable to cast object of type 'System.IO.FileSystemInfo[]'
to type 'FSFileSystemInfo[]'.

Stephany Young said:
The DirInfo.GetFileSystemInfos method is returning an object of the
FileSystemInfo type whereas you are attempting to assign it to a vaiable
of
the FSFileSystemInfo
type. You need to convert the FileSystemInfo object to a FSFileSystemInfo
object.

Try:

FSInfo = CType(DirInfo.GetFileSystemInfos("*-*.pdf"), FSFileSystemInfo)
 
G

Guest

That's it. I just got off on a wrong foot. thanks!

Stephany Young said:
Sorry ... Missed the array and ended up putting you crook.

I'm going to have to ask you at this stage, what sort of operations are you
going to be carrying out with your FSFileSystemInfo class.

The reason I ask is that implementing your class is going to be fraught with
difficulty for reasons that include:

- FileSystemInfo is the base class for the DirectoryInfo and
FileInfo classes.

- DirectoryInfo.GetFileSystemInfos returns an array that can be
a mixture of DirectoryInfo and FileInfo objects.

- You will not be able to deal with the returned DirectoryInfo and
FileInfo objects unless you create your own classes that derive
from FSFileSystemInfo.

If you are looking to get a list of files in a directory that match a given
pattern and sort them, then you do not need to go to all this trouble.

Instead you can use the shared Directory.GetFiles(string, string) overloaded
method to return a string array of the matching files and then use one of
the shared Array.Sort overloaded methods to sort the aray, with or woithout
an IComparer as required.


FacultasNetDeveloper said:
Thanks Stephany,

I tried the following and still get an error.
FSInfo = CType(DirInfo.GetFileSystemInfos("*-*.pdf"), FSFileSystemInfo())
Since FSInfo is an array I assume I will have to convert the array of
FileSystemInfo objects returned by GetFileSystemInfos into an array of
FSFileSystemInfo Objects. However this still seems to yeild

Unable to cast object of type 'System.IO.FileSystemInfo[]'
to type 'FSFileSystemInfo[]'.

Stephany Young said:
The DirInfo.GetFileSystemInfos method is returning an object of the
FileSystemInfo type whereas you are attempting to assign it to a vaiable
of
the FSFileSystemInfo
type. You need to convert the FileSystemInfo object to a FSFileSystemInfo
object.

Try:

FSInfo = CType(DirInfo.GetFileSystemInfos("*-*.pdf"), FSFileSystemInfo)


I am extending FileSystemInfo class so that I can Implement Icomparable.
My
custom class looks like:

Class FSFileSystemInfo
Inherits FileSystemInfo : Implements Icomparable
Function CompareTo(ByVal o As Object) As Integer _
Implements IComparable.CompareTo

My issue comes when I try to set an instance of FSFileSystemInfo to an
object that is FileSystemInfo type. Such as:

Dim DirInfo As New DirectoryInfo("./")
Dim FSInfo() As FSFileSystemInfo
DirInfo = New DirectoryInfo(Server.MapPath(".\"))
FSInfo = DirInfo.GetFileSystemInfos("*-*.pdf") //THIS FAILS

I get a Cast Error when I do the previous statment and I am unsure how
to
cast one object into another within VB.NET. Thanks for any help in
advance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top