471,350 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

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 1369
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by sunaina | last post: by
4 posts views Thread by Avi Kak | last post: by
100 posts views Thread by E. Robert Tisdale | last post: by
6 posts views Thread by Tom | last post: by
7 posts views Thread by Martin Robins | last post: by
6 posts views Thread by Jake Barnes | last post: by
26 posts views Thread by yb | last post: by
12 posts views Thread by Andrew Poulos | last post: by
23 posts views Thread by Hugh Oxford | last post: by

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.