Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I have a need to store a struct int

Status
Not open for further replies.

dpanattoni

IS-IT--Management
Jan 29, 2002
76
US
I have a need to store a struct into a class variable that has no knowledge of the struct definition. For example, the class has:

public class Storage {
public object Data;
.
.
.
}

Within the program that is including the Storage class, there is a struct defined as:

public struct DataStruct {
public string Name;
public string Address;
.
.
.
}

From the program, it appears that I can store DataStruct into the class's Data object. (At least I'm not getting any errors so I assume it's working). But when I try to bring the class's Data object back into the DataStruct, I am getting an error at runtime.

Is this possible with C#? or is there a better way to define the class's data object?

In C, I define the data object as a void * and then I can cast the pointer as any type of struct that I need to from within the program. The program determines what type of struct is stored within the class's data object.

Thanks in advance.
DP
 
I think I figured it out.
By defining my DataStruct as a class instead of a structure, I am able to cast it into and from the object defined in the Storage class.

A struct is considered a value data-type rather than a reference data-type.

DP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top