Why was not included , like in VB.Net the With feature.
In VB you can use an Object like this.
With MyObject
.Text = "Hi"
End With
Or like jScript
with (MyObject)
{
Text = "Hi";
}
Why it is not present in C#? 5 1359
> Why was not included , like in VB.Net the With feature. In VB you can use an Object like this.
With MyObject .Text = "Hi" End With
I'm a VB guy stretching my C# legs. I have always been under the impression
that the With feature was specific to VB. I miss it in C# too. It makes
the source code easier to type, easier to read, and it runs faster too,
because the pointer only has to get resolved once.
I haven't looked, and it doesn't interest me enough right now to check it
out, bue the C# compiler might be smart enough to generate MSIL that's
analogous to the VB implementation of the feature.
--
Peace & happy computing,
Mike Labosh, MCSD
"I have no choice but to believe in free will."
"Mike Labosh" <ml*****@hotmail.com> wrote in message
news:u1**************@TK2MSFTNGP15.phx.gbl... Why was not included , like in VB.Net the With feature. In VB you can use an Object like this.
With MyObject .Text = "Hi" End With I'm a VB guy stretching my C# legs. I have always been under the impression that the With feature was specific to VB. I miss it in C# too. It makes the source code easier to type, easier to read, and it runs faster too, because the pointer only has to get resolved once.
Actually, readability is one of hte big reasons *not* to use with, IMHO. The
last thing readability needs is another indirection from what you are
actually using.
I haven't looked, and it doesn't interest me enough right now to check it out, bue the C# compiler might be smart enough to generate MSIL that's analogous to the VB implementation of the feature.
No, the compiler does not generate any With like code and for good reason. A
With block implies a singular instance of a property\method\variable value,
while straight code doesn't make such guarentees. If the compiler starts
optimizing away accesses it will cause non-obvious bugs when a properties
value changes between or due to calls. Such a practice is unadvisable but
making the compiler break such patterns would be horrendous.
If you really want to emulate with, the simple pattern is:
MyObject o = x.y.z.longname.myObjectProperty;
o.A();
o.B();
o.C();
And, finally, from the horses mouth: http://msdn.microsoft.com/vcsharp/te...t/default.aspx
-- Peace & happy computing,
Mike Labosh, MCSD "I have no choice but to believe in free will."
I did start my career developing with VB, but I have been using C# for the
past five years and even I think that the WITH statement would be
beneficial. After reading the article mentioned, here are my takes on their
specific bullet points.
1) I my opinion the readability is improved. It seem to me that
With MyDataStructure.GetButton(44)
.Text = "Hello"
.BackColor = Color.Blue
End With
is easier to read then
Dim b as Button = MyDataStructure.GetButton(44)
b.Text = "Hello"
b.BackColor = Color.Blue
Besides, the compiler already supports shortcuts such as the using(...) and
foreach(...) blocks.
2) Compilers are complex to begin with. Adding a WITH statement would have
just mean that there would be an additional scope level to deal with. The
compiler already has to deal with determining if something is a class vs.
local variable.
3) Although C# is similar to C++, the statement about not including it
because it was never part of C++ does not make much sense. Optional and
Default parameters ARE part of C++ but were never included in C# so there is
no real correlation between something existing in C++ and in C#.
True that WITH does not offer anything to the language that cannot be coded
some other way, but that can easily be said about numerous other constructs.
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:Oe**************@TK2MSFTNGP10.phx.gbl... "Mike Labosh" <ml*****@hotmail.com> wrote in message news:u1**************@TK2MSFTNGP15.phx.gbl... Why was not included , like in VB.Net the With feature. In VB you can use an Object like this.
With MyObject .Text = "Hi" End With I'm a VB guy stretching my C# legs. I have always been under the impression that the With feature was specific to VB. I miss it in C#
too. It makes the source code easier to type, easier to read, and it runs faster too, because the pointer only has to get resolved once.
Actually, readability is one of hte big reasons *not* to use with, IMHO.
The last thing readability needs is another indirection from what you are actually using.
I haven't looked, and it doesn't interest me enough right now to check
it out, bue the C# compiler might be smart enough to generate MSIL that's analogous to the VB implementation of the feature. No, the compiler does not generate any With like code and for good reason.
A With block implies a singular instance of a property\method\variable
value, while straight code doesn't make such guarentees. If the compiler starts optimizing away accesses it will cause non-obvious bugs when a properties value changes between or due to calls. Such a practice is unadvisable but making the compiler break such patterns would be horrendous.
If you really want to emulate with, the simple pattern is:
MyObject o = x.y.z.longname.myObjectProperty; o.A(); o.B(); o.C();
And, finally, from the horses mouth: http://msdn.microsoft.com/vcsharp/te...t/default.aspx -- Peace & happy computing,
Mike Labosh, MCSD "I have no choice but to believe in free will."
I'll just toss my two cents in here... a 'with' statement cetainly reduces
the amount of text in the code, but it DOES NOT improve readability. In
fact, in my code, I always use the full namespace when declaring objects,
just so there is no confusion as to where it is coming from. While it might
be easier on _your_ eyes if there is less code, it will be easier for another
developer if the code has as much 'self-commenting' as possible.
-James
"Peter Rilling" wrote: I did start my career developing with VB, but I have been using C# for the past five years and even I think that the WITH statement would be beneficial. After reading the article mentioned, here are my takes on their specific bullet points.
1) I my opinion the readability is improved. It seem to me that With MyDataStructure.GetButton(44) .Text = "Hello" .BackColor = Color.Blue End With
is easier to read then
Dim b as Button = MyDataStructure.GetButton(44) b.Text = "Hello" b.BackColor = Color.Blue
Besides, the compiler already supports shortcuts such as the using(...) and foreach(...) blocks. 2) Compilers are complex to begin with. Adding a WITH statement would have just mean that there would be an additional scope level to deal with. The compiler already has to deal with determining if something is a class vs. local variable. 3) Although C# is similar to C++, the statement about not including it because it was never part of C++ does not make much sense. Optional and Default parameters ARE part of C++ but were never included in C# so there is no real correlation between something existing in C++ and in C#.
True that WITH does not offer anything to the language that cannot be coded some other way, but that can easily be said about numerous other constructs. "Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in message news:Oe**************@TK2MSFTNGP10.phx.gbl... "Mike Labosh" <ml*****@hotmail.com> wrote in message news:u1**************@TK2MSFTNGP15.phx.gbl...> Why was not included , like in VB.Net the With feature. > In VB you can use an Object like this. > > With MyObject > .Text = "Hi" > End With
I'm a VB guy stretching my C# legs. I have always been under the impression that the With feature was specific to VB. I miss it in C# too. It makes the source code easier to type, easier to read, and it runs faster too, because the pointer only has to get resolved once.
Actually, readability is one of hte big reasons *not* to use with, IMHO.
The last thing readability needs is another indirection from what you are actually using.
I haven't looked, and it doesn't interest me enough right now to check it out, bue the C# compiler might be smart enough to generate MSIL that's analogous to the VB implementation of the feature.
No, the compiler does not generate any With like code and for good reason. A With block implies a singular instance of a property\method\variable value, while straight code doesn't make such guarentees. If the compiler starts optimizing away accesses it will cause non-obvious bugs when a properties value changes between or due to calls. Such a practice is unadvisable but making the compiler break such patterns would be horrendous.
If you really want to emulate with, the simple pattern is:
MyObject o = x.y.z.longname.myObjectProperty; o.A(); o.B(); o.C();
And, finally, from the horses mouth: http://msdn.microsoft.com/vcsharp/te...t/default.aspx -- Peace & happy computing,
Mike Labosh, MCSD "I have no choice but to believe in free will."
"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:eD**************@TK2MSFTNGP14.phx.gbl... I did start my career developing with VB, but I have been using C# for the past five years and even I think that the WITH statement would be beneficial. After reading the article mentioned, here are my takes on their specific bullet points.
1) I my opinion the readability is improved. It seem to me that With MyDataStructure.GetButton(44) .Text = "Hello" .BackColor = Color.Blue End With
is easier to read then
Dim b as Button = MyDataStructure.GetButton(44) b.Text = "Hello" b.BackColor = Color.Blue
I disagree, but much of that is personal preference.
Besides, the compiler already supports shortcuts such as the using(...) and foreach(...) blocks.
The primary difference with those vs with is that they either act on an
existing variable or define a new one, with creates a scope with new lookup
rules.
Somehow I don't think
with (object x = myObject.ObjectField)
{
x.ToString();
}
does much for you, although that is the pattern foreach forces and using
generally provides.
2) Compilers are complex to begin with. Adding a WITH statement would have just mean that there would be an additional scope level to deal with. The compiler already has to deal with determining if something is a class vs. local variable.
Adding it to the compiler is simple enough, I could add it to mono's
compiler in a couple hours(mostly because I'd have to special some method
resolution stuff). The issue is more changing the language to support method
call syntax with the with statement, (the .Method()) or changing method
resolution within a scope block. It complicates human understanding of the
language by changing the rules mid-stream.
C# is actually a rather simple language. The syntax is pretty consistent
across the board(there are a few abberations, of course, stackalloc is kind
of odd). There are no oddities like VB has(or had) with the .Method() syntax
used in WIth blocks, optional parentheses(are they still optional\weird? I
havn't used vb.net much, I just remember parenthese issues in VB6), or
builtin language intrinsics. Changing it to support WIth more or less forces
that, and that really is undesired.
Of course, that isn't to say that VB is badly designed or even is
inconsistent, just that in this case the design goals differ. C# strives to
be consistent(even at the cost of capability) and library driven while VB
works towards customization and immediate simplicity, even if it costs a
little bit in consistency
You can see it everywhere in the two languages. C# has structs, interfaces
and classes. VB has structures, interfaces, classes, and modules. C#'s
equivlilent to a module(in 2.0 anyway, 1.x had none) is achieved by a
modifier to the class declaration.
C# requires partial on every part of a partial class, vb requires it on only
one.
VB has several casting\conversion operators with a considerable amount of
functinoality, C# has 2 and pushes everything else out into the library.
VB has two types of method(Sub and Function), C# has one.
You could spend all day comparing them(which is part of why I really hate
the "they are just skin around the framework" notion. The langauges are
fundamentally different, their users think differently, they solve different
problems and the same problems in different ways). With fits really well
into the VB paradigm...I am just not so sure that it does within C#.
3) Although C# is similar to C++, the statement about not including it because it was never part of C++ does not make much sense. Optional and Default parameters ARE part of C++ but were never included in C# so there is no real correlation between something existing in C++ and in C#.
I agree this is weak, but on the same hand they were not compelled to add it
as the VB folks were. Could you imagine the uproar if VB.NET didn't support
with? True that WITH does not offer anything to the language that cannot be coded some other way, but that can easily be said about numerous other constructs. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Penn Markham |
last post by:
Hello all,
I am writing a script where I need to use the system() function to call
htpasswd. I can do this just fine on the command line...works great
(see attached file, test.php). When my...
|
by: seberino |
last post by:
I've been reading the beloved Paul Graham's "Hackers and Painters".
He claims he developed a web app at light speed using Lisp and lots
of macros.
It got me curious if Lisp
is inherently faster...
|
by: Zeljko |
last post by:
Hi,
I moved from VB6 to C# recently, and am very satisfied with the
result. However, I miss this one feature. The closest thing I could
find is "using" statement, but it requires object to...
|
by: Patrick |
last post by:
I'm working on a contact management application, and need a hand with one
aspect...
Here's what I want to create:
------------------------------------
A form split into two parts. There is a...
|
by: Piper707 |
last post by:
I need help with using a general template which would process all tags
other than the ones for which specific templates have been written.
My XML looks like this:
<ITEMS>...
| |
by: Charlie Bear |
last post by:
i've got myself into a bit of an oo mess. it's probably me
misunderstanding how oo works.
I've got a base class called "Feature" which some classes inherit. in
the database i've stored the data...
|
by: arne |
last post by:
Hi all,
imagine I call a function, but omit one of the parameters, like:
foo.c:
void foo( int a, int b ) {
/* do something with a and b */
return;
}
|
by: twin2003 |
last post by:
need help with inventory part 5 here is what I have to do
Modify the Inventory Program by adding a button to the GUI that allows the user to move to the first item, the previous item, the next...
|
by: Jean-Fabrice RABAUTE |
last post by:
Hi,
I am happy to announce the release of the Companion.JS v0.2, an alpha
version to a Javascript debugger for IE :
http://www.my-debugbar.com/wiki/CompanionJS/HomePage
The main new feature...
|
by: mirianCalin |
last post by:
the code saves the category, image title, image, and feature..
but the problem is that the "feature" is not saved, but the others were saved..
this is the data types of my table
category = text...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |