Click here to Skip to main content
15,900,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have created a class that opens a file.

Now I want to build some security around that and check if the file exists before creating the class (in the constructor). When the file doesn't exist, the class should return a null object or something like that.

Grtz
Wannes
Posted
Comments
#realJSOP 7-Jul-10 17:28pm    
Be a programmer for god's sake.... I gave you a cursory answer. It's up to you to have the "ah ha!" moment...
Wannes Geysen 8-Jul-10 2:56am    
I think you don't understand the situation here. I want to check if the file exists en throw an exception before the constructor of the class is called.

Ok, so I started to just write out the code because that is an incredible simple thing to do, but I decided that just giving you the code would be doing you an injustice by not making you learn how to research problems.

There is a library called System.IO. This has many good classes for dealing with the file system. One of them is a class called File.

It has several methods in it, one which you would find very useful for this task.

Presumably, you've already created your constructor, so, as you said, all you need to do is check if the file exists.

I would actually not return null, but throw a new exception. That same library (System.IO) has a FileNotFoundException.

So, if the file doesn't exist, create a new FileNotFoundException and throw it.
 
Share this answer
 
Comments
Alain Bertrand 6-Jul-10 13:55pm    
Reason for my vote of 5
I don't see other options neither. Beside using a "factory" approach
It is a little more complicated than that.

The class inherits from a base class (inside a dll, so I don't have the sources). The constructor of my class calls the constructor of this base class.

for example:

public class MyClass : BaseClass
{
   public MyClass(filename) : base(filename)
   {

   }
}


When the filename in the constructor of the base class doesn't exists it throws an exception. How can I catch this exception and handle it, or throw it back as an exception of MyClass
 
Share this answer
 
Why dont you use Factory class to handle these.

You can create a Static Method like

public YourClass GetObject()
{
    if(File.Exists(filename))
    {
        return new YourClass();
    }
}


This will be a better approach. you can also use GetObject to get more types by specifying common Interface as return type.
:rose:
 
Share this answer
 
Comments
Wannes Geysen 7-Jul-10 5:27am    
But has the disadvantage that you need to call the Static Method and "new MyClass(filename)" does not work anymore?
Abhishek Sur 8-Jul-10 15:22pm    
Yes. It is not always good to create a class in such scenarios where you are in. Expose common method, that might look similar to what you might what using factory method (which is much approved way of getting object nowadsys)... and will definitely suit your requirement as well.

Before calling the constructor check for the file. After you check the file, you can send the file object to the constructor directly.
You're over-engineering your code. Create the class, and let it check for the file's existence. Set a property that indicates whether the file exists (or allow the property to actually perform the check). Then you can do something like this:

MyClass myClass = new MyClass(fileName);
if (!myClass.FileExists)
{
    myClass = null;
    return;
}
// .. do stuff with MyClass


...and then get on with the project. You've already spent too much time on this.
 
Share this answer
 
Comments
Wannes Geysen 7-Jul-10 5:57am    
This won't work because the constructor of MyClass will throw an exception when filename does not exist.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900