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
Solved

C# Class Method passing reference

Posted on 2008-06-17
1
300 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

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 …
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 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