CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Syntax Question

  1. #1
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    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.

  2. #2
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    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")); 
            }
        }
    }
    Tutorials: Home & Learn | Start VB.NET | Learn VB.NET | C# Station | GotDotNet | Games in VB.NET 101 Samples: 2002 | 2003 | 2005 | More .NET 2.0 (VB.NET, C#) Articles: VB.NET | C# | ASP.NET | MoreFree Components: WFC | XPCC | ElementsEx | VBPP | Mentalis | ADO.NET/MySQL | VisualStyles | Charting (NPlot, ZedGraph) | iTextSharp (PDF) | SDF (CF) ● Free Literature: VB 2005 (eBook) | VB6 to VB.NET (eBook) | MSDN Magazine (CHM format) ● Bookmarks: MSDN | WinForms .NET | ASP.NET | WinForms FAQ | WebForms FAQ | GotDotNet | Code Project | DevBuzz (CF) ● Code Converter: C#/VB.NET | VB.NET/C# | VS 2005 add-in

  3. #3
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Syntax Question

    Quote 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.

  4. #4
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    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.
    Tutorials: Home & Learn | Start VB.NET | Learn VB.NET | C# Station | GotDotNet | Games in VB.NET 101 Samples: 2002 | 2003 | 2005 | More .NET 2.0 (VB.NET, C#) Articles: VB.NET | C# | ASP.NET | MoreFree Components: WFC | XPCC | ElementsEx | VBPP | Mentalis | ADO.NET/MySQL | VisualStyles | Charting (NPlot, ZedGraph) | iTextSharp (PDF) | SDF (CF) ● Free Literature: VB 2005 (eBook) | VB6 to VB.NET (eBook) | MSDN Magazine (CHM format) ● Bookmarks: MSDN | WinForms .NET | ASP.NET | WinForms FAQ | WebForms FAQ | GotDotNet | Code Project | DevBuzz (CF) ● Code Converter: C#/VB.NET | VB.NET/C# | VS 2005 add-in

  5. #5
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Syntax Question

    Quote 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!

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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

  7. #7
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Syntax Question

    Quote 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.

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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

  10. #10
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Syntax Question

    Cool, thanks for your input guys.

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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?

  12. #12
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: Syntax Question

    Quote 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.
    Tutorials: Home & Learn | Start VB.NET | Learn VB.NET | C# Station | GotDotNet | Games in VB.NET 101 Samples: 2002 | 2003 | 2005 | More .NET 2.0 (VB.NET, C#) Articles: VB.NET | C# | ASP.NET | MoreFree Components: WFC | XPCC | ElementsEx | VBPP | Mentalis | ADO.NET/MySQL | VisualStyles | Charting (NPlot, ZedGraph) | iTextSharp (PDF) | SDF (CF) ● Free Literature: VB 2005 (eBook) | VB6 to VB.NET (eBook) | MSDN Magazine (CHM format) ● Bookmarks: MSDN | WinForms .NET | ASP.NET | WinForms FAQ | WebForms FAQ | GotDotNet | Code Project | DevBuzz (CF) ● Code Converter: C#/VB.NET | VB.NET/C# | VS 2005 add-in

  13. #13
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    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.

  14. #14
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Syntax Question

    Quote 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.

  15. #15
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Syntax Question

    Quote 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

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured