473,396 Members | 1,738 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,396 software developers and data experts.

C# Object Reference?

I know this is possible in C++ with pointers but dont know how to do it in C#.

Simplified Sample Code and Comments

//create object A
object A = new object();

//create object B
object B = A;

//create object C
object C = new object();

//I need to change Object A to point to the contents of Object C only using Object B
May 24 '07 #1
10 33716
Plater
7,872 Expert 4TB
Why do you need two things pointing to the same object? I've not been able to come up with a use for it.
May 24 '07 #2
mwalts
38
I know this is possible in C++ with pointers but dont know how to do it in C#.

Simplified Sample Code and Comments

//create object A
object A = new object();

//create object B
object B = A;

//create object C
object C = new object();

//I need to change Object A to point to the contents of Object C only using Object B
Hmm, don't know how to do this directly, however if you create a function and pass it by reference it will exhibit that behaviour.

i.e

//in main or whatever
//create object A
object A = new object();
someFunction(A);
.
.
.
private void someFunction(ref object B)
{
object C = new object();
B = C;
}


Now A has been changed to C through B

Not sure if that helps you, I have to admit I haven't come up with a good reason to do it directly in C# yet, though I seem to remember using it more then once in my old C days

-mwalts
May 24 '07 #3
Plater
7,872 Expert 4TB
Hmm, don't know how to do this directly, however if you create a function and pass it by reference it will exhibit that behaviour.

i.e

//in main or whatever
//create object A
object A = new object();
someFunction(A);
.
.
.
private void someFunction(ref object B)
{
object C = new object();
B = C;
}


Now A has been changed to C through B

Not sure if that helps you, I have to admit I haven't come up with a good reason to do it directly in C# yet, though I seem to remember using it more then once in my old C days

-mwalts
Well, now A has been set to the contents of C, but changing A would not also change C. They don't occupy the same memory, they are still just copies of one another.

If you want to do memory manipulation you have to make the jump into unmanaged code (unsafe code, check msdn on it) and that gets real nasty real fast.
May 24 '07 #4
The real situation is I have a field from a datarow that I put into a collection.
Then latter I have the collection but not the datarow(but the datarow still exists). I want to set the datarow field to a new value but feel it will only change the collection to the new data and not the orginal datarow field.
May 24 '07 #5
It would be easy if only C# supported pointers to Reference Types
May 24 '07 #6
mwalts
38
It would be easy if only C# supported pointers to Reference Types
Yeah, but direct memory manipulation would kinda defeat the whole managed sandbox concept period.

For normal reference types you would be able to change their values from the fields located in your collection... for boxed value types and immutable reference types (which are of course the most common in DataRow's) I'm not sure what the behavior would be. Probably won't work since you would have to unbox it to change the value... But it might be worth a try to just change the collections value and see what you get

You might have to put the DataRow into the collection instead of the individual field (or some such work around)

Good luck,

-mwalts
May 24 '07 #7
The real situation is I have a field from a datarow that I put into a collection.
Then latter I have the collection but not the datarow(but the datarow still exists). I want to set the datarow field to a new value but feel it will only change the collection to the new data and not the orginal datarow field.
Why don't you want to store a reference to the object itself?

Also, you could implement an event-type system to update the datarow when the value in your collection changes.
May 25 '07 #8
Yeah, I had to put the datarow into the collection

I suppose for more generic answer
if you encapsulate a object into another object you can pass the outer object and then change the inner object without loosing the reference?

or something like that
Jun 5 '07 #9
class WrapperObject
{ public TrackedObject obj }

//now you can do this.
WrapperObject A = new WrapperObject();
A.obj = new TrackedObject();
WrapperObject B = new WrapperObject();

TrackedObject C = new TrackedObject();
B.obj = C;

//now A.obj = C
Jun 23 '07 #10
Plater
7,872 Expert 4TB
Anything created with the "new" keyword will be passed by "reference" automatically.
So if your function takes in as an argument an object,
any actions done on that object withen the function are actually done to the object that was passed in.

I used to do this when I needed a cheap way to get one form to talk to another, I would pass a textbox into the contrustor. (Note: don't actually pass textboxes and stuff like that)
Jun 26 '07 #11

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

Similar topics

2
by: Pkpatel | last post by:
Hi, I keep getting this error every time I try to load crystalreportviewer on a webform with a dataset. Here is the error: -------------------------------------------------------- Server...
4
by: Frawls | last post by:
Hi, I get the following error when trying to run a search on my aspx site, this error only occours if the product im searching for does not exist. Can anybody explain this please and help me...
1
by: Martine | last post by:
Hi there! I have a problem with programmatically adding user controls to my mobile webforms. If I load my usercontrol programmatically (in the Page_Load), the object is instantiated, I have...
2
by: jw56578 | last post by:
why am i getting the "Object reference not set to an instance of an object" error on all page registeration tags in my project. <%@ Page Language="vb" AutoEventWireup="false"...
3
by: nemo | last post by:
Hi, My application works fine on the localhost but spits this error as soon as I put it on the server. I know this error occurs when an object has not been instantiated prior to a reference, but...
3
by: Adam | last post by:
We have a web site that uses .vb for the web pages and .cs for a class module. We are getting the error in .NET 2.0 and VS 2005 beta 2. It does work with .NET 1.1. When trying to access a page...
5
by: eBob.com | last post by:
I am trying to change some Structures to Classes. The Classes now look like this ... Public Class OneAttr Public AttrName As String Public Column As String Public Caption As String End Class...
7
by: Brett | last post by:
I'm not sure why I keep getting this error, "Object reference not set to an instance of an object". Private Function somefunction() as string Dim MyCurrentClass As New Class1 Try For i As...
6
by: kalaivanan | last post by:
hi, i am a beginner in c#. i have theoretical knowledge about object, reference and instance. but i want to know clearly about what is an object, reference and instance. can any one help me? or...
4
by: livmacca | last post by:
Hi, I am new to VB .Net programming and is trying to create a webpage. I encountered the following error and is totally clueless on how to make it work: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
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,...

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.