473,785 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

464 Recognized Expert Contributor
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 1284
r035198x
13,262 MVP
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 MVP
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 Recognized Expert Contributor
that code worked great thanks!
Nov 16 '07 #4
Plater
7,872 Recognized Expert Expert
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
1902
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 parent class (different domain) I receive an error about "File Not Found". I know the object is created successfully and the objects method can be called after loading the object however upon passing it back to the calling class it exploades. I...
58
10181
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 code... TCHAR myArray; DoStuff(myArray);
9
4808
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 am using vector class(STL), the compiler does not allow me to do this. I do realize there is a pitfall in this approach(size of arrays not matching etc), but I wonder how to get around this problem. I have a class hierachy with abstract base...
4
4872
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 parameter. For example, FormOptions formOptions = new FormOptions(); if (formOptions.ShowDialog(this) == DialogResult.OK) {
3
3796
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 a variant containing an array, and interop expects a parameter of type object if you are passing a variant (you are expected to cast it to the correct type in your code). I'd like to find a way of passing arrays so that you don't need to change...
2
10273
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 application. The sub loadValueByVal takes the argument by value so after returning to calling method, the object should be unchanged but it is not Public Class ITest Private MyName As String Public TestId As String Public Sub New()
7
10395
by: AMP | last post by:
Hello, I have this in form1: namespace Pass { public partial class Form1 : Form { public Form2 form2; public Form1() {
9
3803
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 techniques are not feasible) The question is; Given that I have a Person object with a private set for id. What is the recommended approac in passing that object to the web service
7
3307
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 object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
17
7255
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 to show the array data to the end user. Can I do that? How?
0
10346
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9956
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8982
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.