Solved

C# Class Method passing reference

Posted on 2008-06-17
1
299 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
ID: 21809271
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

785 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