Problem with Structures

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simang

    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 GetStructureVal ue(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
  • Peter Jausovec

    #2
    Re: Problem with Structures

    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" <mayetski@gmail .com> wrote in message
    news:d39c956e.0 411090418.3a1d7 0ed@posting.goo gle.com...[color=blue]
    > 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 GetStructureVal ue(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[/color]


    Comment

    • Jakob Christensen

      #3
      RE: Problem with Structures

      You can do this using reflection (import the System.Reflecti on namespace).
      Your method GetStructureVal ue might look something like this:

      Private Function GetStructureVal ue(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 GetStructureVal ue like this:

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

      HTH, Jakob.

      "Simang" wrote:
      [color=blue]
      > 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 GetStructureVal ue(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
      >[/color]

      Comment

      • Simang

        #4
        Re: Problem with Structures

        Hello Jakob,

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

        - Simang

        "Jakob Christensen" <jch@REMOVEpens ion.dk> wrote in message news:<78D628BB-144E-46CA-96E5-644D3FEA2E08@mi crosoft.com>...[color=blue]
        > You can do this using reflection (import the System.Reflecti on namespace).
        > Your method GetStructureVal ue might look something like this:
        >
        > Private Function GetStructureVal ue(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 GetStructureVal ue like this:
        >
        > Dim frm As New Forms
        > frm._01234 = "test"
        > Dim value As Object = GetStructureVal ue(frm, "01234")
        > MessageBox.Show (value.ToString ())
        >
        > HTH, Jakob.
        >
        > "Simang" wrote:
        >[color=green]
        > > 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 GetStructureVal ue(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
        > >[/color][/color]

        Comment

        Working...