473,395 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Passing Class object as ref to other class (C#)

464 Expert 256MB
Okay i create a class object in my "main" class. I want to pass this object by reference to another class. I am able to pass the ref of the object to the constructor, but i don't know how i am able to caputre it and use it.

Expand|Select|Wrap|Line Numbers
  1. class ClassA()
  2. {
  3.      A(ref ClassB ObjectOfClassB)
  4.      {
  5.             //save object of class be to use in another function
  6.      }
  7.  
  8.      public void UseObject ()
  9.      {
  10.            ObjectOfClassB.Variable = 5;
  11.      }
  12. }
  13.  
  14. class ClassB()
  15. {
  16.       public int Variable;
  17. }
  18.  
  19. MainClass //
  20. {
  21.      ClassB MyClassB = new ClassB();
  22.      ClassA MyClassA = new ClassA(ref MyClassB); //so object value can get changed
  23. }
  24.  
i tried to create a ref object in the class, but it says i can't use that typename in a namespace, struct, class etc.
Nov 14 '07 #1
4 1253
r035198x
13,262 8TB
Have a look at this example and try it again

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. class Foo {
  3.     private Bar bar;
  4.     public Foo(Bar bar) {
  5.         this.bar = bar;
  6.     }
  7.     public void UseBar() {
  8.         bar.DoWork();
  9.     }    
  10.     public static void Main(String[] args) {
  11.          Foo foo = new Foo(new Bar());
  12.          foo.UseBar();
  13.     }
  14. }
  15. class Bar {
  16.     public void DoWork() {
  17.         Console.WriteLine("Bar has worked");
  18.     }
  19. }
Nov 14 '07 #2
r035198x
13,262 8TB
Have a look at this example and try it again

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. class Foo {
  3.     private Bar bar;
  4.     public Foo(Bar bar) {
  5.         this.bar = bar;
  6.     }
  7.     public void UseBar() {
  8.         bar.DoWork();
  9.     }    
  10.     public static void Main(String[] args) {
  11.          Foo foo = new Foo(new Bar());
  12.          foo.UseBar();
  13.     }
  14. }
  15. class Bar {
  16.     public void DoWork() {
  17.         Console.WriteLine("Bar has worked");
  18.     }
  19. }
Forget that. I not had my Rooibos tea yet. This should be what you want.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. class Foo {
  3.     private Bar bar;
  4.     public Foo(ref Bar bar) {
  5.         this.bar = bar;
  6.     }
  7.     public void ChangeBar() {
  8.         bar.setVal("Foo's value");
  9.     }    
  10.     public static void Main(String[] args) {
  11.         Bar bar = new Bar("Mine");
  12.          Foo foo = new Foo(ref bar);
  13.          foo.ChangeBar();
  14.          Console.WriteLine(bar.getVal());
  15.     }
  16. }
  17. class Bar {
  18.     private string MyVal;
  19.     public Bar(String MyVal) {
  20.         this.MyVal = MyVal;
  21.     }
  22.     public String getVal() {
  23.         return MyVal;
  24.     }
  25.     public void setVal(String val) {
  26.         MyVal = val;
  27. }    }
Nov 14 '07 #3
Studlyami
464 Expert 256MB
that code worked great thanks!
Nov 16 '07 #4
Plater
7,872 Expert 4TB
The same way you would do it with an int or a string?
Declare a variable of if it:
myClass mc=null;
then in your constructor set "mc" to the referenced object you passed in.
For classes, I don't think you need the "ref" keyword (I've never had to use it?)
Nov 16 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Bryan Martin | last post by:
I have a object that is created in a seperate domain which needs to be passed back to the parent class. Because this object is created in a seperate domain if I try to pass the object back to the...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
4
by: Ron Rohrssen | last post by:
I want to show a dialog and when the form (dialog) is closed, return to the calling form. The calling form should then be able to pass the child form to another object with the form as a...
3
by: Mark | last post by:
Hi From what I understand, you can pass arrays from classic ASP to .NET using interop, but you have to change the type of the.NET parameter to object. This seems to be because classic ASP passes...
2
by: Witold Iwaniec via .NET 247 | last post by:
It seems that when you pass an object to a function it is always passed by reference even if it is explicitly declared ByVal. Is it the behavior of VB.Net? Here is sample code from sample Asp.Net...
7
by: AMP | last post by:
Hello, I have this in form1: namespace Pass { public partial class Form1 : Form { public Form2 form2; public Form1() {
9
by: Greger | last post by:
Hi, I am building an architecture that passes my custom objects to and from webservices. (Our internal architecture requires me to use webservices to any suggestion to use other remoting...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.