locked
Equivalent of VB.NET's With statement in C# RRS feed

  • Question

  • I'd like simplify following codes with something like "With" statement in VB.NET.

    PatientLocalImport patlocalimport = new PatientLocalImport();
    patlocalimport.importMode = ImportMode.UPDATE;
    patlocalimport.patientlocal.PatientID = patient.PatientID;
    patlocalimport.patientlocal.CellPhone = patient.CellPhone;
    patlocalimport.patientlocal.AvoidReminder = pat.AvoidReminder;
    patlocalimport.patientlocal.MessageNo = pat.MessageNo;
    patlocalimport.patientlocal.ActiveDatetime = pat.ActiveDatetime;
    patlocalimport.patientlocal.RecallMessageNo = pat.RecallMessageNo;
    patlocalimport.patientlocal.ConfirmMessageNo = pat.ConfirmMessageNo;

    How to do it in C#?

    Thursday, July 16, 2020 10:43 AM

All replies

  • You can do :

    PatientLocalImport patlocalimport = new PatientLocalImport()
    {
        importMode = ImportMode.UPDATE,
        patientlocal.PatientID = patient.PatientID,
        //... etc
    };

    • Proposed as answer by Cihan YakarMVP Thursday, July 16, 2020 11:14 AM
    Thursday, July 16, 2020 11:06 AM
  • hmm, I don't think you want to do this.

    Cause this looks like your class should have a method for this. Maybe it even is a candidate for an (overloaded) ctor.


    Thursday, July 16, 2020 11:24 AM