is there a With equivilent statement in C#?

Denaes

Well-known member
Joined
Jun 10, 2003
Messages
956
Sorry, I really did try to search, but it doesnt work with common words like With.

In VB you write:

Code:
With dtDataTable.Columns(1)

End With

basically means that when you throw a dot down, it assumes you typed that statement. Similar to Using, but only for short periods of times. Keeps you from having to type out a large line of code 12 times (or Copy & Pasting it) just to change 12 settigns in an object.

does something like this exist in C#?
 
you can do it C# using using also; using has to functions in C#; the one your accostomed to and things like this:

using(SomeNameSpace.SomeNameSpace.SomeClass o = new SomeNameSpace.SomenameSpace.SomeClass())
{
o.Property1 = x;
o.Property2 = y;
o.DoSomething();
}

Not exact syntax, but ball park - VS not here; theres an example in your help files; but I believe to use using in this context the class has to implement the IDisposable interface - I could be way off on base, but I remember something about it because the whole purpose of using it like this is to ensure an object is disposed after youve used it.

So obviously this isnt traditionally used as you would With...End With, it used it strange situations and its generally just easier to add the using line at the top of the document; that is the only keyword I miss from VB.
 
Thats pretty neat. You can create an object, use it and get rid of it really quick.

Well thank you :) My eyes and fingers will love you for it
 
Another approach to bris idea is to just set a new, shorter variable to your object (only works if its a class - not for structs):
C#:
DataColumn col = dtDataTable.Columns[1];
col.MaxLength = 5;
col.SomeProp = "hi";
...
col = null;

The last line is optional (setting col to null). It wont null out the column, only the variable reference col.

None of these provides the ".Property" syntax that VB offers, just shortcuts for typing.

-ner
 
bri189a said:
you can do it C# using using also; using has to functions in C#; the one your accostomed to and things like this:

using(SomeNameSpace.SomeNameSpace.SomeClass o = new SomeNameSpace.SomenameSpace.SomeClass())
{
o.Property1 = x;
o.Property2 = y;
o.DoSomething();
}

Not exact syntax, but ball park - VS not here; theres an example in your help files; but I believe to use using in this context the class has to implement the IDisposable interface - I could be way off on base, but I remember something about it because the whole purpose of using it like this is to ensure an object is disposed after youve used it.

So obviously this isnt traditionally used as you would With...End With, it used it strange situations and its generally just easier to add the using line at the top of the document; that is the only keyword I miss from VB.


The using statement is pretty neat. However it has a completely different use to the with statement in VB.

It is used for making sure that your objects is always terminated (couldnt think of a better word) even when there is an exception. The variable or object can only exist within the using statement.
 
Another note on the "using" block: in the syntax described above, it only works for creating a disposable object (anything supporting IDisposable). You cant use it for a DataColumn object for example.

-ner
 
Back
Top