473,320 Members | 2,004 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,320 software developers and data experts.

how to reference object as integer

// create an object
Control obj=new Control();
// How to write c# code to achieve the following pseudo code?
// use an integer to represent an obj
int temp=(int)obj;
// later recover original object
Control obj1=(Control)temp;

TIA

Nov 15 '05 #1
2 11339
NEVER DO SOMETHING LIKE THIS!

The CLR can move the objects for memory - optimation, and if you persit a
pointer it need additional coding (e.g fixed - statement). This shall only
be done in the case of InterOp!

If you want just to be able to get an integer to repesent your object, you
could do the following:

class ControlIntConverter
{
ArrayList list = new ArrayList();

public int RegisterControl(Control ctl)
{
return list.Add(ctl);
}

public Control GetRegistredControl(int handle)
{
return list[handle] as Control;
}
}

BUT: The integers are only valid as long the app is running! And if so, you
could also save the Reference itself, maybe re-think your design!

GP

"Rob Tillie" <Ro********@student.tul.edu> wrote in message
news:eA**************@TK2MSFTNGP09.phx.gbl...
Why the hell would you want to do that? :|
If it works, it should work how you did it.
I think you want the pointer to the object, so maybe something like:
int temp = (int) &obj;

But I think a memory location is larger than an int, so the conversion will not go properly.

Greetz,
-- Rob.

samlee wrote:
// create an object
Control obj=new Control();
// How to write c# code to achieve the following pseudo code?
// use an integer to represent an obj
int temp=(int)obj;
// later recover original object
Control obj1=(Control)temp;

TIA


Nov 15 '05 #2
"Rob Tillie" <Ro********@student.tul.edu> wrote in message
news:eA**************@TK2MSFTNGP09.phx.gbl...
Why the hell would you want to do that? :|
If it works, it should work how you did it.
I think you want the pointer to the object, so maybe something like:
int temp = (int) &obj;


I think this way you never get a pointer to an object.
You only get a pointer to an int, which actually is not
there!

Instead, you may try to use a GCHandle structure,
which you can convert to an IntPtr and further
to an int.

HTH,
Maciej
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Pablo J Royo | last post by:
Hello: i have a function that reads a file as an argument and returns a reference to an object that contains some information obtained from the file: FData &ReadFile(string FilePath); But ,...
10
by: Tony Johansson | last post by:
Hello Experts!! This class template and main works perfectly fine. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter in...
2
by: John Bowman | last post by:
Hi, I need to sign a specific node in an XML file that contains analytical data & has a structure something like the following: <DOCUMENT> <RESULT_SET> <TESTVAL> </TESTVAL> ...
0
by: Bala | last post by:
I have an ActiveX EXE. One of the methods in this component has "Recordset Object" as input parameter which has to be passed by reference. I have to call this method from C# code. From C#,...
3
by: Roger Maynard | last post by:
9
by: ziman137 | last post by:
Hi all, The results from following codes got me a bit confused. #include <stdio.h> #include <iostream> using namespace std; struct A {
1
by: Matt Sollars | last post by:
Hi! Does anyone know how to reference the object created via an object initializer when setting a child property? It's hard to write into a sentence. Suppose the following summary class for a...
3
by: Hugh Oxford | last post by:
I want to build a string to reference an object. I can reference is manually thus: print_r($this->struct->parts->parts); but if I build a string... $string = "->parts->parts"
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.