Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use object initializers with using statements?

Is there any way to refactor this code to not have to use a temporary variable and still use the syntactic sugar associated with object initializers?

FrmSomeForm someTempForm = new FrmSomeForm()
{
    SomePropA = "A",
    SomePropB = "B",
    SomePropC = "C"
};
using (FrmSomeForm someForm = someTempForm)
{
    someForm.ShowDialog();
}
like image 475
Ryan Peschel Avatar asked Aug 23 '26 10:08

Ryan Peschel


2 Answers

using (FrmSomeForm someForm = new FrmSomeForm())
{
    someForm.SomePropA = "A";
    someForm.SomePropB = "B";
    someForm.SomePropC = "C";
    someForm.ShowDialog();
}

I think that is the simplest way and even the more readable in my opinion ...

Keep things simple :-)

like image 82
aleroot Avatar answered Aug 26 '26 00:08

aleroot


using (FrmSomeForm someForm = new FrmSomeForm(){
    SomePropA = "A",
    SomePropB = "B",
    SomePropC = "C"
})
{
    someForm.ShowDialog();
}

doesn't this work? oO

like image 40
Dmytro Avatar answered Aug 26 '26 01:08

Dmytro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!