473,397 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Problem with Structures

Hi everyone,

I have a structure with this format:
Public Structure Forms
Public _01234 as string
Public _04321 as string
Public _03456 as integer
End Structure

As you can see, the variable names are all numeric. What I want to do
is that given a numeric string, I have to determine if that string is
a valid variable of the Forms structure and if it is, then output the
value.
For example, the Forms structure has these values:

_01234 = "Test1"
_04321 = "Test2"
_03456 = 100

Lets say I have a function GetStructureValue(varName as string)...
If the varName passed is "01234" then the output of the function
should be "Test1", if 03456, the output is 100, if 98765, which is not
defined in the structure, an error or -1 should be passed.

Is there a way to achieve this in vb.net?

Anyone's help will be much appreciated.

Thanks in advance!

Simang
Jul 21 '05 #1
3 1321
Hi,

What kind of a problem are you trying to solve ? I think there ought to be
other solution than the one you proposed

Regards,
Peter Jausovec
(http://blog.jausovec.net)

"Simang" <ma******@gmail.com> wrote in message
news:d3**************************@posting.google.c om...
Hi everyone,

I have a structure with this format:
Public Structure Forms
Public _01234 as string
Public _04321 as string
Public _03456 as integer
End Structure

As you can see, the variable names are all numeric. What I want to do
is that given a numeric string, I have to determine if that string is
a valid variable of the Forms structure and if it is, then output the
value.
For example, the Forms structure has these values:

_01234 = "Test1"
_04321 = "Test2"
_03456 = 100

Lets say I have a function GetStructureValue(varName as string)...
If the varName passed is "01234" then the output of the function
should be "Test1", if 03456, the output is 100, if 98765, which is not
defined in the structure, an error or -1 should be passed.

Is there a way to achieve this in vb.net?

Anyone's help will be much appreciated.

Thanks in advance!

Simang

Jul 21 '05 #2
You can do this using reflection (import the System.Reflection namespace).
Your method GetStructureValue might look something like this:

Private Function GetStructureValue(ByVal frm As Forms, ByVal varName As
String) As Object
Dim t As Type = frm.GetType()
Dim field As FieldInfo = t.GetField("_" & varName)
Dim value As Object = field.GetValue(frm)
Return value
End Function

You can call GetStructureValue like this:

Dim frm As New Forms
frm._01234 = "test"
Dim value As Object = GetStructureValue(frm, "01234")
MessageBox.Show(value.ToString())

HTH, Jakob.

"Simang" wrote:
Hi everyone,

I have a structure with this format:
Public Structure Forms
Public _01234 as string
Public _04321 as string
Public _03456 as integer
End Structure

As you can see, the variable names are all numeric. What I want to do
is that given a numeric string, I have to determine if that string is
a valid variable of the Forms structure and if it is, then output the
value.
For example, the Forms structure has these values:

_01234 = "Test1"
_04321 = "Test2"
_03456 = 100

Lets say I have a function GetStructureValue(varName as string)...
If the varName passed is "01234" then the output of the function
should be "Test1", if 03456, the output is 100, if 98765, which is not
defined in the structure, an error or -1 should be passed.

Is there a way to achieve this in vb.net?

Anyone's help will be much appreciated.

Thanks in advance!

Simang

Jul 21 '05 #3
Hello Jakob,

Thanks for the reply. It's just what I was looking for. =D

- Simang

"Jakob Christensen" <jc*@REMOVEpension.dk> wrote in message news:<78**********************************@microso ft.com>...
You can do this using reflection (import the System.Reflection namespace).
Your method GetStructureValue might look something like this:

Private Function GetStructureValue(ByVal frm As Forms, ByVal varName As
String) As Object
Dim t As Type = frm.GetType()
Dim field As FieldInfo = t.GetField("_" & varName)
Dim value As Object = field.GetValue(frm)
Return value
End Function

You can call GetStructureValue like this:

Dim frm As New Forms
frm._01234 = "test"
Dim value As Object = GetStructureValue(frm, "01234")
MessageBox.Show(value.ToString())

HTH, Jakob.

"Simang" wrote:
Hi everyone,

I have a structure with this format:
Public Structure Forms
Public _01234 as string
Public _04321 as string
Public _03456 as integer
End Structure

As you can see, the variable names are all numeric. What I want to do
is that given a numeric string, I have to determine if that string is
a valid variable of the Forms structure and if it is, then output the
value.
For example, the Forms structure has these values:

_01234 = "Test1"
_04321 = "Test2"
_03456 = 100

Lets say I have a function GetStructureValue(varName as string)...
If the varName passed is "01234" then the output of the function
should be "Test1", if 03456, the output is 100, if 98765, which is not
defined in the structure, an error or -1 should be passed.

Is there a way to achieve this in vb.net?

Anyone's help will be much appreciated.

Thanks in advance!

Simang

Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ivan | last post by:
Hi All, I have a problem with marshaling complex structures (containing numbers, strings, arrays of another structures) to native C function in dll. I have already posted same question to...
4
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system...
4
by: Animesh | last post by:
Hi All, I don't know whethher this is possible or not. This is the result of a bad design problem. Here I go; I have a structure like this: typedef struct _s_index_entry { char *doc_id;...
7
by: Nikos Mitas | last post by:
I have the following structure: struct DIRECTORY{ char nameDir; struct DIRECTORY *parent; int numSubDir; struct DIRECTORY *subDir; };
7
by: Stephan Rose | last post by:
Ok here is my scenario I have an interface called IScalar which describes a one dimensional number that has a certain unit of measurement. This interface is used to create multiple structures,...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
11
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
44
by: svata | last post by:
Hello, I wonder how to resize such array of structures using realloc()? #include <stdio.h> #include <stdlib.h> #define FIRST 7 typedef struct { char *name;
4
by: cleanrabbit | last post by:
Hello! I hate having to do this, because im almost certain there is someone in the world that has come across this problem and i just havent found their solution yet, so i do appologise if this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.