Solved

C# Class Method passing reference

Posted on 2008-06-17
1
303 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

626 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