Simple Inheritance - An unhandled exception of type 'System.StackOverflowException'...

D

daz_oldham

Hi everyone

As my expedition into OOP takes another shaky step, I now am getting
the error:

An unhandled exception of type 'System.StackOverflowException' occurred
in xxx.dll.

In my Visit.cls I have got:

public class Visit
{
public FABInfo Info = new FABInfo(); // Line that the error
flags up on.
}


Then in my FABInfo.cls I have got:

public sealed class FABInfo : Visit
{
public string myFunc()
{
return "this.m_sFABSessionID.ToString();";
}
}

When I create an Instance of Visit, I get a loop of
oVisit.Info.Info.Info.Info..... so that is where the problem lies.

Any ideas anyone?

Many thanks

Darren
 
M

Marc Gravell

You create a new Visit, and in the ctor it creates a FABInfo...

*however*, FABInfo *is* a Visit, so this also runs the Visit ctor, and hence
the loop.

What is this trying to represent? Rather than trying to fix the dodgy code,
if you explain what you think you are trying to do, then people can tell you
the right way to do it... which might not resemble your code much ;-p

Marc
 
D

daz_oldham

Hey Mark - thanks for the quick reply.

Visit is going to be my base class, and it will maily have properties
in it such as the location of web services, usernames, passwords etc
etc. And from that, there will be a number of child classes such as
Info, Package, Detail.

What I want to do is from Info:Visit, be able to access this.Password
for example and the same from my other child classes.

Does that make sense?

Thanks

Daz
 
M

Marc Gravell

OK, if I understand your scenario, then the FABInfo creation in the Visit
ctor is erroneous - just ditch it. If you want a FABInfo, then simply use
"new FABInfo()"... since FABInfo derives from Visit this will create the new
FABInfo object by first running the visit ctor, then the FABInfo ctor.

If the Visit genuinely needs a FABInfo, then the object model is screwy;
it's chicken and egg: you can't create a Visit without first successfully
creating a FABInfo, yet a FABInfo /is/ a Visit - so neither can be created.

Marc
 
D

daz_oldham

Marc

I sort of understand what you're saying, but I don't think I have
gotten over what I am trying to do.

1) Visit is intended to hold a set variables that are defined when
oVisit is created.

2) FabInfo, FabBasket, FabCoupon etc etc are going to represent
different types of functionality on the site, and ultimately all call
different web services from a given provider.

So, what I wanted to do is configure a Visit object with the usernames,
passwords, urls etc, and then perform my different requests such as:

1) oVisit.Configure(parameters[]); // Initialise my object

2) oVisit.FABInfo.GetInformation(parameters[]);

- or -

3) oVisit.FabBasket.AddItem(parameters[]);

I thought this would save me having to do oFabBasket.Configure() and
oFabInfo.Configure() if I wanted to use both of the classes on the same
page. I could do oVisit.Confgure() and then call
oVisit.FabBasket.Something();

Does that make sense, or do you think I need to review the way I was
planning to set this out?

Thanks

Darren
 
D

daz_oldham

I'm going to reply to myself at this point Marc - tell me if you
disagree.

I would be better off with a couple of public (base?) classes:

FabInfo
FabBasket
FabWhatever

And each of thise will inherit

FabConfigure, which can then be configured by oFabInfo.Configure()

I think this would be the right way of doing it. What do you think?

Thanks

Darren
 
M

Marc Gravell

Quite possibly; you might also look at using an interface rather than a
base-class, but that really comes down to the design
 
G

Guest

Another basic point is that the base class shouldn't know about the derived
classes.

It looks like you need three classes (or two and an interface).

1) The base class (perhaps abstract) or interface of...
2) The derived classes
3) The class that holds instances of those derived classes
 
D

daz_oldham

Thanks Marc / Piebald

I am fairly sure that I have got it the right way now, and I do need
intervaces for some genric calls that i have got to make.

However, I think I've got it working now with classes, so I am fairly
happy :)

I've had a bit of a crash course in OOP and C# this last 48 hours, and
I'd like to thank everyone for their help!!!

I am on my way now, and the next thing is having to use WebRequest to
consume non-soap web services, so I should imagine i'll be posting
again soon ;)

Many many thanks

Daz
 
M

Marc Gravell

Search for something I posted a few weeks ago; I posted full code for a
client and server consuming a non-SOAP, xml-based web-service.

Marc
 
M

Marc Gravell

Looking in my outbox: 28th April, titled: Need to write an HTTP server , can
i use a webservice with "NOSOAP"?

Marc
 
D

daz_oldham

Hi Mark - found it :)

Thanks - there is a lot of usefull snippets in there that I can have a
look at - you're a star, thankyou so much for your help.

Darren
 

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