473,230 Members | 4,187 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,230 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 33709
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: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...

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.