Does Anybody Else Miss the WITH construct of Visual Basic?
-
As I read [Adam Storr - Playing with C# 7 - Deconstruct](https://adamstorr.azurewebsites.net/blog/playing-with-csharp-7-deconstruct?utm\_source=Main&utm\_campaign=0cdc8b3a40-EMAIL\_CAMPAIGN\_2017\_12\_19\_COPY\_01&utm\_medium=email&utm\_term=0\_aa2f642d94-0cdc8b3a40-227561569&mc\_cid=0cdc8b3a40&mc\_eid=8087c9508d), I kept hoping that it would answer at long last the question of how to do
with
in C#. Alas, it fell short again. Does anybody besides me wish that C# supportedwith
blocks, or have I overlooked a seriously underused language feature?David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting
Many years ago, I wrote an expression evaluator for a Pascal compiler. "With" provided extra levels of complexity, which made me dislike it. "With" in VB leads to ambiguities when nested as you can have multiple sets of 'withed' variables in the same inner block. 'with' (now deprecated) in JavaScript lead to ambiguities where you could loop the 'with' and variables in the same construct could be global in one pass and local in subsequent passes. Used carefully, "With" can be a convenient shortcut; but it can be used badly. If you give people knives, they may whittle beautiful sculptures, but they are more likely to injure themselves or someone else.
-
I don't miss Basic, either, but I miss that construct, which came to my attention when it became part of VBA as it manifested in Microsoft Access 2.0.
David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting
Real men don't code in VB, or use any of its bastard constructs, such as
with
orgoto
.".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
BillWoodruff wrote:
I think it's a mistake to try and "bend" C# to fit your background in anothr language.
I'd agree - and suspect that's what a lot of the recent C# changes have been driven by: VB and C++ developers which want to continue writing VB and C++ code in C# instead of learning a newer, fresher language paradigm. And maybe that's why C# is getting "bloated" and losing it's focus as a coherent language.
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
OriginalGriff wrote:
VB and C++ developers which want to continue writing VB and C++ code in C# instead of learning a newer, fresher language paradigm
Not to mention it will rightly piss off the other members of your team when they run across your code in the project.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
Surely, you jest.
with Foo {
.Bar = 123;
.Baz = @"Zap!";
.Goo = 3.14159;
.Ergo = 1.1414141414l4;
}David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting
-
Many years ago, I wrote an expression evaluator for a Pascal compiler. "With" provided extra levels of complexity, which made me dislike it. "With" in VB leads to ambiguities when nested as you can have multiple sets of 'withed' variables in the same inner block. 'with' (now deprecated) in JavaScript lead to ambiguities where you could loop the 'with' and variables in the same construct could be global in one pass and local in subsequent passes. Used carefully, "With" can be a convenient shortcut; but it can be used badly. If you give people knives, they may whittle beautiful sculptures, but they are more likely to injure themselves or someone else.
I got the same speak a few years ago when I would introduce LINQ into an answer because I was not catering to the lowest common denominator. Those same people now have no issues with it. (LINQ)
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal
-
Foo.Bar = 123;
Foo.Baz = @"Zap!";
Foo.Goo = 3.14159;
Foo.Ergo = 1.1414141414l4;is two lines shorter :)
noop()
So? How many more characters, each requiring a keystroke, does your proposal require?
David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting
-
Many years ago, I wrote an expression evaluator for a Pascal compiler. "With" provided extra levels of complexity, which made me dislike it. "With" in VB leads to ambiguities when nested as you can have multiple sets of 'withed' variables in the same inner block. 'with' (now deprecated) in JavaScript lead to ambiguities where you could loop the 'with' and variables in the same construct could be global in one pass and local in subsequent passes. Used carefully, "With" can be a convenient shortcut; but it can be used badly. If you give people knives, they may whittle beautiful sculptures, but they are more likely to injure themselves or someone else.
Quote:
Used carefully, "With" can be a convenient shortcut; but it can be used badly. If you give people knives, they may whittle beautiful sculptures, but they are more likely to injure themselves or someone else.
The same can be said of almost every construct in any programming language. I'll take my chances. Using programming languages requires wisdom, a commodity that is, unfortunately, in very short supply.
David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting
-
Did I see the code sample that would never compile [^]: yes. Did I see a reference in another post you made to something "illustrated" without explanation: yes.
David A. Gray wrote:
Do you like typing the class name in front of every member name in a code block that sets ten properties one after another?
I do not see how this "sucker-punch question" is relevant to anything discussed here :) Beginning with the object initializer syntax in C# (C# 3.0, .NET 3.5), initializing a bunch of whatever when a new object was created became much easier. What's your issue here ? If you want to simulate 'With, techniques are well known, for a long time (see my post here). Now, if you have a better way, that doesn't use reflection, or the usual Extension Method: I'm all ears !
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
It was a random thought, intended to generate the sort of discussion that has ensued. As a practical matter, I use object initializers, overloaded constructors, and constructors that have optional arguments almost exclusively. Unfortunately, this isn't always a workable option; thankfully, those times are rare.
David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting
-
It was a random thought, intended to generate the sort of discussion that has ensued. As a practical matter, I use object initializers, overloaded constructors, and constructors that have optional arguments almost exclusively. Unfortunately, this isn't always a workable option; thankfully, those times are rare.
David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting
David A. Gray wrote:
It was a random thought, intended to generate the sort of discussion that has ensued.
One of the best excuses I've seen on this forum, but, an excuse for what ? But, I'm so happy you have random thoughts that you realize (later?) come from a place of deep wisdom and are, obviously, evidence of your sublime intelligence generously helping mere mortals to say what needs to be said :wtf:
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
-
It was a random thought, intended to generate the sort of discussion that has ensued. As a practical matter, I use object initializers, overloaded constructors, and constructors that have optional arguments almost exclusively. Unfortunately, this isn't always a workable option; thankfully, those times are rare.
David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting
Well, it did generate discussion :) And, I wouldn't mind having a 'With, or a special flavor of 'Using. cheers, Bill
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
-
Quote:
Used carefully, "With" can be a convenient shortcut; but it can be used badly. If you give people knives, they may whittle beautiful sculptures, but they are more likely to injure themselves or someone else.
The same can be said of almost every construct in any programming language. I'll take my chances. Using programming languages requires wisdom, a commodity that is, unfortunately, in very short supply.
David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting
David A. Gray wrote:
Using programming languages requires wisdom, a commodity that is, unfortunately, in very short supply.
Amen ! And, to use a musical analogy: imagine you are learning to play the violin and every six months the number of strings changes ... the shape of the instrument changes ... every year there is another way to play a double-stop ... every year the shape and acoustics of concert halls and practice rooms change ... But, one might ask: is 'wisdom' a giant reservoir of pragmatic technique and hard-won insight ... bound to specific contexts and artifacts ... or, is 'wisdom' a deep understanding of organizing principles and algorithms. Well, the debate on that has been hot since Plato and Aristotle (episteme vs, techne) :) I favor what the ancient Greeks called phronesis, a "practical wisdom." Note this is sometimes translated as "prudence" because of its association with "virtue" (arete) in the Greek sources: I think that's misleading for the modern reader because of the association we have of "virtue" with morality. What arete meant for Plato and Aristotle ... imho, something very different than in the Judaeo-Christian tradition semantics of "virtue." If only I had some ... 'wisdom' :omg: ... perhaps I should say if only some 'wisdom' had me: "The truth is a snare: you cannot have it, without being caught. You cannot have the truth in such a way that you catch it, but only in such a way that it catches you." Soren Kierkegaard
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
-
So? How many more characters, each requiring a keystroke, does your proposal require?
David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting
Considering a new line is two-characters long on Windows (CrLf): * -5 (
with
) on the first line * -4 (}CrLf
) on the first line * -3 (}CrLf
) on the last line So 12 characters less, plus three timesFoo
-> -12 + 9 = -3 In the end, mine seems 3 characters shorter than yours. But the number of characters is not really important; I do not feel the with construct makes the code clearer, just more bloated.noop()
-
Well, it did generate discussion :) And, I wouldn't mind having a 'With, or a special flavor of 'Using. cheers, Bill
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
Quote:
a special flavor of 'Using
That's what I had in mind, but I wanted to see whether anybody else had the same idea.
David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting
-
Quote:
a special flavor of 'Using
That's what I had in mind, but I wanted to see whether anybody else had the same idea.
David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting
imho, while using 'using seems intuitive ... compared to 'with ... the fact that 'using already plays multiple semantic roles in C# argues against it. The word 'context comes to mind as descriptive and mnemonic in this scenario, but, I don't think Mads would go for it :)
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot