Classes and Namespaces

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael C

    Classes and Namespaces

    In Vb a class was a file, but in C# you have multiple classes in one file
    which
    has a namespace.

    I am trying to get some common code libraries written but this is stuffing
    my head up.

    Is it normal in C# to have a namespace for each class file or isn't it a
    class

    I now need to call like this

    <MyNameSpace contained in class file that I added to a solution .
    MyClassName> myclassName = new <1st Part>

    Is this normal I AM SO CONFUSED (Any help welcome!)

    Regards

    Michael



  • Kirk Graves

    #2
    Re: Classes and Namespaces

    Michael,

    Both c# and vb.net have the idea of a Namespace.
    In simple terms, think of a Namespace as a hierarchal organization of your
    classes. If you look at the MS .Net Framework, all the classes are
    organized by the sort of thing they are used for (kind of like the way you
    organize files in a directory structure so you know were to find the
    specific file you are looking for without having all files sitting at c:\).
    System.Data is all the data stuff, System.IO is all the stream and file io
    stuff. We use namespaces in our code for the same reason, just to organize
    the classes into something that is easier to deal with.

    In C#, each file in the project can have 1 or more classes. Usually, each
    file has a Namespace that encloses the class(es) in the file. That same
    Namespace can be used in other files as well.

    The important part to remember is that Namespaces are not objects, just a
    way of organizing things (I expect I will take some flak for that last
    statement, but without making things too complicated it is basicly true.)

    Kirk Graves
    KRGIT Software

    "Michael C" <askme@askme.co m> wrote in message
    news:OULuLyK%23 DHA.1632@TK2MSF TNGP12.phx.gbl. ..[color=blue]
    > In Vb a class was a file, but in C# you have multiple classes in one file
    > which
    > has a namespace.
    >
    > I am trying to get some common code libraries written but this is stuffing
    > my head up.
    >
    > Is it normal in C# to have a namespace for each class file or isn't it a
    > class
    >
    > I now need to call like this
    >
    > <MyNameSpace contained in class file that I added to a solution .
    > MyClassName> myclassName = new <1st Part>
    >
    > Is this normal I AM SO CONFUSED (Any help welcome!)
    >
    > Regards
    >
    > Michael
    >
    >
    >[/color]


    Comment

    • Kirk Graves

      #3
      Re: Classes and Namespaces

      Michael,

      Both c# and vb.net have the idea of a Namespace.
      In simple terms, think of a Namespace as a hierarchal organization of your
      classes. If you look at the MS .Net Framework, all the classes are
      organized by the sort of thing they are used for (kind of like the way you
      organize files in a directory structure so you know were to find the
      specific file you are looking for without having all files sitting at c:\).
      System.Data is all the data stuff, System.IO is all the stream and file io
      stuff. We use namespaces in our code for the same reason, just to organize
      the classes into something that is easier to deal with.

      In C#, each file in the project can have 1 or more classes. Usually, each
      file has a Namespace that encloses the class(es) in the file. That same
      Namespace can be used in other files as well.

      The important part to remember is that Namespaces are not objects, just a
      way of organizing things (I expect I will take some flak for that last
      statement, but without making things too complicated it is basicly true.)

      Kirk Graves
      KRGIT Software

      "Michael C" <askme@askme.co m> wrote in message
      news:OULuLyK%23 DHA.1632@TK2MSF TNGP12.phx.gbl. ..[color=blue]
      > In Vb a class was a file, but in C# you have multiple classes in one file
      > which
      > has a namespace.
      >
      > I am trying to get some common code libraries written but this is stuffing
      > my head up.
      >
      > Is it normal in C# to have a namespace for each class file or isn't it a
      > class
      >
      > I now need to call like this
      >
      > <MyNameSpace contained in class file that I added to a solution .
      > MyClassName> myclassName = new <1st Part>
      >
      > Is this normal I AM SO CONFUSED (Any help welcome!)
      >
      > Regards
      >
      > Michael
      >
      >
      >[/color]


      Comment

      • Sunny

        #4
        Re: Classes and Namespaces

        Hi Michael,
        Every class belong to a namespace. This is usefull, because now you may have
        classes with equal names, which belongs to different namespaces, and behave
        differently without any confusion for the compiler or the user of your
        class. As an example, assume that you have 2 namespaces: TCPcomunication s
        and HTTPcomunicatio ns. Now in every namespace you can create
        ConnectionClass , and it will be different for evry namespace.
        And in your code you can clearly identify the exact class you want to use:
        TCPcomunication s.ConnectionCla ss or HTTPcomunicatio ns.ConnectionCl ass.

        You can have nested namespaces, like MyCompany.MyFir stProject.BaseO bjects.
        If you create a class in that namespace - MyClass, this class will be
        completeley different from the class MyClass, created by someone else in
        other namespace(s), and in your code you can be sure that you are using the
        right one.

        Also, if you do not want in your code always to type:
        MyCompany.MyFir stProject.BaseO bjects.MyClass, you can use the "using"
        keyword:

        using MyCompany.MyFir stProject.BaseO bjects

        .... and later in the code, you can use:
        MyClass obj = new MyClass;

        Hope that helps
        Sunny
        Michael C wrote:
        [color=blue]
        > In Vb a class was a file, but in C# you have multiple classes in one file
        > which
        > has a namespace.
        >
        > I am trying to get some common code libraries written but this is stuffing
        > my head up.
        >
        > Is it normal in C# to have a namespace for each class file or isn't it a
        > class
        >
        > I now need to call like this
        >
        > <MyNameSpace contained in class file that I added to a solution .
        > MyClassName> myclassName = new <1st Part>
        >
        > Is this normal I AM SO CONFUSED (Any help welcome!)
        >
        > Regards
        >
        > Michael[/color]


        Comment

        • Sunny

          #5
          Re: Classes and Namespaces

          Hi Michael,
          Every class belong to a namespace. This is usefull, because now you may have
          classes with equal names, which belongs to different namespaces, and behave
          differently without any confusion for the compiler or the user of your
          class. As an example, assume that you have 2 namespaces: TCPcomunication s
          and HTTPcomunicatio ns. Now in every namespace you can create
          ConnectionClass , and it will be different for evry namespace.
          And in your code you can clearly identify the exact class you want to use:
          TCPcomunication s.ConnectionCla ss or HTTPcomunicatio ns.ConnectionCl ass.

          You can have nested namespaces, like MyCompany.MyFir stProject.BaseO bjects.
          If you create a class in that namespace - MyClass, this class will be
          completeley different from the class MyClass, created by someone else in
          other namespace(s), and in your code you can be sure that you are using the
          right one.

          Also, if you do not want in your code always to type:
          MyCompany.MyFir stProject.BaseO bjects.MyClass, you can use the "using"
          keyword:

          using MyCompany.MyFir stProject.BaseO bjects

          .... and later in the code, you can use:
          MyClass obj = new MyClass;

          Hope that helps
          Sunny
          Michael C wrote:
          [color=blue]
          > In Vb a class was a file, but in C# you have multiple classes in one file
          > which
          > has a namespace.
          >
          > I am trying to get some common code libraries written but this is stuffing
          > my head up.
          >
          > Is it normal in C# to have a namespace for each class file or isn't it a
          > class
          >
          > I now need to call like this
          >
          > <MyNameSpace contained in class file that I added to a solution .
          > MyClassName> myclassName = new <1st Part>
          >
          > Is this normal I AM SO CONFUSED (Any help welcome!)
          >
          > Regards
          >
          > Michael[/color]


          Comment

          • Michael C

            #6
            Re: Classes and Namespaces

            Hi

            So I could use the using statement as long as the class name didn't exist in
            another namespace otherwise I would get a compile error correct (Correct?)

            And also if I create a dll should I use the same name as the namespace
            within the dll correct!

            But what happens if I have the following MyDllName added as a reference to a
            solution and
            2 namespaces within that Dll ... or is that usually not good coding
            practice?

            Regards

            Michael


            "Sunny" <sunnyask@icebe rgwireless.com> wrote in message
            news:%23PQzdCL% 23DHA.1452@TK2M SFTNGP09.phx.gb l...[color=blue]
            > Hi Michael,
            > Every class belong to a namespace. This is usefull, because now you may[/color]
            have[color=blue]
            > classes with equal names, which belongs to different namespaces, and[/color]
            behave[color=blue]
            > differently without any confusion for the compiler or the user of your
            > class. As an example, assume that you have 2 namespaces: TCPcomunication s
            > and HTTPcomunicatio ns. Now in every namespace you can create
            > ConnectionClass , and it will be different for evry namespace.
            > And in your code you can clearly identify the exact class you want to use:
            > TCPcomunication s.ConnectionCla ss or HTTPcomunicatio ns.ConnectionCl ass.
            >
            > You can have nested namespaces, like MyCompany.MyFir stProject.BaseO bjects.
            > If you create a class in that namespace - MyClass, this class will be
            > completeley different from the class MyClass, created by someone else in
            > other namespace(s), and in your code you can be sure that you are using[/color]
            the[color=blue]
            > right one.
            >
            > Also, if you do not want in your code always to type:
            > MyCompany.MyFir stProject.BaseO bjects.MyClass, you can use the "using"
            > keyword:
            >
            > using MyCompany.MyFir stProject.BaseO bjects
            >
            > ... and later in the code, you can use:
            > MyClass obj = new MyClass;
            >
            > Hope that helps
            > Sunny
            > Michael C wrote:
            >[color=green]
            > > In Vb a class was a file, but in C# you have multiple classes in one[/color][/color]
            file[color=blue][color=green]
            > > which
            > > has a namespace.
            > >
            > > I am trying to get some common code libraries written but this is[/color][/color]
            stuffing[color=blue][color=green]
            > > my head up.
            > >
            > > Is it normal in C# to have a namespace for each class file or isn't it a
            > > class
            > >
            > > I now need to call like this
            > >
            > > <MyNameSpace contained in class file that I added to a solution .
            > > MyClassName> myclassName = new <1st Part>
            > >
            > > Is this normal I AM SO CONFUSED (Any help welcome!)
            > >
            > > Regards
            > >
            > > Michael[/color]
            >
            >[/color]


            Comment

            • Michael C

              #7
              Re: Classes and Namespaces

              Hi

              So I could use the using statement as long as the class name didn't exist in
              another namespace otherwise I would get a compile error correct (Correct?)

              And also if I create a dll should I use the same name as the namespace
              within the dll correct!

              But what happens if I have the following MyDllName added as a reference to a
              solution and
              2 namespaces within that Dll ... or is that usually not good coding
              practice?

              Regards

              Michael


              "Sunny" <sunnyask@icebe rgwireless.com> wrote in message
              news:%23PQzdCL% 23DHA.1452@TK2M SFTNGP09.phx.gb l...[color=blue]
              > Hi Michael,
              > Every class belong to a namespace. This is usefull, because now you may[/color]
              have[color=blue]
              > classes with equal names, which belongs to different namespaces, and[/color]
              behave[color=blue]
              > differently without any confusion for the compiler or the user of your
              > class. As an example, assume that you have 2 namespaces: TCPcomunication s
              > and HTTPcomunicatio ns. Now in every namespace you can create
              > ConnectionClass , and it will be different for evry namespace.
              > And in your code you can clearly identify the exact class you want to use:
              > TCPcomunication s.ConnectionCla ss or HTTPcomunicatio ns.ConnectionCl ass.
              >
              > You can have nested namespaces, like MyCompany.MyFir stProject.BaseO bjects.
              > If you create a class in that namespace - MyClass, this class will be
              > completeley different from the class MyClass, created by someone else in
              > other namespace(s), and in your code you can be sure that you are using[/color]
              the[color=blue]
              > right one.
              >
              > Also, if you do not want in your code always to type:
              > MyCompany.MyFir stProject.BaseO bjects.MyClass, you can use the "using"
              > keyword:
              >
              > using MyCompany.MyFir stProject.BaseO bjects
              >
              > ... and later in the code, you can use:
              > MyClass obj = new MyClass;
              >
              > Hope that helps
              > Sunny
              > Michael C wrote:
              >[color=green]
              > > In Vb a class was a file, but in C# you have multiple classes in one[/color][/color]
              file[color=blue][color=green]
              > > which
              > > has a namespace.
              > >
              > > I am trying to get some common code libraries written but this is[/color][/color]
              stuffing[color=blue][color=green]
              > > my head up.
              > >
              > > Is it normal in C# to have a namespace for each class file or isn't it a
              > > class
              > >
              > > I now need to call like this
              > >
              > > <MyNameSpace contained in class file that I added to a solution .
              > > MyClassName> myclassName = new <1st Part>
              > >
              > > Is this normal I AM SO CONFUSED (Any help welcome!)
              > >
              > > Regards
              > >
              > > Michael[/color]
              >
              >[/color]


              Comment

              Working...