Click here to Skip to main content
15,889,852 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi all, I'm wondering if you could help me.

I want to know how to write the C# equivalent, or a better way of writing of the following c structure.


struct SoftwareLayer
{
  char* swSrcPathsBufferPtr;
  char* utCtrPathsBufferPtr;
  char* swSrcFileNameBufferPtr;
  char* utCtrFileNameBufferPtr;
  unsigned int foo;
  unsigned int bah;
};


I want to make use of a List in C#, so typically the C# code should look like something like this:
E.g


public unsafe struct SoftwareLayer
{
  List<string> *swSrcPathBufferPtr;
  List<string> *utCtrPathsBufferPtr;
  List<string> *swStrFileNameBufferPtr;
  List<string> *utCtrFileNameBufferPtr;
  uint foo;
  uint bah;
};</string></string></string></string>


I'm getting the following problem: "Cannot take the address of, get the size of, or declare a pointer to a managed type ('System.Collections.Generic.List<string></string>')"

Question:
How would one define a structure to have a member that points to a List?
Your help would be appreciated, thx.

Regards, :)
Rudolf
Posted
Updated 7-Nov-10 22:01pm
v3
Comments
Radhakrishnan G. 8-Nov-10 4:39am    
struct is a value type in C#, so it is better to use value types in it, List is a managed type(Systems.Collections.List)??
R. Erasmus 8-Nov-10 6:49am    
better however I'm sure not impossible. I've decided on a different approach altogether. Thx for the help.

Hi,
C#
public struct SoftwareLayer
{
public List<char> swSrcPathBufferPtr;
public List<char> utCtrPathsBufferPtr;
public List<char> swStrFileNameBufferPtr;
public List<char> utCtrFileNameBufferPtr;
public uint foo;
public uint bah;
}

jerem
 
Share this answer
 
v2
Have a look here:
http://www.developerfusion.com/article/84519/mastering-structs-in-c/[^]

The list is a generic type for objects and since a struct is a value type the compiler isn't all that happy about it. Also, the ArrayList would be inefficient to use because it would do boxing and unboxing, which will give you a performance hit. A solution could be to wrap the struct in a class for storage. The class can expose the struct as a property and also make the values accessible using properties. This makes accessing is more easy and you can use that class with the generic list.

Good luck!
 
Share this answer
 
Comments
Dalek Dave 8-Nov-10 4:23am    
Good Answer.
R. Erasmus 8-Nov-10 4:26am    
I've decided to go the class/property route as you suggested thanks.
Rajesh Anuhya 8-Nov-10 6:46am    
Good Call

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900