0

I have three classes, the FileManagerClass contains two events which are EventHandler<FileTransferEventArgs> FileMove and EventHandler<FileTransferEventArgs> FileMoveError.

I then have a custom EventArgs class named FileTransferEventArg that inherits from the base EventArgs class. I have a third class called FolderDirectoryModel that then listens for the events to be raised and responds to it with either a OnFileMove(object sender, FileTransferEventArg arg) or OnFileMoveError(object sender, FileTransferEventArg arg).

Overall, it looks like this:

class FileTransferManager
{
    public event EventHandler<FileTransferEventArgs> FileMove;
    public event EventHandler<FileTransferEventArgs> FileMoveError;
}

public class FileTransferEventArgs : EventArgs
{
    string FileName { get; set; }
    string Message { get; set; }

    internal FileTransferEventArgs(FileModel file, string message)
    {
        this.FileName = file.FileName;
        this.Message = message;
    }
}

public class FolderDirectoryModel
{
    void TransferFile(FileModel toBeTransfered, string destinationPath)
    {
        if (!File.Exists(toBeTransfered.FilePath))
        {
            File.Move(toBeTransfered.FilePath, destinationPath);
            FileTransferEvents.FileTransferManager TransferAgent = new FileTransferEvents.FileTransferManager();
            TransferAgent.FileMove += new EventHandler<FileTransferEventArgs>(OnFileMove);                
        }
        else
        {
            FileTransferEvents.FileTransferManager TransferAgent = new FileTransferEvents.FileTransferManager();
            TransferAgent.FileMoveError += new EventHandler<FileTransferEventArgs>(OnFileMoveError);
        }
    }

    //Handler Methods
    private void OnFileMoveError(object sender, FileTransferEventArgs args)
    {
        //what i want to happen

        MessageBox.Show($"File {args.FileName} has not been moved succefully to {args.FilePath} because of....");
    }

    private void OnFileMove(object sender, FileTransferEventArgs args)
    {
        //what i want to happen
        MessageBox.Show($"File {args.FileName} has been moved successfully to {args.FilePath}
    }
}

My problem is that I would like to use the properties inside my FileTransferEventArgs class to display the file name that was moved, whether or not it was successful, and providing a reason that will be inside the message property.

However when I get every thing wired up I not able to view the data that is passed along. I am not sure if I am improperly using the events or I am trying to do something that is not possible.

Simona
  • 279
  • 1
  • 8
Recht88
  • 113
  • 1
  • 1
  • 9
  • 2
    You need to create `TransferAgent` object at class level in `FolderDirectoryModel` class. You are creating it inside constructor which will not be available outside constructor. Are you seeing any messageboxes? – Chetan Jun 21 '20 at 02:48
  • 1
    https://learn.microsoft.com/en-us/dotnet/standard/events/how-to-raise-and-consume-events – Chetan Jun 21 '20 at 02:49
  • 1
    https://riptutorial.com/csharp/example/10727/creating-custom-eventargs-containing-additional-data – Chetan Jun 21 '20 at 02:50
  • @ChetanRanpariya in the link you provided the event is declared inside of the Product class does it have to be declared in the same class in order for it to work? – Recht88 Jun 21 '20 at 14:11
  • Yes... events are associated with a class and triggered for an instance of a class. – Chetan Jun 21 '20 at 14:25

1 Answers1

0

The FileMove and FileMoveError events must be placed in the FolderDirectoryModel class.

Events are triggered in the same class. Using the Invoke method.

public class FileTransferEventArgs : EventArgs
{
    public string FileName { get; }
    public string FilePath { get; }
    public string Message { get; }

    internal FileTransferEventArgs(FileModel file, string message)
    {
        FileName = file.FileName;
        FilePath = file.FilePath;
        Message = message;
    }
}

public class FileModel
{
    public string FileName { get; set; }
    public string FilePath { get; set; }
}

public class FolderDirectoryModel
{
    public event EventHandler<FileTransferEventArgs> FileMove;
    public event EventHandler<FileTransferEventArgs> FileMoveError;

    public void TransferFile(FileModel fileModel, string destinationPath)
    {
        if (!File.Exists(fileModel.FilePath))
        {
            File.Move(fileModel.FilePath, destinationPath);
            var fileTransferEventArgs = new FileTransferEventArgs(fileModel, "Success");
            FileMove?.Invoke(this, fileTransferEventArgs);
        }
        else
        {
            var fileTransferEventArgs = new FileTransferEventArgs(fileModel, "Cannot move");
            FileMoveError?.Invoke(this, fileTransferEventArgs);
        }
    }
}

Usage

var fileModel = new FileModel { FileName = "name", FilePath = "path" };

var folderDirectoryModel = new FolderDirectoryModel();
folderDirectoryModel.FileMove += FolderDirectoryModel_FileMove;
folderDirectoryModel.FileMoveError += FolderDirectoryModel_FileMoveError;

folderDirectoryModel.TransferFile(fileModel, "destPath");

void FolderDirectoryModel_FileMove(object sender, FileTransferEventArgs args)
{
    MessageBox.Show($"File {args.FileName} has been moved successfully to {args.FilePath}");
}

void FolderDirectoryModel_FileMoveError(object sender, FileTransferEventArgs args)
{
    MessageBox.Show($"File {args.FileName} has not been moved to {args.FilePath} because of {args.Message}");
}

Event handlers are located in the same code where the FolderDirectoryModel instance is created and used.

Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49