views:

44

answers:

3

In my current project everyone names variables and methods after what they are and what they do. This is good for quickly understanding the code, but with a lot of long varible names comes some headaches, such as when copying a dataset to an entity/object. So, while you understand the code, the readability still takes a blow.

veryLongVariableName.Id = datasetVeryLongVariableName.Id
veryLongVariablename.Something = datasetVeryLongVariableName.Something
etc.

Using VB.NET's With keyword can help.

With veryLongVariableName

    .Id = datasetVeryLongVariableName.Id
    .Something = datasetVeryLongVariableName.Something

End With

Now, my question, is there any way of using With, or something similar, but for several variables at the same time? Something like:

With veryLongVariableName As a, datasetVeryLongVariableName as b

    a.Id = b.Id
    a.Something = b.Something

End With

I'm all for descriptive naming conventions, but they do tend to clutter things. Especially in VB!

+4  A: 

No, this is not possible.

With works with a single object only. See MSDN.

You can however, nest With statements (though this is not recommended as you could end up making your code really unreadable).

Oded
+1 Didn't know you could nest them. Glad you put a warning on it.
wllmsaccnt
+4  A: 

If the code that does these sets of assignments is part of a single logical action then you could refactor it out into it's own method/function call. While this doesn't specifically answer your question about a multiple with usage, it would improve readibility in lines with suggestions found in popular books such as Code Complete.

wllmsaccnt
+1: I think this probably gets to the same point, good idea.
Brian MacKay
+2  A: 

Behind the scenes, all the With keyword does is declare a temporary variable that references the object you specify. Its references will be compiled to exactly the same IL code as fully-qualifying the object name, so there is no performance penalty. You can easily do this yourself:

Dim a as MyClass = veryLongVariableName
Dim b as MyOtherClass = datasetVeryLongVariableName

a.Id = b.Id
a.Something = b.Something
Cody Gray