473,490 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C# Object to X Type Question


Hi

I am writing a program using the SortedList class . Please contain laughter
if i am doing something stupid.,

I am using a struct as the value part to store a range of information
against a unique key (see below)

I was hoping that when i found the entry again, i could get a reference back
to my struct and update the data.

Things don't seem to be working as i expected. If this was C or C++ i would
know how to handle the pointers and casting.
Is my approach valid ? Is casting from struct to object and vice versa OK
?

FileRecord Entry;
object nObj;
........
.....other code here...

try
{
if (!FileInfoEntries.ContainsKey(Filename))
{
// First use of key part

Entry = new FileRecord();
Entry.nOpenCount = 0;
Entry.nReadCount = 0;
Entry.nWriteCount = nFileLine;
Entry.bMonitorMe = true;
Entry.nAverageReadLength = 0;
FileInfoEntries.Add(Filename, (object)Entry);
}
else
{
// repeat of key part

nIndex = FileInfoEntries.IndexOfKey(Filename);
nObj = FileInfoEntries.GetByIndex(nIndex);
Entry = (FileRecord) nObj;
}
}

.... below here Entry.xxx is updated
Thanks in advance (struct is below)

===============================================

public struct FileRecord
{
public Boolean bMonitorMe;
public int nOpenCount;
public int nReadCount;
public int nWriteCount;
public int nAverageReadLength;
public int nLastOpenTime;
public int nLastOpenMode;
public int nLastIOOperationTime;
public Boolean bStartupFile;
public Boolean bRuntimeFile;
public Boolean bShutdownFile;
};

Dec 6 '05 #1
3 1449
If my understanding is correct, the problem here is that a struct (in
CLR/C#) is value-typed, so when you do GetByIndex you essentially get a
bitwise clone of the origianl object, and *not* a handle to the (shared)
object. You can happily "cast" (in reality, box/unbox) between struct to
object and back, but you aren't getting your original object. Consider the
following:

public class Test {
public struct MyTest {
public int MyField; // yes sloppy, but quick for demo
}
public static void Main() {
MyTest t1 = new MyTest();
t1.MyField = 1;
MyTest t2 = t1;
t2.MyField = 2;
Console.WriteLine(t1.MyField);
Console.WriteLine(t2.MyField);
Console.ReadLine();
}
}

This prints 1 and 2, not 2 and 2, because t1 and t2 are completly different
entities.

To do what you want, you *either* need to use a class, not a struct, else
you will need to remove the struct from the array and re-add the updated
value; I would favour the class approach. In C++, struct and class are much
closer together than in C# - for you scenario I believe this should be a
class.

Hope this helps,

Marc

"Greg Roberts" <no****@nospam.com> wrote in message
news:eF****************@TK2MSFTNGP10.phx.gbl...

Hi

I am writing a program using the SortedList class . Please contain
laughter if i am doing something stupid.,

I am using a struct as the value part to store a range of information
against a unique key (see below)

I was hoping that when i found the entry again, i could get a reference
back to my struct and update the data.

Things don't seem to be working as i expected. If this was C or C++ i
would know how to handle the pointers and casting.
Is my approach valid ? Is casting from struct to object and vice versa
OK ?

FileRecord Entry;
object nObj;
.......
....other code here...

try
{
if (!FileInfoEntries.ContainsKey(Filename))
{
// First use of key part

Entry = new FileRecord();
Entry.nOpenCount = 0;
Entry.nReadCount = 0;
Entry.nWriteCount = nFileLine;
Entry.bMonitorMe = true;
Entry.nAverageReadLength = 0;
FileInfoEntries.Add(Filename, (object)Entry);
}
else
{
// repeat of key part

nIndex = FileInfoEntries.IndexOfKey(Filename);
nObj = FileInfoEntries.GetByIndex(nIndex);
Entry = (FileRecord) nObj;
}
}

... below here Entry.xxx is updated
Thanks in advance (struct is below)

===============================================

public struct FileRecord
{
public Boolean bMonitorMe;
public int nOpenCount;
public int nReadCount;
public int nWriteCount;
public int nAverageReadLength;
public int nLastOpenTime;
public int nLastOpenMode;
public int nLastIOOperationTime;
public Boolean bStartupFile;
public Boolean bRuntimeFile;
public Boolean bShutdownFile;
};


Dec 6 '05 #2
Hi,

Two things :
1- Use a hashtable
2- use a class instead of a struct , see marc's post for a good explanation

from msdn:
It is recommended that you use a struct for types that meet any of the
following criteria:

a.. Act like primitive types.
b.. Have an instance size under 16 bytes.
c.. Are immutable.
d.. Value semantics are desirable.
Yours fails I think all of them

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Greg Roberts" <no****@nospam.com> wrote in message
news:eF****************@TK2MSFTNGP10.phx.gbl...

Hi

I am writing a program using the SortedList class . Please contain
laughter if i am doing something stupid.,

I am using a struct as the value part to store a range of information
against a unique key (see below)

I was hoping that when i found the entry again, i could get a reference
back to my struct and update the data.

Things don't seem to be working as i expected. If this was C or C++ i
would know how to handle the pointers and casting.
Is my approach valid ? Is casting from struct to object and vice versa
OK ?

FileRecord Entry;
object nObj;
.......
....other code here...

try
{
if (!FileInfoEntries.ContainsKey(Filename))
{
// First use of key part

Entry = new FileRecord();
Entry.nOpenCount = 0;
Entry.nReadCount = 0;
Entry.nWriteCount = nFileLine;
Entry.bMonitorMe = true;
Entry.nAverageReadLength = 0;
FileInfoEntries.Add(Filename, (object)Entry);
}
else
{
// repeat of key part

nIndex = FileInfoEntries.IndexOfKey(Filename);
nObj = FileInfoEntries.GetByIndex(nIndex);
Entry = (FileRecord) nObj;
}
}

... below here Entry.xxx is updated
Thanks in advance (struct is below)

===============================================

public struct FileRecord
{
public Boolean bMonitorMe;
public int nOpenCount;
public int nReadCount;
public int nWriteCount;
public int nAverageReadLength;
public int nLastOpenTime;
public int nLastOpenMode;
public int nLastIOOperationTime;
public Boolean bStartupFile;
public Boolean bRuntimeFile;
public Boolean bShutdownFile;
};


Dec 6 '05 #3
thanks guys for the good oil here and the quick reply
Dec 6 '05 #4

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

Similar topics

1
2566
by: sunaina | last post by:
This is the first program I am writing using PHP and Mysql. I am creating a game where user thinks of an object and my program guesses the object while asking series of yes/no questions. All a...
4
4750
by: Avi Kak | last post by:
Hello: Please forgive me if my question is too silly or just not well-formed. Wesley Chun in his book (Core Python Programming) says that **everything** in Python is an object. So I became...
4
3073
by: Tom | last post by:
I want to open a recordset object on an .asp page. When I open the recordset I would like to use a stored procedure that expects a parameter to be passed for the stored procedure. I will then use...
100
5153
by: E. Robert Tisdale | last post by:
What is an object? Where did this term come from? Does it have any relation to the objects in "object oriented programming"?
6
1532
by: Tom | last post by:
I have a problem, to which I have been unable to find a solution for days now, after checking numerous references (both in books and online). Perhaps someone here can help. Here's my problem: ...
7
11917
by: Martin Robins | last post by:
I am currently looking to be able to read information from Active Directory into a data warehouse using a C# solution. I have been able to access the active directory, and I have been able to return...
6
4803
by: Jake Barnes | last post by:
I was just reading this article on Ajaxian: http://ajaxian.com/archives/show-love-to-the-object-literal This is a newbie question, but what is the object literal? I thought it was like an...
26
5631
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
12
5502
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
23
4308
by: Hugh Oxford | last post by:
How do I get an object's name? EG. $obj_FOO = new Bar; echo $obj_FOO->getName(); 'obj_FOO'
0
7112
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
7146
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,...
0
7183
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
7356
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
5448
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,...
0
3084
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...
0
1389
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 ...
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
277
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...

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.