469,625 Members | 1,446 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,625 developers. It's quick & easy.

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 1118
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Thomas Paul Diffenbach | last post: by
4 posts views Thread by Animesh | last post: by
7 posts views Thread by Nikos Mitas | last post: by
7 posts views Thread by Stephan Rose | last post: by
7 posts views Thread by John Dann | last post: by
2 posts views Thread by ajikoe | last post: by
44 posts views Thread by svata | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.