-1

I have method e.g.

public async Task<int> AddUser() 
{
   //some logic
    await _externalClass.DoSomething();
   //some logic
} 

And I want to get value from method await _externalClass.DoSomething(); (return type is Task and return type cannot be changed by me)

To make code like that:

public async Task<int> AddUser() 
{
   //some logic
    await _externalClass.DoSomething();
    if(valueThatIWantToGetFromExternalMethod) 
    {
       //some logic
    }
   //some logic
} 

How can I make this value from external method (this should be bool)valueThatIWantToGetFromExternalMethod ?

I thought that I can use ref or out keyword, but we can't do this in async methods in C#. Can someone show me how to implement this kind of logic?

dimitri
  • 109
  • 6
  • Change `public int AddUser()` to `public async Task AddUserAsync()`. Now you can use `await` in your method, example `var valueThatIWantToGetFromExternalMethod = await _externalClass.DoSomethingAsync();` – Igor Apr 05 '22 at 11:35
  • 1
    So you can change the parameters to the function but not the return type? – Magnus Apr 05 '22 at 11:37
  • @Igor but ```externalClass.DoSomethingAsync();``` is void method – dimitri Apr 05 '22 at 11:37
  • 2
    If it returns `void` then it can't be awaited either so `await` is pointless. – Igor Apr 05 '22 at 11:38
  • 3
    I don't think there's enough information here. If a method in some class you can't change doesn't return anything, what value are you wanting from it? It sounds like something you need to take up with the authors of whatever library this is. – Charles Mager Apr 05 '22 at 11:39
  • @Igor oh sorry, I mean Task – dimitri Apr 05 '22 at 11:39
  • Why cant you change the return value to Task for example? – Magnus Apr 05 '22 at 11:41
  • So that would indicate the external method does not return a result *after* it has been awaited. Does the instance have state that changes after the method is called? – Igor Apr 05 '22 at 11:42
  • @Igor no, it just 'SaveChanges' somewhere – dimitri Apr 05 '22 at 11:43
  • 2
    So where does it save the changes? We're missing a *lot* of context here... (You're mentioning `ref` and `out` parameters as if you can change the method signature, but apparently you can't change the return type to something more useful? That's very odd...) – Jon Skeet Apr 05 '22 at 11:45
  • @JonSkeet this is no matter, I just need one value from external class, and if there was possibillity to use ref or out I would use it, but there is no, cause this is async, and I'm looking for some alternatives. – dimitri Apr 05 '22 at 11:47
  • @JonSkeet I know that I can change type, it's simple. but I dont want to do that, that is the reason why I'm here – dimitri Apr 05 '22 at 11:48
  • @dimitri but you've not provided enough information or context for anyone to provide a solution – Charles Mager Apr 05 '22 at 11:49
  • @CharlesMager what kind of context do you mean? What do you need? – dimitri Apr 05 '22 at 11:50
  • Where's this value? Is it stored somewhere? Can it be accessed via e.g. a property on the class? You say you can't change there return type, but you can change the parameters - so could you expose this value as a property if it isn't already? – Charles Mager Apr 05 '22 at 11:51
  • @CharlesMager method firstly check current status somewhere and I need this current status, and then method save changes. – dimitri Apr 05 '22 at 11:52
  • @CharlesMager I can change method inside, but just wan't to change return type – dimitri Apr 05 '22 at 11:53
  • Then add a `CurrentStatus` property to the class and read that after it's updated? – Charles Mager Apr 05 '22 at 11:53
  • @CharlesMager I want to use some alternative to ref or out, e.g. delegate, func, or something like that, but don't know correct way to do that – dimitri Apr 05 '22 at 11:54
  • 4
    So what you're saying is that you *can* use the entirely natural and idiomatic way of doing things (change the return type) but you want to do things in an awkward way instead - but without providing us any information about *why* you want to use a roundabout approach. I find it hard to see how this is a useful question. – Jon Skeet Apr 05 '22 at 12:00
  • 2
    You can pass a function to do something with the result (per [this answer](https://stackoverflow.com/a/58158597/1320845) approach 2), but as @JonSkeet says this is a very unusual approach that would confuse anyone who read it later. Just change the return type? – Charles Mager Apr 05 '22 at 12:02
  • @JonSkeet it will not help you if I will say the reason why I don't want to change return type, I'm just looking for some alternatives and that's all – dimitri Apr 05 '22 at 12:02
  • 2
    Tell us, perhaps it will help us (and you). – Magnus Apr 05 '22 at 12:24
  • Let's say that `DoSomething` was a synchronous method with a `void` return type, how would you solve the problem in this case? You could update the question with a solution for the synchronous version, and ask for a solution for the asynchronous version. You should also change the title of the question, because *"void async method"* is not what you are working with (based on your comments). – Theodor Zoulias Apr 05 '22 at 15:00
  • Related: [How to write an async method with out parameter?](https://stackoverflow.com/questions/18716928/how-to-write-an-async-method-with-out-parameter) – Theodor Zoulias Apr 05 '22 at 16:12

1 Answers1

1

You can pass a reference object into the method. Then changes made to the object will be available for you inside the AddUser scope.

string or class is a reference type.

  • That is if you cant return a Task from your DoSomething method
Sebastian
  • 64
  • 1
  • 7