문제

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!

도움이 되었습니까?

해결책

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.

다른 팁

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).

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top