C# Object to X Type Question

  • Thread starter Thread starter Greg Roberts
  • Start date Start date
G

Greg Roberts

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.


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;
};
 
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
 
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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top