Ref parameters and async task

First of all, I understand that the ref, in and out parameters are forbidden in asynchronous tasks.
However, as I have this need, I’m trying to find the cleanest, simplest and most effective solution.
And unintentionally I’ve created a case that seems to work (I realised later). But I need your help to better understand why this case works as opposed to a simple string.
I’m in C# / WPF and I’m trying to apply the MVVM pattern.

This is what is called from the ViewModel

await _NetworkUtil.ExecuteNetworkTask(NetworkStep1,NetworkStep2, DomainFQDN, 1000, 4, Gateway, IPAddress, SubNetMask, DNSSrvIPAddress, DomainController);

This is the method called from a utility class

public async Task ExecuteNetworkTask(Result networkStep1, Result networkStep2, string domaineFqdn, int timeout, int nbPing,string gateway, string ipAddress, string subnetMask, string dnsSrvIPAddress, string domainController)

I need to update the parameters NetworkStep1, NetworkStep2, DomainController
Now for some important details. “Result” networkStep1 / networkStep2 is a custom class

    public class Result
{
    private bool _success;
    public bool Success {
        get { return _success; } 
        set { _success = value; } }

    private StringBuilder _message;
    public StringBuilder Message
    {
        get { return _message; }
        set
        {
            _message = value;
           
        }
    }

    private StringBuilder _exception;
    public StringBuilder Exception
    {
        get { return _exception; }
        set
        {
            _exception = value;

        }
    }

    public Result()
    {
        _message = new StringBuilder();
        Exception = new StringBuilder();
    }

    public void UpdateLog(string message)
    {
        Message.AppendLine(message);
        OnLogUpdated(message);
    }

    public void UpdateEx(string message)
    {
        Exception.AppendLine(message);
        OnLogUpdated(message);
    }

    public event Action<string> LogUpdated;
    protected virtual void OnLogUpdated(string updatedLog)
    {
        LogUpdated?.Invoke(updatedLog);
    }
}

This is how the Result is initialised before the task is called (in ViewModel)

NetworkStep1 = new Result();
NetworkStep1.LogUpdated += UpdateTextBoxLog;
NetworkStep2 = new Result();
NetworkStep2.LogUpdated += UpdateTextBoxLog;

await _NetworkUtil.ExecuteNetworkTask(NetworkStep1,NetworkStep2, DomainFQDN, 1000, 4, Gateway, IPAddress, SubNetMask, DNSSrvIPAddress, DomainController);

The .LogUpdated += UpdateTextBoxLog is used to update a log window in real time when a Result is updated.
Example within the ExecuteNetworkTask :

networkStep1.UpdateLog(‘Connected to network’)

These are the declarations in ViewModel for NetworkStep1/2. “Session” is an instantiation of the M_Session Model.

    public Result NetworkStep1
    {
        get { return Session.NetworkEtape1; }
        set
        {
            Session.NetworkEtape1 = value;
            OnPropertyChanged(nameof(NetworkStep1));
        }
    }

public string DomainController
{
    get { return Session.DomainController; }
    set
    {
        Session.DomainController = value;
        OnPropertyChanged(nameof(DomainController));
    }
}

That’s it for the summary, in the hope that it’s not too confusing and at the same time sufficiently complete.
To summarise now, I need to update the ViewModel’s DomainControler property via the ExecuteNetworkTask async task. I could, of course, set it as a return, but as I could potentially have several properties to update, I thought it would be more practical to set them as parameters. As the DomainController update didn’t work, I finally realised that I couldn’t use a REF in an async task.
BUT … surprisingly the result class networkStep1 and 2 update very well! Since the log is updated in real time and the control to move on works well.

if(NetworkStep1.Success && NetworkStep2.Success)
{
    CurrentStepView = new UserAuthentification();
}

Basically, I didn’t create the Result class for this purpose at all, but the way I’m using it, it seems (unless it’s a big illusion) that the way I’m using it allows it to be updated within an async task.
Could you help me understand how? So that I can update the DomainController.
Thank you all for your help

2

The reasons the Result class is updating and DomainController is not is because Result is a standard reference type and DomainController is a string, which is immutable.

Standard reference types pass references to specified parameter value to methods whereas value types (structs and primitive types) pass copies of specified parameter values to methods. This means that if you pass a class to a method and edit it, the original class will be updated but if you pass an integer to a method and update it, the original integer will not be updated.

Strings are immutable reference types so will pass references of specified values to methods but when the values are edited, the original value will not change but a new instance will be created.

So you would need the ref keyword for any string parameters whose original instances you want to be updated but you don’t need it for any class parameter (e.g. Result) whose original instances you want to be updated.

Unfortunately you cannot use the keyword ref with async methods so you will need to return your updated string. If you need to return multiple value type values, you can create data type for the return values (similar to a parameter object but for your return value);

See the following resource for more info on reference type vs value type.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật