-
January 30th, 2008, 12:26 AM
#1
Syntax Question
Hey everyone,
I don't work with C# often, so forgive my ignorance.
In any case, I'm currently using objects that are deeply nested in various namespaces, sort of like this:
Code:
A.B.C.D.E obj = new A.B.C.D.E();
My question: does C# have a method of automatically resolving the correct object type on the right-hand side of the assignment? Ideally, I'd love to be able to say:
Code:
A.B.C.D.E obj = new E();
This, however, doesn't work... I was just wondering if there is something similar that I can use to get the same effect.
Thanks in advance!
P.S.
I'm not referring to using "using ...".
P.P.S.
Actually, if using "using ..." is my only option, is there a way to make the using statement local to a certain scope like in C++? Perhaps there is also namespace aliasing available?
Last edited by Plasmator; January 30th, 2008 at 12:30 AM.
-
January 30th, 2008, 01:32 AM
#2
Re: Syntax Question
C# requires a fully qualified type name. If you haven't specified that you're 'using' a particular namespace in a code file then you have to fully qualify any member of that namespace every time you use it. A 'using' statement refers to the current code file, no more, no less. You normally import namespaces but you can import types too. Aliasing is supported.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LVI = System.Windows.Forms.ListViewItem;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
LVI item = new LVI("Item");
item.SubItems.Add(new LVI.ListViewSubItem(item, "Subitem"));
}
}
}
-
January 30th, 2008, 02:53 AM
#3
Re: Syntax Question
Originally Posted by jmcilhinney
...
Awesome, thank you. I'm starting to kinda like C# now.
Oh and, so C# has no support for local scope aliasing? That is, I can't do something like this:
Code:
//we're within a function
{
using LVI = System.Windows.Forms.ListViewItem;
LVI item = new LVI("Item");
item.SubItems.Add(new LVI.ListViewSubItem(item, "Subitem"));
}
//...
Right?
Last edited by Plasmator; January 30th, 2008 at 02:56 AM.
-
January 30th, 2008, 04:27 AM
#4
Re: Syntax Question
You can use a 'using' directive within a namespace block as long as it's at the top, but it's not valid anywhere inside a type definition.
-
January 30th, 2008, 04:49 AM
#5
Re: Syntax Question
Originally Posted by jmcilhinney
You can use a 'using' directive within a namespace block as long as it's at the top, but it's not valid anywhere inside a type definition.
Great, thanks for the clarification!
-
January 30th, 2008, 08:35 AM
#6
Re: Syntax Question
If you are using the Visual Studio IDE, you can just type in the class name [eg ListViewItem], right click and select resolve. It will give you the option of putting the proper using statement at the topof the file automatically (however it will not automatically alias).
Personally I have a strong dislike (and generally forbid) aliasing. It can make the code very difficult to read and understand. The BCL (Base Class Library) has unique leaf names. This is why there is SqlDataAdapter and OleDbDataAdapter classes ranther than just DataAdaptr classes in two different namespaces.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
January 30th, 2008, 04:34 PM
#7
Re: Syntax Question
Originally Posted by TheCPUWizard
Personally I have a strong dislike (and generally forbid) aliasing. It can make the code very difficult to read and understand. The BCL (Base Class Library) has unique leaf names. This is why there is SqlDataAdapter and OleDbDataAdapter classes ranther than just DataAdaptr classes in two different namespaces.
Thanks Wizard, you make a good point.
P.S.
A style question: when working in C++, I never use the "using" statement, i.e., I prefer to write "a::b" instead of "using namespace a; /*...*/ b".
I've noticed that a lot of C# code relies on "using" statements. So I'm wondering if this practice is widely accepted in the C# community?
Should I be writing:
Code:
using System.Net.Mail;
//...
MailMessage msg = new MailMessage();
As opposed to:
Code:
//...
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
Same question with "primitives" -- i.e., does anyone ever write "System.Int32" instead of "int"?
Thanks in advance, sorry if this sounds silly, just trying to get into the right mindset.
Last edited by Plasmator; January 30th, 2008 at 04:46 PM.
-
January 30th, 2008, 04:54 PM
#8
Re: Syntax Question
For me, I prefer to use the using xxxx statement at the top of the file. The only time I don't do it that way is when objects from different namespaces will conflict. I do it this way because I find the following to reeuire less typing and be more readable
Code:
MailMessage msg = new MailMessage();
than
Code:
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
For the other, I prefer the C# built-in types over the .net primitives.
-
January 30th, 2008, 05:10 PM
#9
Re: Syntax Question
Complete agreement with Arjay.
Personally I VERY rarely find names to conflict (maybe once or twice a year) with using statements at the top. This is probably because of the way I factor my code combined with the fact that Microsoft made a deliberate effort to avoid potential naming conflicts in high probablaity areas.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
January 30th, 2008, 05:14 PM
#10
Re: Syntax Question
Cool, thanks for your input guys.
-
January 30th, 2008, 05:26 PM
#11
Re: Syntax Question
I'm not sure anyone's opinion on this but I always put the using statements inside the namespace (as opposed to at the top of the file).
E.g.
Code:
namespace MyCompany.MyApp.MyComponent
{
#region Using Directives
using System;
using System.Runtime.Serialization;
using System.ServiceModel;
#endregion Using Directives
public class MyClass
{
// etc.
Other than style considerations (I like it but you may not), is there any other reason not to do it this way?
-
January 30th, 2008, 05:33 PM
#12
Re: Syntax Question
Originally Posted by Arjay
I'm not sure anyone's opinion on this but I always put the using statements inside the namespace (as opposed to at the top of the file).
E.g.
Code:
namespace MyCompany.MyApp.MyComponent
{
#region Using Directives
using System;
using System.Runtime.Serialization;
using System.ServiceModel;
#endregion Using Directives
public class MyClass
{
// etc.
Other than style considerations (I like it but you may not), is there any other reason not to do it this way?
Doing it that way makes those directives local to that namespace block only. That said, if you more than one namespace block in the one code file then you need style check. I think it's clearer with the 'using' directives at the top of the file and, since the IDE puts them there by default, it's also less effort to leave them there.
-
January 30th, 2008, 05:51 PM
#13
Re: Syntax Question
Thanks for your time guys.
One more question: I recall hearing something about an "official" C# style guide a long time ago -- does such a thing exist? If so, is it free for download, and do you guys follow it yourselves?
Thanks again.
-
January 30th, 2008, 05:51 PM
#14
Re: Syntax Question
Originally Posted by jmcilhinney
Doing it that way makes those directives local to that namespace block only. That said, if you more than one namespace block in the one code file then you need style check. I think it's clearer with the 'using' directives at the top of the file and, since the IDE puts them there by default, it's also less effort to leave them there.
Thanks. I just wanted to be sure that there weren't any negative side effects. I only ever have one namespace in a file at a time.
I should mention that if you move them as I've done here, and use the 'Resolve' menu item, the IDE is smart enough to put the new 'using' statement in the correct location.
-
January 31st, 2008, 09:36 AM
#15
Re: Syntax Question
Originally Posted by Plasmator
Thanks for your time guys.
One more question: I recall hearing something about an "official" C# style guide a long time ago -- does such a thing exist? If so, is it free for download, and do you guys follow it yourselves?
Thanks again.
There are a number of style guides out there, including two that I know of that were published by Microsoft. I did not find either of those doing a quick Web Search (http://www.google.com/search?hl=en&q=c%23+style+guide) .
Here at DynConDev we have developed our own, and I typically encourage our clients to each develop their own after reading some of the ones that others use. Years of experience have repeatedly shown that a style that works excellently for one development environment will be less than optimal for another (Naming Conventions, Commenting Requirements, etc).
The important thing is to be consistant. If you ae in a professional (or even amateur team) environment then this should be a witten document.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|