29

I know that C# has the using keyword, but using disposes of the object automatically.

Is there the equivalence of With...End With in Visual Basic 6.0?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
odiseh
  • 25,407
  • 33
  • 108
  • 151
  • 2
    "with" actually existed well before VB. It exists in Pascal, for dealing with record types. Don't know if Pascal borrowed the idea from elsewhere. – JeffK Oct 29 '10 at 15:32

6 Answers6

38

It's not equivalent, but would this syntax work for you?

Animal a = new Animal()
{
    SpeciesName = "Lion",
    IsHairy = true,
    NumberOfLegs = 4
};
tomfanning
  • 9,552
  • 4
  • 50
  • 78
  • 5
    "With" does not create instances, so this is wrong. – John Stock Apr 28 '17 at 21:02
  • @ John Stock But it solves some problems in a way. All depends on context. When you need to set up some properties and you use some kind of property injection, this is simplifies some things. Of course main problem is that this not allow to call some sort of returning functions because it requires already created instance. – Mantas Daškevičius Feb 07 '20 at 12:22
35

C# doesn't have an equivalent language construct for that.

Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223
22

There is no equivalent, but I think discussing a syntax might be interesting!

I quite like;

NameSpace.MyObject.
{
    active = true;
    bgcol = Color.Red;
}

Any other suggestions?

I cant imagine that adding this language feature would be difficult, essentially just a preprocessed.

EDIT:

I was sick of waiting for this feature, so here is and extension that achieves a similar behavior.

/// <summary>
/// C# implementation of Visual Basics With statement
/// </summary>
public static void With<T>(this T _object, Action<T> _action)
{
    _action(_object);
}

Usage;

LongInstanceOfPersonVariableName.With(x => {
     x.AgeIntVar = 21;
     x.NameStrVar = "John";
     x.NameStrVar += " Smith";
     //etc..
});

EDIT: Interestingly it seems someone beat me to the punch, again, with this "solution". Oh well..

expelledboy
  • 2,033
  • 18
  • 18
  • Although not exactly the same, I like your idea. If used with good formatting it can make code more readable. – Rudi Sep 20 '12 at 11:53
  • So, you ended up with again repeating the variable name. You can do just the same with: `x = LongInstanceOfPersonVariableName; x.AgeIntVar = 21; //etc.` – LoBo Oct 24 '14 at 07:25
  • 1
    I just threw up a little in my mouth. – JJS Feb 01 '16 at 15:46
10

I think the equivalent of the following VB:

With SomeObjectExpression()
  .SomeProperty = 5
  .SomeOtherProperty = "Hello"
End With

Would be this is C#:

{
  Var q=SomeOtherExpression();
  q.SomeProperty = 5;
  q.SomeOtherProperty = "Hello";
}

The only real difference is that in vb, the identifier doesn't have a name "q", but is simply a default identifier used when a period is encountered without any other identifier before it.

KyleMit
  • 30,350
  • 66
  • 462
  • 664
supercat
  • 77,689
  • 9
  • 166
  • 211
  • 1
    What's with the outer curlies? Sorry, but this isn't in any way close to equivalent. It just a bunch of calls to setters. – tomfanning Nov 11 '10 at 22:03
  • 2
    The outer braces set the scope of variable q. Provided that the object expression is a class rather than a struct, I think the VB.Net and C# code above are almost precisely equivalent. The former statement is an example of how a with statement might be used, and the latter statement is a C# translation. The C# example uses a couple setters because that's what the arbitrarily-chosen VB.net example does. The essential point is that the "with" is equivalent to defining a temporary variable and then using it any time a period is used with no preceding identifier. – supercat Nov 13 '10 at 17:55
  • That's not an equivalent, it's an alternative. – John Stock Apr 28 '17 at 21:04
  • @JohnStock: When I say "equivalent", I am talking about the behaviors. No matter what `SomeOtherExpression` or the property setters do, I don't think there is any way any of them could determine which of the above code snippets was evaluating them *except* by using Reflection to examine the calling context. The fact that Reflection could distinguish the calling context makes them not *quite* equivalent, but in all other respects the behaviors would be identical. – supercat Apr 28 '17 at 22:10
7

There's no equivalent to With ... End With in C#.

Here's a comparison chart for you that illustrates differences between Visual Basic and C#.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Joseph
  • 25,330
  • 8
  • 76
  • 125
  • That comparison chart is awesome. I am working on transitioning to C# from VB and that is going to be very helpful. – Buck Hicks May 11 '11 at 13:36
3

There is no equivalent structure in C#. This is a Visual Basic 6.0 / VB.NET feature.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454