473,378 Members | 1,580 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,378 software developers and data experts.

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.Collections.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(FilePath, FileMode.Open,
FileAccess.ReadWrite);
brMyFile = new BinaryReader(fsMyFile);

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.Header.MyCode).ToString();
someclass.Close();

How can I get the someclass.Header.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.

Jun 23 '06 #1
4 1270
jo*********@topscene.com wrote:
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...
You're right: there's some confusion here. I've made some notes
in-line.
using System;
using System.Collections.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(FilePath, FileMode.Open,
FileAccess.ReadWrite);
brMyFile = new BinaryReader(fsMyFile);

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.
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]");


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.

Jun 23 '06 #2
There will be exactly one MyCode for the header. There is only one
header.

Bruce Wood wrote:
jo*********@topscene.com wrote:
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...


You're right: there's some confusion here. I've made some notes
in-line.
using System;
using System.Collections.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(FilePath, FileMode.Open,
FileAccess.ReadWrite);
brMyFile = new BinaryReader(fsMyFile);


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.
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]");


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.


Jun 23 '06 #3
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.Header.ReadMyCode;
someclass.Header.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.
jo*********@topscene.com wrote:
There will be exactly one MyCode for the header. There is only one
header.

Bruce Wood wrote:
jo*********@topscene.com wrote:
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...


You're right: there's some confusion here. I've made some notes
in-line.
using System;
using System.Collections.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(FilePath, FileMode.Open,
FileAccess.ReadWrite);
brMyFile = new BinaryReader(fsMyFile);


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.
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]");


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.


Jun 23 '06 #4

jo*********@topscene.com wrote:
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.Header.ReadMyCode;
someclass.Header.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.


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.

Jun 23 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rex_chaos | last post by:
Hi all, I am writing my own container and need an iterator. I am hesitating if I should my iterator should inherited from std::iterator or just write my own one. Please give me an idea. BTW,...
0
by: Simon North | last post by:
I am documenting C++ classes. We have created an authoring environment and the developers write the text themselves. I edit and output the XML instances. I've created my own code (DTD, schema,...
3
by: rjaw | last post by:
Hi there, using the udb-type2-driver on z/OS DB version 7, we have a problem getting the connection to the database. The small program we use looks like this: ...
2
by: Andrew S. Giles | last post by:
OK, Ive run my head into this wall for too long. I need help. I am developing an applicaiton in C# to present a user with a GUI to specify a configurable list of machines that he wants to listen...
9
by: Jamiil | last post by:
This is not a question where the source code is important, it is not important at all. So don't expect to find answers to my questions in the code; the answers will be in how the class interacts...
4
by: Tarun Mistry | last post by:
Hi all, I have posted this in both the c# and asp.net groups as it applies to both (apologies if it breaks some group rules). I am making a web app in asp.net using c#. This is the first fully OO...
4
by: kl.vanw | last post by:
I would like to count the nesting level in template classes. How can I make the following work? #include <assert.h> template <class T> class A { public: A() { // what goes here?
7
by: dlarsson | last post by:
Okay folks, I thought I was doing something very, very simple, but I cannot seem to get this to work at all. Can anyone identify what I am doing wrong here-? _________________________________ ...
4
by: christophe.gosiau | last post by:
Hi everybody, I have the following problem: For an application, I made 2 classes: Order and Ticket In the Order object I store an array with Ticket objects. so far so good, now I needed the Order...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.