By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,458 Members | 1,435 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,458 IT Pros & Developers. It's quick & easy.

C# equivalent of Pascal "With"

P: n/a
In Pascal/Delphi you can say

With MyStructure do begin
Field1 := something;
Field2 := somethingElse;
:
// and so on...
end;

"With MyStructure" avoids having to prepend "MyStructure" to every
reference to MyStructure's fields. Is there an equivalent in C#?

Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
> "With MyStructure" avoids having to prepend "MyStructure" to every
reference to MyStructure's fields. Is there an equivalent in C#?


No.

Many people coming to C# from languages that have it, really want it though.

Mark
Nov 16 '05 #2

P: n/a
Given "instanceOfAClassWithAReallyLongName",
the idiom is something like
ClassWithAReallyLongName c = instanceOfAClassWithAReallyLongName;
c.Property1 = x;
c.Property2 = y;
//etc.
but then, you might want to build a helper class that
just handles the assignment.
--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
Home of Matrix.NET
"DrOrbit" <nl**@nlsa.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
In Pascal/Delphi you can say

With MyStructure do begin
Field1 := something;
Field2 := somethingElse;
:
// and so on...
end;

"With MyStructure" avoids having to prepend "MyStructure" to every
reference to MyStructure's fields. Is there an equivalent in C#?

Nov 16 '05 #3

P: n/a
MarkT [developmentor] <Ma****************@discussions.microsoft.com>
wrote:
"With MyStructure" avoids having to prepend "MyStructure" to every
reference to MyStructure's fields. Is there an equivalent in C#?


No.

Many people coming to C# from languages that have it, really want it though.


And many other people are glad it's not in there :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

P: n/a
Someone onced mentioned this language feature during a whiteboard
open-session talk with Anders Hejlsberg. The conversation went
something like this:

"I wish that C# had something like Pascal's 'with' for record
selection."

Hejlsberg's answer was:

"Well, there's no harm in wishing, is there?"

For my part, I agree with Mr. Hejlsberg: I don't miss this feature at
all, given that I now have an IDE that fills in most of those long
names for me. It made some sense back in the days of writing source
code in text editors. These days I think it would cause more problems
(difficulties reading code) than it would solve (ease of editing).

Nov 16 '05 #5

P: n/a
I think the best way to do this is to create a method in the target
class; within that method, "this." is optional (and better left out).

Peace,
--Carl

Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.