api question

B

Bill

I am developing an api that provides methods for reading and
extracting text in text files that have a very structured format.
There are various Read methods for reading lines, paragraphs and
specific content sections by heading name. I want to provide a
ReadSubSection method that can read a subsection and it would work
somewhat like the ReadSubTree method of the XmlReader class.How can
that be done? It should return a new instance of the Read class,
similar to what happens with ReadSubTree. I tried having the
ReadSubSection method return a new instance of its own class, but that
results in the class fields being reinitialized which messes up the
instance created by the user of the api. How does the XmlReader work
internally when ReadSubTree is called? Can a class spawn an instance
of itself without interferring with user created instances?
 
P

Peter Duniho

Bill said:
I am developing an api that provides methods for reading and
extracting text in text files that have a very structured format.
There are various Read methods for reading lines, paragraphs and
specific content sections by heading name. I want to provide a
ReadSubSection method that can read a subsection and it would work
somewhat like the ReadSubTree method of the XmlReader class.How can
that be done? It should return a new instance of the Read class,
similar to what happens with ReadSubTree. I tried having the
ReadSubSection method return a new instance of its own class, but that
results in the class fields being reinitialized

Assuming by "class fields" you mean "instance fields of the class", that
makes sense. A new instance of a class has all new instance fields,
which are initialized from scratch (not "reinitialized", since for that
to happen they would have had to have been initialized in the first
place, which for a newly created instance can't happen).
which messes up the instance created by the user of the api.

That, however, does not make sense. Existing instances should not be
affected at all by the creation of new instances. Certainly C# and .NET
don't provide any automatic way for that to happen, so if it is in fact
happening, you've done something specifically to cause it to happen.
How does the XmlReader work
internally when ReadSubTree is called? Can a class spawn an instance
of itself without interferring with user created instances?

Yes, of course.

If you can post a concise-but-complete code example showing what
behavior you're having trouble with, we can probably explain what you've
done wrong and how to fix it.

Pete
 
B

Bill

Looks like I have it working. I was returning an instance of the base
class. I also added a private constructor which I use for the class to
spawn an instance of itself. It seems to work, but is that the best
thing to do?

I have a Reader class with a Read and ReadSubSection methods and the
class has private fields that track what and how much has been read by
the Read method.

public Reader(filename)
{
//opens a stream on filename
}
//private constructor
private Reader(section)
{
this.SubSection==section;
}

Reader reader1=new Reader(filename);
//user code here for a loop for the Read method to read to a certain
point in the file
Reader reader2=reader1.ReadSubSection();

public ReadSubSection()
{
return new Reader(this.section);
}
 
G

Gregory A. Beamer

How does the XmlReader work
internally when ReadSubTree is called?

With XML, unlike a structure set of text lines, you have an end pointer (as
tags are paired). Pulling the SubTree is reading until the closing member
of the pair and pulling the tags within.
Can a class spawn an instance
of itself without interferring with user created instances?

Yes. it is called recursion. ;-)

The only caution I would give on recusion is if you do not watch it, you
can get caught in an inescapable loop. As the spawning appears to be
dictated by the formatted file, this should not be an issue, however, as it
should not spawn an object when there is nothing in the file telling it to
spawn one.

My question is why you are using a custom formatted file instead of XML. Is
it some other industry standard? If so, see if someone has already created
a parser that parses the information into bits.

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
P

Peter Duniho

Gregory said:
[...]
Can a class spawn an instance
of itself without interferring with user created instances?

Yes. it is called recursion. ;-)

Please don't do that.

An example:

class MyClass
{
public static MyClass Create()
{
return new MyClass();
}

public MyClass Create()
{
return new MyClass();
}
}

There is no recursion there, and yet it meets exactly the criteria
stipulated by the OP. It is wrong to call something "recursion" when
there is no actual "recursion" involved. Doing so will only confuse
matters.

Pete
 
G

Gregory A. Beamer

[email protected]:

There is no recursion there

The problem, technically (if truly like XML, as the OP has stated) is
recursive in nature:

Tree
--- Read Subtree 1
--- Read Subtree 2
--- Read Subtree n

I did not state you have to create a recursive loop to solve the problem,
however.

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
P

Peter Duniho

Gregory said:
[email protected]:

There is no recursion there

The problem, technically (if truly like XML, as the OP has stated) is
recursive in nature: [...]

And that would have been relevant had the question been "how do I read
the XML structure"?

But it wasn't. The question asked, the one you quoted and purported to
answer, had nothing at all to do with recursion. Saying it does only
confuses things.

Pete
 
G

Gregory A. Beamer

But it wasn't. The question asked, the one you quoted and purported to
answer, had nothing at all to do with recursion. Saying it does only
confuses things.

I took "read structure like XML" as structure being hierarchical. You did
not. Does not make me wrong.

Besides, I had a smiley, so lighten up. LOL ;-)

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
P

Peter Duniho

Gregory said:
I took "read structure like XML" as structure being hierarchical.

You can take "read structure like XML" however you like. Since the OP
didn't write that, nor was it the question you quoted in your answer,
it's completely irrelevant.
You did not. Does not make me wrong.

Answering a question other than the one that was asked, and other than
the one you quoted, does not make you wrong? I suppose that depends on
your definition of "wrong". But, "recursion" is definitely the wrong
answer to the question you quoted.
Besides, I had a smiley, so lighten up. LOL ;-)

Are you now saying your entire reply was intended as a joke? If not,
the smiley is irrelevant. And if so, then why in the world would you
feel a need to defend the content of your reply at all?

Methinks thou dost protest too much.

So, is it really your position that in order for "a class to spawn an
instance of itself without interfering with [other] user created
instances", recursion is required, or even in any way solves that
particular problem?

I'd love to see a code example. Post a method in a class that creates a
new instance of that class, where that new instance does not interfere
with other created instances, where recursion is used, and where
recursion cannot be eliminated from the algorithm (*). If you can do
so, I will happily admit I was wrong to correct you.

Pete

(*) (It should go without saying, but I'll say it anyway: as you know,
one can modify any recursive method so that the method itself is not
recursive; but the interesting question here is whether the _algorithm_
itself can be made to not be recursive...you have no need to worry that
I will replace your recursive call with a stack data structure and call
that good).
 
G

Gregory A. Beamer

Answering a question other than the one that was asked, and other than
the one you quoted, does not make you wrong?

I am not sure it is doing either of us any good to continue this thread, so
I am dropping off.

I certainly don't take this as seriously as you seem to and think that a
continued forum fight is a waste of time. Your mileage may vary.

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top