Solved

'with' function?

Posted on 2003-11-25
6
215 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now