Solved

'with' function?

Posted on 2003-11-25
6
223 Views
Last Modified: 2010-04-16
Does C# have anything similar to Visual Basic's 'with' function?
0
Comment
Question by:derekthornton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 22

Expert Comment

by:_TAD_
ID: 9819937


nope.


most people think that using the "using" statement is the same thing, but it is not.

0
 
LVL 22

Accepted Solution

by:
_TAD_ earned 50 total points
ID: 9819960
There are, however, a lot of other shortcuts you can use.

What code are you trying to shorten?  If you post it, we may be able to tidy it up for you.


The great thing about .Net is that many of its properties are really just references to other objects.

So instead of typing:
object1.child1.subchild2.subchild3.value1 = "something1";
object1.child1.subchild2.subchild3.value2 = "something2";
object1.child1.subchild2.subchild3.value3 = "something3";

many times you can say:

object1.child1.subchild2.subchild3 myObj;
myObj.value1 = "something1";
myObj.value2 = "something2";
myObj.value3 = "something3";
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9819976
No.
Why?
- It makes the code hard to read.
- Things like "myObject.MyOtherObject.TheThridObject.AIndexer[myObject.MyOtherObject.AValue].Name" are nothing but very bad programming style. So there's no reason to make it easier to do bad programming...
- Everything more than one, max. two steps away should not be accessed directly.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:derekthornton
ID: 9820116
I'm just trying to define the fileds of a struct, I can type them all in, it would just be easier with a 'with' like block. Thanks anyway!
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9820203
Define a constructor on the struct.

public struct MyStruct
{
    int a;
    int b;
    int c;

    public MyStruct(int a, int b, int c)
    {
        this.a = a;
        this.b = b;
        this.b = b;
    }
}

Then call new MyStruct(1, 2, 3);
0
 
LVL 2

Expert Comment

by:ericgu
ID: 9820537
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question