SeanKilleen
SeanKilleen

Reputation: 8977

Best way to implement calling a constructor from another constructor?

The Setup

I imagined it would look something like:

public MyFileClass(FileInfo info)
{
    //do things 
}

public MyFileClass (string url, string Description)
{
    // var tempfile = FileInfo that results from creating file 
    MyFileClass(tempfile)
}

Question

What is the proper way to implement this idea? I've got the file creation part down, just not quite sure of the syntax for making it work in this way.

Upvotes: 1

Views: 48

Answers (1)

Michael
Michael

Reputation: 1833

You could implement a static method to create the FileInfo.

public class MyFileClass
{
    public MyFileClass(FileInfo info)
    {
        // do work
    }

    public MyFileClass(string url, string description)
        : this(GetFileInfo(url, description))
    { 
        // do more work
    }

    static FileInfo GetFileInfo(string url, string description)
    {
        return new FileInfo();
    }
}

Upvotes: 3

Related Questions