Solved

'with' function?

Posted on 2003-11-25
6
218 Views
Last Modified: 2010-04-16
Does C# have anything similar to Visual Basic's 'with' function?
0
Comment
Question by:derekthornton
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 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