Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return multiple values from a C# asynchronous method

I have worked with asynchronous methods and the methods which return multiple values, separately. In this specific situation, following "GetTaskTypeAndId()" method should be asynchronous and should return multiple values at the same time. How should I make this method asynchronous?

        public async Task DeleteSchoolTask(int schoolNumber, int taskDetailId)         {               var result = GetTaskTypeAndId(taskDetailId); // This should be called asynchronously             int taskId = result.Item1;             string taskType = result.Item2;              // step 1: delete attachment physically from server             var fileService = new FileService(Logger, CurrentUser);             var relativeFilePath = $"{schoolNumber}\\{Consts.RM_SCHOOL}\\{taskDetailId}";             fileService.DeleteAttachmentFolderFromServer(Consts.CONFIG_SMP_UPLOADFILE_ROOTPATH, relativeFilePath);              // step 2: delete records from database             await _routineMaintenanceRepo.Value.DeleteSchoolTask(taskDetailId);         }           public (int, string) GetTaskTypeAndId(int taskDetailId) // How should I write this line using 'async Task'?         {             var detailRecord = await _routineMaintenanceRepo.Value.GetDetailRecord(taskDetailId);              int taskId = 0;             string taskType = "";              switch (detailRecord.TaskType)             {                 case 1:                     taskId = (int)detailRecord.RoutineMaintenanceTaskId;                     taskType = Consts.RM_DEFAULT;                     break;                 case 2:                     taskId = (int)detailRecord.RoutineMaintenanceTaskDuplicateId;                     taskType = Consts.RM_DUPLICATE;                     break;                 case 3:                     taskId = (int)detailRecord.RoutineMaintenanceTaskSchoolId;                     taskType = Consts.RM_SCHOOL;                     break;                 default:                     break;             }             return (taskId, taskType);         } 
like image 300
Kushan Randima Avatar asked Sep 20 '18 04:09

Kushan Randima


People also ask

How do we return multiple values from AC function?

We can return more than one values from a function by using the method called “call by address”, or “call by reference”. In the invoker function, we will use two variables to store the results, and the function will take pointer type data.

Can you return multiple values?

You can return multiple values from a function using either a dictionary, a tuple, or a list. These data types all let you store multiple values.

Can you return multiple values from a function in C #?

New programmers are usually in the search of ways to return multiple values from a function. Unfortunately, C and C++ do not allow this directly.

How many values can a function return at a time in C?

We all know that a function in C can return only one value.


1 Answers

After playing with the code I was able to figure it out. Here is the solution.

        public async Task DeleteSchoolTask(int schoolNumber, int taskDetailId)         {               var result = await GetTaskTypeAndId(taskDetailId);             int taskId = result.Item1;             string taskType = result.Item2;              // step 1: delete attachment physically from server             var fileService = new FileService(Logger, CurrentUser);             var relativeFilePath = $"{schoolNumber}\\{Consts.RM_SCHOOL}\\{taskDetailId}";             fileService.DeleteAttachmentFolderFromServer(Consts.CONFIG_SMP_UPLOADFILE_ROOTPATH, relativeFilePath);              // step 2: delete records from database             await _routineMaintenanceRepo.Value.DeleteSchoolTask(taskDetailId);         }          public async Task<(int, string)> GetTaskTypeAndId(int taskDetailId)         {             var detailRecord = await _routineMaintenanceRepo.Value.GetDetailRecord(taskDetailId);              int taskId = 0;             string taskType = "";              switch (detailRecord.TaskType)             {                 case 1:                     taskId = (int)detailRecord.RoutineMaintenanceTaskId;                     taskType = Consts.RM_DEFAULT;                     break;                 case 2:                     taskId = (int)detailRecord.RoutineMaintenanceTaskDuplicateId;                     taskType = Consts.RM_DUPLICATE;                     break;                 case 3:                     taskId = (int)detailRecord.RoutineMaintenanceTaskSchoolId;                     taskType = Consts.RM_SCHOOL;                     break;                 default:                     break;             }             return (taskId, taskType);         } 
like image 137
Kushan Randima Avatar answered Sep 17 '22 17:09

Kushan Randima