Equivalent of VB's "with"

  • Thread starter Thread starter Mr Flibble
  • Start date Start date
Also see:
http://www.tangiblesoftwaresolutions.com/Articles/CSharp Equivalent to VB With.htm
(excuse the product plug at the bottom)

Basically, the gist of it is that "With" is no better than an abbreviated
variable named "x" - it's obscure and also implies somehow that the block
centers around the object being With'ed (when it really just centers around
something you didn't want to re-type).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
 
* David Anton said:
Also see:
http://www.tangiblesoftwaresolutions.com/Articles/CSharp Equivalent to VB With.htm
(excuse the product plug at the bottom)

Basically, the gist of it is that "With" is no better than an abbreviated
variable named "x" - it's obscure and also implies somehow that the block
centers around the object being With'ed (when it really just centers around
something you didn't want to re-type).

" Another alternative is to refactor so that the With block is a new
method with the object passed in as a parameter. You can use a reduced
parameter name length (without making it obscure)."

How do you reduce the parameter name length? can you give me an example?

Cheers,

Signore Flibble
 
It's just a possibility that may or may not be appropriate.

For example, instead of:
somehorriblelongnameyoudontwanttoretype.methodone()
somehorriblelongnameyoudontwanttoretype.methodtwo()
somehorriblelongnameyoudontwanttoretype.methodthree()
somehorriblelongnameyoudontwanttoretype.methodfour()

You *could* just call a new method:
YourMethod(somehorriblelongnameyoudontwanttoretype)

and then the new method defined as:
Public Sub YourMethod(ByVal activeobject As Whatever)
activeobject.methodone()
activeobject.methodtwo()
activeobject.methodthree()
activeobject.methodfour()
End Sub

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
 
David Anton said:
It's just a possibility that may or may not be appropriate.

For example, instead of:
somehorriblelongnameyoudontwanttoretype.methodone()
somehorriblelongnameyoudontwanttoretype.methodtwo()
somehorriblelongnameyoudontwanttoretype.methodthree()
somehorriblelongnameyoudontwanttoretype.methodfour()

You *could* just call a new method:
YourMethod(somehorriblelongnameyoudontwanttoretype)

and then the new method defined as:
Public Sub YourMethod(ByVal activeobject As Whatever)
activeobject.methodone()
activeobject.methodtwo()
activeobject.methodthree()
activeobject.methodfour()
End Sub

Even better would be to make YourMethod a method of the Whatever class.
Making too many calls into another object is known as "feature envy," and it
often (though not always) indicates that there's a new method waiting to be
extracted.

///ark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top