473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C#NET2008 Using WITH

126 100+
Hullo Good Guys,
I need your help.

I am very puzzled about using WITH with C#NET2008.

The coding below when used in VB.NET
I will be using WITH :
Expand|Select|Wrap|Line Numbers
  1. Wth me.ListBoxCust
  2.     .ColumnWidth = this.ListBoxCust.Width;
  3.     .DisplayMember = "Companyname";
  4.     .ValueMember  ="CustomerID";
  5.     .DataSource = DS.Tables["Cust"];
  6. End with
  7.  
------------------------------------------------
When coding with C#NET2008 how do I use WITH
on the same coding as above


this.ListBoxCust.ColumnWidth = this.ListBoxCust.Width;
this.ListBoxCust.DisplayMember = "Companyname";
this.ListBoxCust.ValueMember ="CustomerID";
this.ListBoxCust.DataSource = DS.Tables["Cust"];
Oct 12 '10 #1
9 1415
hype261
207 100+
I don't believe there is an equivalent of the With in the C# language. Other than that you don't have to use this unless you have a naming conflict. The only time you have to use this is in the situation below assuming your class has a member variable x and some method called foo.

Expand|Select|Wrap|Line Numbers
  1. public foo(int x)
  2. {
  3.  
  4. this.x = x;
  5.  
  6. }
  7.  
Oct 13 '10 #2
lenniekuah
126 100+
Hi Hype 1261,
thank you for sharing your thought with me.
The reason of me wanting to use WITH in C#NET2008 is to reduce the coding. I did not realised that it does not works in C#NET2008
Oct 13 '10 #3
Aimee Bailey
197 Expert 100+
In VB.Net the With statement is a short-cut that I, and many others feel is an ugly way of going about things, in c# the statement was not included because the following is much the same, and reads more logically:

Expand|Select|Wrap|Line Numbers
  1. TimeSpan a = ALongVariableName;
  2.  
  3. MessageBox.Show(a.Days.ToString());
  4. MessageBox.Show(a.Hours.ToString());
  5. MessageBox.Show(a.Minutes.ToString());
  6.  
Hope this helps.

Aimee.
Oct 13 '10 #4
Christian Binder
218 Expert 100+
There's no equivalent in c#, but you are able to initialize on creation of the object, e.g.
Expand|Select|Wrap|Line Numbers
  1. this.ListBoxCust = new ListBox() {
  2.   ColumnWidth = this.ListBoxCust.Width, //I don't know if this line works ...
  3.   DisplayMember = "Companyname",
  4.   ValueMember ="CustomerID",
  5.   DataSource = DS.Tables["Cust"]
  6. };
  7.  
If you'd like to use With for structural purposes, you can use regions or curly brackets e.g.
Expand|Select|Wrap|Line Numbers
  1. {
  2.   this.ListBoxCust.ColumnWidth = this.ListBoxCust.Width;
  3.   this.ListBoxCust.DisplayMember = "Companyname";
  4.   this.ListBoxCust.ValueMember ="CustomerID";
  5.   this.ListBoxCust.DataSource = DS.Tables["Cust"]; 
  6. }
  7.  
  8. //or
  9. #region ListBoxCust-Initialization
  10.   this.ListBoxCust.ColumnWidth = this.ListBoxCust.Width;
  11.   this.ListBoxCust.DisplayMember = "Companyname";
  12.   this.ListBoxCust.ValueMember ="CustomerID";
  13.   this.ListBoxCust.DataSource = DS.Tables["Cust"]; 
  14. #endregion ListBoxCust-Initialization
  15.  
Oct 13 '10 #5
GaryTexmo
1,501 Expert 1GB
In VB.Net the With statement is a short-cut that I, and many others feel is an ugly way of going about things, in c# the statement was not included because the following is much the same, and reads more logically:

Expand|Select|Wrap|Line Numbers
  1. TimeSpan a = ALongVariableName;
  2.  
  3. MessageBox.Show(a.Days.ToString());
  4. MessageBox.Show(a.Hours.ToString());
  5. MessageBox.Show(a.Minutes.ToString());
Hope this helps.

Aimee.
Just a quick note on this... when you do stuff like this I think it's best to do it in a using statement.

Expand|Select|Wrap|Line Numbers
  1. using (TimeSpan a = ALongVariableName)
  2. {
  3.   MessageBox.Show(a.Days.ToString());
  4.   ...
  5. }
The reason for this is that anything in that using statement gets disposed of at the end of the scope. I did some tests a while back and it looks like objects don't necessarily get destroyed right as they go out of scope, they hang around until the GC feels like cleaning things up. Doing this, your memory allocation for a will be gone at the end of the using statement.

Just keeps things tidy :)
Oct 13 '10 #6
Curtis Rutland
3,256 Expert 2GB
I typically only use the using statements for objects that implement IDisposable.

You are correct about garbage collection (it happens when the environment decides it's necessary), but it's not such a bad thing.

On the other hand, there are several object types that must be disposed of, such as Streams and DataConnections. The great thing about the using statement is that the Dispose method of any IDisposable is called upon exiting the block.

You can manually handle this if you have to use an object across multiple scopes, but it's nice to be able to do:

Expand|Select|Wrap|Line Numbers
  1. using(StreamWriter sw = new StreamWriter(path))
  2.   sw.Write(output);
And not have to worry about flushing and closing my stream.

We've gone far afield though. As others have pointed out, there is no equivalent of with in C#. The closest you can get to that is initializing properties upon creation, as Christian mentioned.
Oct 13 '10 #7
Aimee Bailey
197 Expert 100+
Only fear id have with using using's (get it ;)) so often, would be that the following:

Expand|Select|Wrap|Line Numbers
  1. using (TimeSpan a = ALongVariableName)
  2. {
  3.    MessageBox.Show(a.Days.ToString());
  4.    ...
  5. }
  6.  
might also dispose the variable ALongVariableName, I think when it come's to C#, sometimes we just have to trust that the GC knows what its doing, plus on the bright side, its better than the one in mono hehe.

Aimee.
Oct 14 '10 #8
GaryTexmo
1,501 Expert 1GB
My first thought when I read that was, "There's no way Microsoft would be that dumb!" and yet, here we are.

Expand|Select|Wrap|Line Numbers
  1.             Form theForm = new Form();
  2.             theForm.Disposed += new EventHandler(theForm_Disposed);
  3.             theForm.Text = "This is a form!";
  4.  
  5.             Console.WriteLine(theForm.Text);
  6.  
  7.             using (Form a = theForm)
  8.             {
  9.                 Console.WriteLine(a.Size);
  10.             }
  11.  
  12.             theForm.ShowDialog();
  13.             Console.WriteLine(theForm.Text);
The ShowDialog line throws a disposed exception... you are indeed correct, Aimee. In light of this, I don't think it's appropriate to use what I suggested as an alternative to VB's "with" after all.

Good catch!
Oct 14 '10 #9
Curtis Rutland
3,256 Expert 2GB
It's not really dumb, it's an intended thing. They mention it on the MSDN page for "using statement." They also mention that it's not a recommended usage, because of that exact issue.
Oct 14 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Ron | last post by:
If I wrap a SQLConnection with a using (...){} block will the connection automatically be closed if an exception occures? Or will I still need a try block to catch the exception and close the...
0
by: Mark Micallef | last post by:
Hi, I'm having a problem using a databound dropdownlist inside a reorderlist ajax control. Here's a snippet of the code I'm using: <InsertItemTemplate> <div class="insertArea">...
1
by: Mark Micallef | last post by:
Hi, this question relates to a control in the Ajax toolkit for asp.net 2. I'm having a problem using a databound dropdownlist inside a reorderlist ajax control. Here's a snippet of the code I'm...
0
by: Andreas Schmitt | last post by:
I wrote a small timer class for use in a graphics engine I am working on for teaching myself. The small time based animations I tried with it seem to work fine but displaying the frame rate I...
0
by: hussainiyad | last post by:
Dear anyone can help me plz , i,m starting a project chat room using with peer to peer concept with asp.net and c# , can u plz tell me what is the coding or any guidence
0
by: abhilash12 | last post by:
is there any searchengine using with java for find word in ms word file pls help me
2
by: DC | last post by:
Hi, I am using a GridView to present data in a DataTable, which I store only in ViewState and when the user hits the "OK" button the rows in the DataTable will be used to execute transactions. ...
1
by: amit saini | last post by:
Hi I have using back end sql server 2000, fornt end Visual Basic and For reporting using Ms-Word 2000. I have face a problem . In sql server i have using Image type data type and stored a image in...
1
by: kelleram | last post by:
Has anyone been able to successfully create a Merge Into Statement using 'With UR' as part of the select? I have the following statement: MERGE INTO TABLE C USING(SELECT ...) AS S ON...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.