473,704 Members | 6,309 Online
Bytes | Software Development & Data Engineering Community
+ 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 (!FileInfoEntri es.ContainsKey( Filename))
{
// First use of key part

Entry = new FileRecord();
Entry.nOpenCoun t = 0;
Entry.nReadCoun t = 0;
Entry.nWriteCou nt = nFileLine;
Entry.bMonitorM e = true;
Entry.nAverageR eadLength = 0;
FileInfoEntries .Add(Filename, (object)Entry);
}
else
{
// repeat of key part

nIndex = FileInfoEntries .IndexOfKey(Fil ename);
nObj = FileInfoEntries .GetByIndex(nIn dex);
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 nAverageReadLen gth;
public int nLastOpenTime;
public int nLastOpenMode;
public int nLastIOOperatio nTime;
public Boolean bStartupFile;
public Boolean bRuntimeFile;
public Boolean bShutdownFile;
};

Dec 6 '05 #1
3 1458
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.WriteLi ne(t1.MyField);
Console.WriteLi ne(t2.MyField);
Console.ReadLin e();
}
}

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******** ********@TK2MSF TNGP10.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 (!FileInfoEntri es.ContainsKey( Filename))
{
// First use of key part

Entry = new FileRecord();
Entry.nOpenCoun t = 0;
Entry.nReadCoun t = 0;
Entry.nWriteCou nt = nFileLine;
Entry.bMonitorM e = true;
Entry.nAverageR eadLength = 0;
FileInfoEntries .Add(Filename, (object)Entry);
}
else
{
// repeat of key part

nIndex = FileInfoEntries .IndexOfKey(Fil ename);
nObj = FileInfoEntries .GetByIndex(nIn dex);
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 nAverageReadLen gth;
public int nLastOpenTime;
public int nLastOpenMode;
public int nLastIOOperatio nTime;
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******** ********@TK2MSF TNGP10.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 (!FileInfoEntri es.ContainsKey( Filename))
{
// First use of key part

Entry = new FileRecord();
Entry.nOpenCoun t = 0;
Entry.nReadCoun t = 0;
Entry.nWriteCou nt = nFileLine;
Entry.bMonitorM e = true;
Entry.nAverageR eadLength = 0;
FileInfoEntries .Add(Filename, (object)Entry);
}
else
{
// repeat of key part

nIndex = FileInfoEntries .IndexOfKey(Fil ename);
nObj = FileInfoEntries .GetByIndex(nIn dex);
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 nAverageReadLen gth;
public int nLastOpenTime;
public int nLastOpenMode;
public int nLastIOOperatio nTime;
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
2577
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 user has to do is say yes or no to the questions that my program asks. I have coded until the part where once user thinks of an object and enters submit and the program asks him the first question. Now when the user enters yes/no for this first...
4
4757
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 curious about the precise definition of an object in Python. My curiosity was also driven by Wesley's statement that while every class instance is an object, not every object is a
4
3090
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 the recordset to loop thru the recordset, update values from the recordset and then update the database by passing parmeters to another stored procedure. I would like to use the recordset object but can it be used to pass a parameter to a stored...
100
5255
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
1555
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: I'm trying to define a series of radio objects as a large array for a survey, like this: 1) Does this make sense? <input type="radio" name="question" value="Y"> Yes <input type="radio" name="question" value="N"> No
7
11944
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 "DirectoryEntry" objects within the path that I specify (either using the DirectoryEnrtry.Children or using the DirectorySearcher class) and all started well and dandy! Now the problem; some of the properties of the DirectoryEntry objects being...
6
4832
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 array notation, but is it really its own thing? You can declare arrays this way, can't you? An object in Javascript is like a hash array that stores pointers to properties and methods? Or is object literal notation a convenience form, wholly...
26
5684
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 several parts of the DOM, but this does not include the window object. Thank you
12
5551
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. Foo = function(type) { this.num = 0; this.type = type this.trigger(); } Foo.prototype.trigger = function() {
23
4335
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
8763
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9266
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8972
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7878
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5926
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4435
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3128
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 we have to send another system
3
2073
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.