Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi people,

So here's the thing, I have two classes each with their own functions Say Class One and Class Two.

Now Class Two Say has Functions that I don't want people to edit, but is usable from Class One and uses some functions from Class One.

Class One is user written class but also uses Class Two functions.

Now at the top of each class I declare a new instance of each other ( those who picked up on this already will know its Cyclic and It'll throw some errors - Stack Errors ). I know I've done something wrong and I know the cause is the FACT that I create new instances of each other and that In turn Keeps creating a Never-ending Loop until the Compiler has had enough.

My question is how can I have class One use functions from class two and vice versa without creating that loop.

My simple solution and last resort is to copy over all functions from Class One to Class two.... but losing on people not editing Class Two(which I want to remain constant) .....

Thanks
Posted
Updated 10-Oct-11 23:52pm
v2
Comments
Xeshan Ahmed 11-Oct-11 5:58am    
question is not clear ...
codenameyash 11-Oct-11 6:02am    
Ok, Class One and Class Two, then I create Instances of each other in each respective class.... however when I run the program, it throws a Stack Problem..

When I comment out creating the instance of Class Two for example and Comment out calls To All Class Two Methods, the Project Works... NO Stack Problems, as I have now Removed the Dependency on Class Two From Class One..
BillWoodruff 11-Oct-11 19:24pm    
May I strongly suggest you study the wise advice in the solutions of Mehdi and SAK here; while you don't give us enough information to understand the mutual dependencies of your two classes, such cross-referencing is often a sign of poor fundamental design, or "feature creep." A simple re-factoring here may help you create much better quality code.

Your best option is to create a third class as a helper and have Class1 and Class2 use the Helper.
 
Share this answer
 
Comments
OriginalGriff 11-Oct-11 6:09am    
Yep - or even two helpers to keep the methods separate. +5
Mehdi Gholam 11-Oct-11 6:13am    
Cheers
Sergey Alexandrovich Kryukov 11-Oct-11 19:04pm    
Good and simple idea, my 5. Actually, a problem of OP is deeper; I write about it in my solution; please see.
--SA
BillWoodruff 11-Oct-11 19:21pm    
+5 excellent advice, Mehdi. even though we have such sketchy information about why these two classes are "in each other's shoes," the design seem questionable.
Mehdi Gholam 12-Oct-11 1:22am    
In my experience as a consultant it's very hard to tell people they are wrong as they go defensive and shutdown on you which is non-productive for both parties, so you have to gently nudge them in the right direction. Moreover the issues are fundamental design flaws, and admittedly they are very hard to fix quickly and painlessly.

There are two ways of doing this the easiest is the one I mentioned the harder way is through inheritance and base classes which is very hard for people to wrap their heads around.
imho the key question to ask here is: where/how are the instances of Class1 and Class2 created ?

If they are both created by a third class, there's an easy solution. If Class1 creates Class2, another solution.

Assuming Class2 is either created by Class1, or created by a third class that also creates Class1:

Put a public property in Class2 of type 'Class1' and when the instance of Class2 is created, insert the reference to the instance of Class1 into it:
// in Class1
public Class1 ReferenceToClass2 { get; set; }

// in Class2
public Class1 ReferenceToClass1 { get; set; }

// in the code where an instance of Class2 is created
// assume an instance of Class1, named MyClass1, has been created

Class2 MyClass2 = new Class2();

MyClass2.ReferenceToClass1 = MyClass1;

MyClass1.ReferenceToClass2 = MyClass2;
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Oct-11 19:05pm    
Very good explanation of the essence of the problem, my 5. Actually, a problem of OP is deeper; I write about it in my solution; please see.
--SA
Mehdi Gholam 12-Oct-11 1:16am    
My 5!
This is my addition to other answers: your problem is simple: you want to do something which you should not do. This is not a technical problem: stop wanting what you want and concentrate on ultimate goal, and the problem will disappear by itself. You tend to think in a very dangerous way: you want to preserve your original design and have problem throwing it away and put it right to solve the real problem, not the one which exists only in your imagination. Be more brave to do so.

—SA
 
Share this answer
 
Comments
BillWoodruff 11-Oct-11 19:20pm    
+5 Very good point, and wise advice, SAK; I was just thinking about adding something to my solution to, gently, suggest the poster re-consider their design, and refactor, recode, perhaps take Mehdi's advice and put common methods into a third class.
Sergey Alexandrovich Kryukov 11-Oct-11 20:20pm    
Thank you, Bill. Consider you already gave this advice, which is the way to overcome the problem. You know, sometimes it can be very good not to jump to a solution even though there is one and live with a problem for a while; it can be fruitful.
--SA
Mehdi Gholam 12-Oct-11 1:20am    
In my experience as a consultant it's very hard to tell people they are wrong as they go defensive and shutdown on you which is non-productive for both parties, so you have to gently nudge them in the right direction. Moreover the issues are fundamental design flaws, and admittedly they are very hard to fix quickly and painlessly.

My 5 anyway.
Sergey Alexandrovich Kryukov 12-Oct-11 1:43am    
Thank you, Mehdi.

Oh yes, what you say is absolutely true. I don't want to do it "gently". I'll explain why. In this particular case, I don't care much if OP accepts or denies my advice, by apparent reasons. At the same time, I prefer my voice to be listened. Collectively, such posts create useful precedent.

In our modern word, there is a huge unjust shift: people who "go defensive" and ultimately create low or negative value (yes, in software development it is often purely negative) literally consumes lives of hard-working people doing their works properly. This shift also destroys projects and companies and creates loss of jobs. The shift is asymmetric: projects are failed due to resistance to improvements much more often than, say, due to excessive perfectionism (causing unacceptable delays). This is controversial point, but I'm sure in my opinion.

This does not apply to OP, because she or he is apparently a novice, but I would rather invite people to move in right direction from the very beginning. This is exactly what you are talking about: "going defensive" is a very dangerous behavior which should be realized and avoided from the very beginning. If someone criticizes me, I either thankfully accept it and consider it as helping me to improve myself (it happened many times here at CodeProject), or I proof that my approach is better. Protecting oneself just because improvement is difficult or too much work is a very dangerous practice.

--SA
Mehdi Gholam 12-Oct-11 1:50am    
Hear! Hear!
hmmmmm... quite valid points... I never reject help, cos usually its quite hard to come by or criticisms cos those are hard to come by as well...Oh also, you should have seen my code a few years back used to do everything in FORM1.CS( **hides in shame**). I think this year I've seen the wonders of Classes and things like that...
Primary job btw is an Embedded design engineer, hence Electronics and not software so my skills are sketchy, ok lets be honest more than sketchy, :), give me a Microcontroller and I'll happily go away into my Engineering cave....

Anyway, love the ideas, I shall try them out, when I google some meanings of them and understand more...

Cheers.... TY ppl.... :)
 
Share this answer
 
use recursion for interation if you dont want to use loop
 
Share this answer
 
Comments
OriginalGriff 11-Oct-11 6:07am    
Reason for my vote of one: answer has nothing to do with the question.
The question is to do with avoiding a looping in declaration of a class:
public class A { B b = new B(); }
public class B { A a = new A(); }
When you try to instantiate either class, you will get a run time error that the stack has overflowed.
Recursion cannot help here, as it is the declarations that are recursive!
Sergey Alexandrovich Kryukov 11-Oct-11 18:59pm    
Agree.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900