Denaes Posted July 30, 2004 Posted July 30, 2004 Sorry, I really did try to search, but it doesn't work with 'common' words like With. In VB you write: 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#? Quote
bri189a Posted July 30, 2004 Posted July 30, 2004 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; there's 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 you've used it. So obviously this isn't traditionally used as you would 'With...End With', it used it strange situations and it's generally just easier to add the using line at the top of the document; that is the only keyword I miss from VB. Quote
Denaes Posted July 30, 2004 Author Posted July 30, 2004 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 Quote
*Experts* Nerseus Posted July 30, 2004 *Experts* Posted July 30, 2004 Another approach to bri's idea is to just set a new, shorter variable to your object (only works if it's a class - not for structs): DataColumn col = dtDataTable.Columns[1]; col.MaxLength = 5; col.SomeProp = "hi"; ... col = null; The last line is optional (setting col to null). It won't null out the column, only the variable reference col. None of these provides the ".Property" syntax that VB offers, just shortcuts for typing. -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
John_0025 Posted August 23, 2004 Posted August 23, 2004 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; there's 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 you've used it. So obviously this isn't traditionally used as you would 'With...End With', it used it strange situations and it's 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 (couldn't think of a better word) even when there is an exception. The variable or object can only exist within the using statement. Quote
*Experts* Nerseus Posted August 24, 2004 *Experts* Posted August 24, 2004 Another note on the "using" block: in the syntax described above, it only works for creating a disposable object (anything supporting IDisposable). You can't use it for a DataColumn object for example. -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.