Solved

C# Class Method passing reference

Posted on 2008-06-17
1
294 Views
Last Modified: 2013-12-16
If I have a (C# VS2008) class (object) with fields and methods (A), and those method instanciate another object (B) which has fields and methods ... how do I reference the (A) fields/methods from within a (B) method? And what if a method in the (B) object instanciates another object (C) and wants to reference the (A) object fields/methods ... what is the proper syntax? For example:
class MyClassA
   int ItemAint;
   MyClassB CB = new MyClassB;
   public string MyMethodA1()
   { CB.DoItemB(???); }
   public string MyMethodA2()
   { // some stuff }
class MyClassB
   int itemBint;
   private string MyMethodB(???)
   { if ( ???.itemAint == 1 )
      { ???.MyMethodA2(); } }


I'm thinking this might work: (is it correct?)

class MyClassA
   int ItemAint;
   MyClassB CB = new MyClassB;
   public string MyMethodA1()
   { CB.DoItemB(this); }
   public string MyMethodA2()
   { // some stuff }
class MyClassB
   int itemBint;
   private string MyMethodB(MyClassA obj)
   { if ( obj.itemAint == 1 )
      { obj.MyMethodA2(); } }
0
Comment
Question by:mmexchange
1 Comment
 
LVL 18

Accepted Solution

by:
philipjonathan earned 500 total points
Comment Utility
At a glance, your code' syntax looks ok.
If supposing MyMethodB needs to instantiate class C and pass reference to A.
then
private string MyMethodB(MyClassA obj)

{

  if ( obj.itemAint == 1 )

      { obj.MyMethodA2(); }

  MyClassC classC = new MyClassC(obj);

}

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now