1

Which one is best approach out of the following? Or Both has same effect ?

Dim carrierName As String
Dim someotherName As String
Dim anotherOne As String

Using oDa As New MyCompany.DataAccess.MyModule
     carrierName = oDa.GetCarrierName(itemNumber)
End Using

Using oDa As New MyCompany.DataAccess.MyModule
     someotherName = oDa.GetSomeOtherName(itemNumber,1)
End Using

Using oDa As New MyCompany.DataAccess.MyModule
     anotherOne = oDa.GetAnotherName("somevalue")
End Using

OR

Using oDa As New MyCompany.DataAccess.MyModule
     carrierName = oDa.GetCarrierName(itemNumber)   
     someotherName = oDa.GetSomeOtherName(itemNumber,1)   
     anotherOne = oDa.GetAnotherName("somevalue")
End Using
3
  • Uh, this is VB.NET, not C#... I've edited accordingly. Commented Feb 11, 2011 at 14:49
  • They are not doing the same thing. The first creates three MyCompany.DataAccess.MyModule objects while the second creates only one. It will depend on how MyCompany.DataAccess.MyModule behaves. How about measuring it? Commented Feb 11, 2011 at 14:49
  • Why creating and destroying the same thing over and over? Use only one ´using´ Commented Feb 11, 2011 at 14:50

4 Answers 4

3

Well, one version will create three MyModule instances, the other will only create one. We can't tell what the difference is without knowing more about MyModule.

The second approach looks cleaner to me, but without knowing the semantic differences, it's hard to say that it's definitely better.

Sign up to request clarification or add additional context in comments.

3 Comments

Even without knowing anything about MyModule, wouldn't creating 3 instances be worse than creating 1? Unless you get a buy 1, get 2 free deal :)
@Tundey: Not necessarily. Maybe it's more expensive to call GetSomeOtherName on a MyModule which has already had GetCarrierName called on it, for example. It seems unlikely, but the point is that there could be huge differences - and we just can't see what they are.
I suppose it's likely...though that'll be really counter-intuitive.
0

In the first, you are creating and disposing of 3 instances of MyModule while in the second you are only creating and disposing 1 instance. So the second approach is better. It's cleaner and more straightforward too.

2 Comments

That's a bit like saying, "I'm trying to decide which phone to buy. Phone X costs $100, phone Y costs $200. As phone X is cheaper, it's better to buy phone X." The two phones presumably do different things. You can't tell which is actually the best phone to buy without knowing those differences.
Since it's all the same MyModule, these are all Phone X. I do agree with your point that it's possible (albeit unlikely) that calling a method on an object makes other methods more expensive than creating a new instance of that object.
0

The two approachs are fondamentally different, but the end result depend on your MyModule implementation.

The second one seems better, as it creates and manages only one MyModule object, especially if MyModule is costly to create and to dispose.

The first one can be necessary if your MyModule doesn't allow multiple requests with the same instance. But if it's the cas. hem... it would look like a bug to me.

Comments

0

The latter is better in most cases. Using is just shortcut for

var oDa = new MyCompany.DataAccess.MyModule();
try {
    carrierName = oDa.GetCarrierName(itemNumber)   
    someotherName = oDa.GetSomeOtherName(itemNumber,1)   
    anotherOne = oDa.GetAnotherName("somevalue")
}
finally {
    oDa.Dispose();
}

First method could be used when every method allocates huge amount of memory that will need to be cleaned right away, but I don't think it is your case.

btw: you seem to have mistaken c# with visual basic

1 Comment

The using statement does not release memory (unless it's unmanaged memory safely tucked behind an IDisposable managed wrapper).

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.