Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# Class Method passing reference

Posted on 2008-06-17
1
Medium Priority
?
305 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 2000 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

670 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