1

I have couple of comboboxes in my project. And their content is changing a lot. So I put their content in my database. Now, I wrote a code like below for retrieving their data but I wonder, is this 'usings' working like my approach or doesn't matter at all? My main question is; is this a healthy way to use objects like this?

DataTable results;
using (results = myDataInteractionClass.SelectDB())
{
    // fill table objects to combobox
}

using (results = myDataInteractionClass.SelectAnotherDB())
{
    // fill this new table's objects to combobox
}

using (results = myDataInteractionClass.SelectThirdDB())
{
    // so goes on like this.
}

2 Answers 2

5

Yes, this is perfectly fine. You should always narrow down the scope as much as possible, so having three usings here is good (opposed to one using around the entire block).

One suggestion though: to prevent misuse of dataTable between or after the using, I would suggest to create the variable inside the using:

using (/*so here*/ DataTable results = myDataInteractionClass.SelectDB())
{
    // fill table objects to combobox
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for answer & advice. I pre-defined the datatable because I think like "maybe if I use one datatable definition at first, I can make profit of device's stack RAM area & performance.". It's not that necessary & readability is more important in this situation then?
1

As I guess your only goal is to read values from table and fill them in combobox, consider using DataReader Insted of DataTable it is lightweight compared to DataTable. And yes Patrick's suggestion is appropriate. I normally use var with using statement.

2 Comments

oh, ok. that makes more sense to make profit of performance rather than my method. thanks.
@mimozz usually, if an answer solves your problem or gives useful information, you upvote it to appreciate the effort.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.