|
That code is ..... disturbing.
I think about half the new features showing up in C# now are just for syntactic sugar. This is one where I think the community of old-timers would be split on its usefulness vs how many people would actually use it.
I'd be interested in seeing any telemetry on its use.
|
|
|
|
|
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
|
|
|
|
|
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 times Foo -> -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()
|
|
|
|
|
Used it mostly in Visual FoxPro; and in VB.
Less code; clearer outlines; more generic possibilities.
Yep; totally useless.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
You really want 'With in C#: [^]. This hack uses reflection ... I'd never use it in production code: [^]
And, now, the "sermon:" I think it's a mistake to try and "bend" C# to fit your background in another language. At the same time, imho, most people will, in the first phase of learning, look for, expect, things/structures through mental habits conditioned by other languages. I believe (hypothesis) that programmers often have a kind of imprinting/bonding experience with the first language they learn well.
Surrender !
«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
modified 18-May-19 11:54am.
|
|
|
|
|
Did you see my terse example? Do you like typing the class name in front of every member name in a code block that sets ten properties one after another?
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
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
I'm still doing some programming in VB.Net.
I never use With.
|
|
|
|
|
Just assign whatever you'd usually with with to a variable with a short name.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
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
|
|
|
|
|
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' ... 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
modified 19-May-19 2:37am.
|
|
|
|
|
I get a lot of useful information and links when I asked questions on this site.
I'm looking for an easy way to store my replies and internet links so I can refer back to them when I need to.
At the moment it's a case of copy and pasting into a text file.
Does anyone know of a useful program that makes it easy to quickly select text and store it in a text file. Maybe a program running in the background that could store the text to a certain text file or ask me what text file to add the text to.
Brian
|
|
|
|
|
One simple way is to use the CP Bookmark system: at the bottom right of each message or question is a "bookmark" icon - it looks like a little ribbon - just click that and it adds it to a list. Clicking on "My Bookmarks" under the username dropdown at the top right of any page gives you tabs collections organised by type (Message, Tip, Article, ...)
Saves the hassle of copy and paste!
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!
|
|
|
|
|
Thanks Griff for the suggestion but it would might mean searching Code Project for a certain reply.
I like to keep my replies off line if possible.
Brian
|
|
|
|
|
I used a wiki in the past, makes it easy to organize information. Combine that with C# Online Compiler | .NET Fiddle[^] and you can try some of the samples online.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Sorry Eddy I got your name mixed up with Bill in my reply.
I'm mainly looking for a off-line program to collect my text.
If I knew enough about C# I'd write a program that does this.
Brian
|
|
|
|