Beginner Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOtherBeginner Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 17th, 2009, 07:52 AM
VentureFree VentureFree is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 2 VentureFree User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 33 m 59 sec
Reputation Power: 0
Objects owner (C#)

I've got two classes, one of which is a member of the other.
Code:
public class MyClass
{
    // Fields, Constructors, Methods, etc...
}

public class OwnerClass
{
    // Fields, Constructors, Methods, etc...
    public MyClass MemberClass; 
}
The MyClass object (that is MemberClass in the OwnerClass object) needs to get some data from the specific OwnerClass object that owns it. Is it possible to simply have a pointer to the owner, and is it safe to do so? If not (to either question), what is the best way to be able to access owner data?

Reply With Quote
  #2  
Old June 17th, 2009, 01:58 PM
SteffenL SteffenL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 76 SteffenL User rank is First Lieutenant (10000 - 20000 Reputation Level)SteffenL User rank is First Lieutenant (10000 - 20000 Reputation Level)SteffenL User rank is First Lieutenant (10000 - 20000 Reputation Level)SteffenL User rank is First Lieutenant (10000 - 20000 Reputation Level)SteffenL User rank is First Lieutenant (10000 - 20000 Reputation Level)SteffenL User rank is First Lieutenant (10000 - 20000 Reputation Level)SteffenL User rank is First Lieutenant (10000 - 20000 Reputation Level)SteffenL User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 12 h 35 m 35 sec
Reputation Power: 110
This is one way, at least:
C# Code:
Original - C# Code
  1. public class MyClass
  2. {
  3.     // Fields, Constructors, Methods, etc...
  4.     private object _Owner;
  5.  
  6.     public MyClass(object Owner)
  7.     {
  8.         this._Owner = Owner;
  9.     }
  10. }
  11.  
  12. public class OwnerClass
  13. {
  14.     // Fields, Constructors, Methods, etc...
  15.     public MyClass MemberClass;
  16.  
  17.     public OwnerClass()
  18.     {
  19.         this.MemberClass = new MemberClass(this);
  20.     }
  21. }

I am not sure if there are better ways.

Reply With Quote
  #3  
Old June 18th, 2009, 05:05 AM
VentureFree VentureFree is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 2 VentureFree User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 33 m 59 sec
Reputation Power: 0
I had been under the mistaken impression that declaring an object in a class in such a way made a separate copy of that object. After a little more research it's clear that an object that I make in this way is really only a reference to the object that it's set to. In other words, if I change the original OwnerClass object independently of the MyClass object, it's automatically reflected in the MyClass object because it's only a reference. This makes things a whole lot easier than I thought they would be.

If I'm wrong about that please let me know. I'll take a lack of response as acknowledgment that my understanding is correct.

Reply With Quote
  #4  
Old June 18th, 2009, 05:20 AM
SteffenL SteffenL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 76 SteffenL User rank is First Lieutenant (10000 - 20000 Reputation Level)SteffenL User rank is First Lieutenant (10000 - 20000 Reputation Level)SteffenL User rank is First Lieutenant (10000 - 20000 Reputation Level)SteffenL User rank is First Lieutenant (10000 - 20000 Reputation Level)SteffenL User rank is First Lieutenant (10000 - 20000 Reputation Level)SteffenL User rank is First Lieutenant (10000 - 20000 Reputation Level)SteffenL User rank is First Lieutenant (10000 - 20000 Reputation Level)SteffenL User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 12 h 35 m 35 sec
Reputation Power: 110
> I'll take a lack of response as acknowledgment that my understanding is correct
Or maybe someone could be too lazy or busy to reply back.

A class is a reference type, so you would only pass a reference to it; not make a copy of it.
Anyone, please correct me if I am wrong.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOtherBeginner Programming > Objects owner (C#)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
Stay green...Green IT