?
Solved

'with' function?

Posted on 2003-11-25
6
Medium Priority
?
224 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 200 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month13 days, 18 hours left to enroll

800 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