Help With Nesting Classes In Library

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • joey.powell@topscene.com

    Help With Nesting Classes In Library

    I am writing a class that will do some binary file IO. The class will
    need to read a header from the binary file, and it will also need to
    read a varying number of records in the file. I currently have two main
    methods: Open(string FilePath) and Close(). I would like to read all of
    the data into variables whenever Open is called. I would like to have a
    Header (sub?)class within the main class, and maybe a collection or
    array for the records. The following is a snippet of what I have so
    far...


    =============== =========

    using System;
    using System.Collecti ons.Generic;
    using System.IO;
    using System.Text;

    namespace MyNameSpace
    {
    public class MyClass
    {
    private FileStream fsMyFile;
    private BinaryReader brMyFile;

    public void Open(string FilePath)
    {
    fsMyFile = new FileStream(File Path, FileMode.Open,
    FileAccess.Read Write);
    brMyFile = new BinaryReader(fs MyFile);

    Header.MyCode = 2;
    }

    public void Close()
    {
    brMyFile.Close( );
    fsMyFile.Close( );
    }

    public static class Header
    {
    private static int _MyCode;
    public static int MyCode
    {
    get { return _MyCode; }
    set { _MyCode = value; }
    }
    }
    }
    }

    =============== =========

    I want to be able to reference this dll to other projects and then use
    it like...

    MyClass someclass = new MyClass();
    someclass.Open( "[path to file]");
    TextBox1.Text = (someclass.Head er.MyCode).ToSt ring();
    someclass.Close ();

    How can I get the someclass.Heade r.MyCode syntax to work? Will it not
    work now because I am putting the header class inside the main class?
    Are you confused as I am?

    Any suggestions would be appreciated.

  • Bruce Wood

    #2
    Re: Help With Nesting Classes In Library

    joey.powell@top scene.com wrote:[color=blue]
    > I am writing a class that will do some binary file IO. The class will
    > need to read a header from the binary file, and it will also need to
    > read a varying number of records in the file. I currently have two main
    > methods: Open(string FilePath) and Close(). I would like to read all of
    > the data into variables whenever Open is called. I would like to have a
    > Header (sub?)class within the main class, and maybe a collection or
    > array for the records. The following is a snippet of what I have so
    > far...[/color]

    You're right: there's some confusion here. I've made some notes
    in-line.
    [color=blue]
    > using System;
    > using System.Collecti ons.Generic;
    > using System.IO;
    > using System.Text;
    >
    > namespace MyNameSpace
    > {
    > public class MyClass
    > {
    > private FileStream fsMyFile;
    > private BinaryReader brMyFile;
    >
    > public void Open(string FilePath)
    > {
    > fsMyFile = new FileStream(File Path, FileMode.Open,
    > FileAccess.Read Write);
    > brMyFile = new BinaryReader(fs MyFile);
    >[/color]

    The following line should compile fine. It doesn't matter that Header
    is nested inside MyClass: you're just setting a static property of the
    class Header to be 2.
    [color=blue]
    > Header.MyCode = 2;
    > }
    >
    > public void Close()
    > {
    > brMyFile.Close( );
    > fsMyFile.Close( );
    > }
    >
    > public static class Header
    > {
    > private static int _MyCode;
    > public static int MyCode
    > {
    > get { return _MyCode; }
    > set { _MyCode = value; }
    > }
    > }
    > }
    > }
    >
    > =============== =========
    >
    > I want to be able to reference this dll to other projects and then use
    > it like...
    >
    > MyClass someclass = new MyClass();
    > someclass.Open( "[path to file]");[/color]

    The difficulty with the following is that you're trying to use a static
    class name as though it were a property name, and an instance property
    at that. The first thing you have to decide is whether MyCode is an
    instance-level thing (that is, there is one MyCode for each Header that
    you create) or whether it's a static thing (that is, there is only one
    MyCode in the entire program, no matter how many Headers or MyClasses
    you instantiate). Once you have that sorted out, I can give you more
    information on how to achieve what you want.

    Comment

    • joey.powell@topscene.com

      #3
      Re: Help With Nesting Classes In Library

      There will be exactly one MyCode for the header. There is only one
      header.

      Bruce Wood wrote:[color=blue]
      > joey.powell@top scene.com wrote:[color=green]
      > > I am writing a class that will do some binary file IO. The class will
      > > need to read a header from the binary file, and it will also need to
      > > read a varying number of records in the file. I currently have two main
      > > methods: Open(string FilePath) and Close(). I would like to read all of
      > > the data into variables whenever Open is called. I would like to have a
      > > Header (sub?)class within the main class, and maybe a collection or
      > > array for the records. The following is a snippet of what I have so
      > > far...[/color]
      >
      > You're right: there's some confusion here. I've made some notes
      > in-line.
      >[color=green]
      > > using System;
      > > using System.Collecti ons.Generic;
      > > using System.IO;
      > > using System.Text;
      > >
      > > namespace MyNameSpace
      > > {
      > > public class MyClass
      > > {
      > > private FileStream fsMyFile;
      > > private BinaryReader brMyFile;
      > >
      > > public void Open(string FilePath)
      > > {
      > > fsMyFile = new FileStream(File Path, FileMode.Open,
      > > FileAccess.Read Write);
      > > brMyFile = new BinaryReader(fs MyFile);
      > >[/color]
      >
      > The following line should compile fine. It doesn't matter that Header
      > is nested inside MyClass: you're just setting a static property of the
      > class Header to be 2.
      >[color=green]
      > > Header.MyCode = 2;
      > > }
      > >
      > > public void Close()
      > > {
      > > brMyFile.Close( );
      > > fsMyFile.Close( );
      > > }
      > >
      > > public static class Header
      > > {
      > > private static int _MyCode;
      > > public static int MyCode
      > > {
      > > get { return _MyCode; }
      > > set { _MyCode = value; }
      > > }
      > > }
      > > }
      > > }
      > >
      > > =============== =========
      > >
      > > I want to be able to reference this dll to other projects and then use
      > > it like...
      > >
      > > MyClass someclass = new MyClass();
      > > someclass.Open( "[path to file]");[/color]
      >
      > The difficulty with the following is that you're trying to use a static
      > class name as though it were a property name, and an instance property
      > at that. The first thing you have to decide is whether MyCode is an
      > instance-level thing (that is, there is one MyCode for each Header that
      > you create) or whether it's a static thing (that is, there is only one
      > MyCode in the entire program, no matter how many Headers or MyClasses
      > you instantiate). Once you have that sorted out, I can give you more
      > information on how to achieve what you want.[/color]

      Comment

      • joey.powell@topscene.com

        #4
        Re: Help With Nesting Classes In Library

        I just want a logical way to oranize the information and to make it
        accessible from function calls to the DLL.

        I have one main file that contains a header and several, varying length
        records.

        I want to set it up so that I can create an instance of the class...

        MyClass someclass = new MyClass();

        and then call the open method...

        someclass.Open( );

        Once the open method is called I would like to be able to do things
        like...

        someclass.Heade r.ReadMyCode;
        someclass.Heade r.WriteMyCode;

        I can handle all of the code to do the reading and writing...I just
        need to organize the classes, properties, etc... better.

        Any suggestions appreaciated.


        joey.powell@top scene.com wrote:[color=blue]
        > There will be exactly one MyCode for the header. There is only one
        > header.
        >
        > Bruce Wood wrote:[color=green]
        > > joey.powell@top scene.com wrote:[color=darkred]
        > > > I am writing a class that will do some binary file IO. The class will
        > > > need to read a header from the binary file, and it will also need to
        > > > read a varying number of records in the file. I currently have two main
        > > > methods: Open(string FilePath) and Close(). I would like to read all of
        > > > the data into variables whenever Open is called. I would like to have a
        > > > Header (sub?)class within the main class, and maybe a collection or
        > > > array for the records. The following is a snippet of what I have so
        > > > far...[/color]
        > >
        > > You're right: there's some confusion here. I've made some notes
        > > in-line.
        > >[color=darkred]
        > > > using System;
        > > > using System.Collecti ons.Generic;
        > > > using System.IO;
        > > > using System.Text;
        > > >
        > > > namespace MyNameSpace
        > > > {
        > > > public class MyClass
        > > > {
        > > > private FileStream fsMyFile;
        > > > private BinaryReader brMyFile;
        > > >
        > > > public void Open(string FilePath)
        > > > {
        > > > fsMyFile = new FileStream(File Path, FileMode.Open,
        > > > FileAccess.Read Write);
        > > > brMyFile = new BinaryReader(fs MyFile);
        > > >[/color]
        > >
        > > The following line should compile fine. It doesn't matter that Header
        > > is nested inside MyClass: you're just setting a static property of the
        > > class Header to be 2.
        > >[color=darkred]
        > > > Header.MyCode = 2;
        > > > }
        > > >
        > > > public void Close()
        > > > {
        > > > brMyFile.Close( );
        > > > fsMyFile.Close( );
        > > > }
        > > >
        > > > public static class Header
        > > > {
        > > > private static int _MyCode;
        > > > public static int MyCode
        > > > {
        > > > get { return _MyCode; }
        > > > set { _MyCode = value; }
        > > > }
        > > > }
        > > > }
        > > > }
        > > >
        > > > =============== =========
        > > >
        > > > I want to be able to reference this dll to other projects and then use
        > > > it like...
        > > >
        > > > MyClass someclass = new MyClass();
        > > > someclass.Open( "[path to file]");[/color]
        > >
        > > The difficulty with the following is that you're trying to use a static
        > > class name as though it were a property name, and an instance property
        > > at that. The first thing you have to decide is whether MyCode is an
        > > instance-level thing (that is, there is one MyCode for each Header that
        > > you create) or whether it's a static thing (that is, there is only one
        > > MyCode in the entire program, no matter how many Headers or MyClasses
        > > you instantiate). Once you have that sorted out, I can give you more
        > > information on how to achieve what you want.[/color][/color]

        Comment

        • Bruce Wood

          #5
          Re: Help With Nesting Classes In Library


          joey.powell@top scene.com wrote:[color=blue]
          > I just want a logical way to oranize the information and to make it
          > accessible from function calls to the DLL.
          >
          > I have one main file that contains a header and several, varying length
          > records.
          >
          > I want to set it up so that I can create an instance of the class...
          >
          > MyClass someclass = new MyClass();
          >
          > and then call the open method...
          >
          > someclass.Open( );
          >
          > Once the open method is called I would like to be able to do things
          > like...
          >
          > someclass.Heade r.ReadMyCode;
          > someclass.Heade r.WriteMyCode;
          >
          > I can handle all of the code to do the reading and writing...I just
          > need to organize the classes, properties, etc... better.
          >
          > Any suggestions appreaciated.[/color]

          OK... so it looks to me as thought there is one Header _for each
          MyClass_. That is, each instance of MyClass represents a file, and each
          file has one header.

          In that case you don't want Header to be a static member... you want it
          to be an instance property of MyClass:

          public class MyClass
          {
          private Header _header;
          ...
          public class Header
          {
          private int _code;
          ...
          public int MyCode { get { return this._code; } }
          }
          ...
          public Header Header
          {
          get { return this._header; }
          }
          }

          Now you can say:

          MyClass c = new MyClass();
          int code = c.Header.MyCode ;

          The problem is that you're using "static" where you shouldn't. If I
          understand your problem correctly, there isn't just one header in you
          whole program: there is one header per file. The fact that your program
          may be reading only one file is incidental.

          Comment

          Working...