Click here to Skip to main content
15,886,052 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create a structure and acess it's elements using a loop
Is it possible?

VB
<StructLayout(LayoutKind.Sequential)> _
    Private Structure data
        Public a As Integer
        Public b As sort
        Public c As sort
        Public d As Integer
        Public e As Integer
    End Structure


VB
Dim dat as data  'OBJECT TO STR
Dim ptr As IntPtr = AllocHGlobal(SizeOf(dat))
StructureToPtr(dat, ptr, True)



Now I have created a pointer "ptr"
The problem is that I want to acess the members of structure using a loop.
Is it possible?


VB
for i as integer=0 to 6
       ptr(i)=44
next
Posted
Updated 15-Oct-10 9:40am
v4
Comments
HimanshuJoshi 15-Oct-10 14:22pm    
Removed text-speak. Use full words when posting here.
DaveAuld 15-Oct-10 14:34pm    
Is it really a structure you need to be using for what you are trying to achieve?
Be Yourself 15-Oct-10 15:20pm    
Sorry But I was not Concerned about What the elements are and moreover it was just a sample.
But in real there are disimilar datas

Yes, Google "VB.net reflection". Reflection allows you to inspect types at runtime.
 
Share this answer
 
Comments
Be Yourself 16-Oct-10 9:15am    
Thanks I will search for it..
Be Yourself 18-Oct-10 2:43am    
I have searched for Reflections but that is a different thing from what I am trying to do..
AspDotNetDev 18-Oct-10 4:02am    
I'm pretty sure it is. What, specifically, do you think Reflection is incapable of?
Be Yourself 18-Oct-10 10:09am    
I just want to create a pointer to the structure (Let us say "ptr")
Now if I write ptr(0)=20 it should set the first datamember of the structure to 20 irrespective of whatever it's name might be.
Similarly ptr(1),ptr(2)...etc
AspDotNetDev 18-Oct-10 13:58pm    
That can be done with reflection. First, you can use refelection to programmatically (and at runtime) get a list of all the properties (or variables) on an object. You can even filter them out to, say, only use properties that are integers. You can then loop through those properties and set the value of each. If you must use the index syntax, you can create a wrapper class that acts like a list and allows you to very simply set values of properties based on index. Here is one place you might start: http://www.vb-helper.com/howto_net_list_properties.html
Why don't you create a collection (or an array) of integers instead? You can iterate through those objects.
 
Share this answer
 
Comments
Be Yourself 15-Oct-10 15:17pm    
A array of integers would not contain a integer and a sort datatype as i think U would be aware of.
#realJSOP 15-Oct-10 15:27pm    
A List would allow sorting. It would also be more versatile because you can add/remove items from it. An array is static.
Be Yourself 15-Oct-10 15:39pm    
PLZ Just help me understand if I can Use the pointer as I have Shown in code??
Is there any method to perform such operation in structure or I should use any other method to store disimilar elements and index them through loop.

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