Results 1 to 13 of 13

Thread: [RESOLVED] Refer to structure element using variable

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2018
    Posts
    159

    Resolved [RESOLVED] Refer to structure element using variable

    Hi

    If I have a simple structure set up like this:

    Code:
    Structure myfile
    
    Public fullpath As String
    Public numrecords As Integer
    public category as string
    
    End Structure
    I can refer to the 'fullpath' element easily enough like this:

    Code:
    myfile.fullpath
    Is there any way I can refer to an element using a variable?

    So it would be something like:

    Code:
    myvar="fullpath"
    myfile.(myvar)
    Is something like this possible? I'd like to apply the technique to a list of structures.

    Thanks for reading!

  2. #2
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Refer to structure element using variable

    if you have a list(of myfile), you can call the variable of the structure like that : list.item(i).fullpath for example. Why do you want to put the structure variable name into an other variable ?
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Refer to structure element using variable

    No, not like that.

    You could kind of do something like that via reflection, but that would be kind of like using a chainsaw to cut a steak.

    Whatever the objective is, though, there is a different way to accomplish the task that would be better. Not sure what the objective is, so I can't say which option is better.
    My usual boring signature: Nothing

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Refer to structure element using variable

    There's no simple way to do it like what you show. As Shaggy suggests you can access members via a name stored in a String using Reflection but the code requires several lines - not a major ordeal but significant for simple member access - and is orders of magnitude slower than direct access. Why exactly do you think that this is a good idea in the first place? Please explain what you're trying to achieve, rather than how you're trying to achieve it. We've seen basically this same question asked for no good reason on many previous occasions.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2018
    Posts
    159

    Re: Refer to structure element using variable

    Quote Originally Posted by jmcilhinney View Post
    There's no simple way to do it like what you show. As Shaggy suggests you can access members via a name stored in a String using Reflection but the code requires several lines - not a major ordeal but significant for simple member access - and is orders of magnitude slower than direct access. Why exactly do you think that this is a good idea in the first place? Please explain what you're trying to achieve, rather than how you're trying to achieve it. We've seen basically this same question asked for no good reason on many previous occasions.
    Fair enough - it's a bit too involved to post enough of the code for it to make sense to be honest, but essentially I've got a list of structures that hold information about the field positions of a bunch of text files I'm reading in.

    So, for example, the structure is:

    Code:
    myfile.add1
    myfile.add2
    myfile.add3
    myfile.pcode
    Each text file gets it's own list entry. In the structure, if the first line of the address in a text file is the 9th field, myfile.add1 gets a value of 9 and so on.

    There bits of the program where I thought it would be neater to refer to the element name with a variable, but the program is very much a work in progress, so if this is a no-no I'll do some restructuring to see if I can tidy it up some and find a better way.

    Cheers

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: [RESOLVED] Refer to structure element using variable

    Two things came to mind when I first saw the post. The first is a Dictionary, which doesn't look likely to suit you well at all, but they are good for things you want to look up.

    The other idea was an Enum that had the various fields:

    Code:
    Public Enum MyFields
     add1
     add2
     add3
     pcode
    End Enum
    Now you could write a function that would return something:

    Code:
    Public Function GetField(seekItm As yourStructure,someFields As MyField) As ....Something
     Select Case someFields
       Case add1
         Return seekItm.add1
       Case add2
         Return seekItm.add2
       Case add3
         Return seekItm.add3
       Case pcode
         Return seekItm.pcode
      End Select
    End Function
    Obviously, that isn't nearly as simple as what you started with, but you could improve on it by making it a member of the structure itself, in which case you could drop the first argument and the call would look like:

    Dim someValue = yourStructureInstance(add1)

    which isn't far from what you started out with. There are a couple problems with this design, though. For one thing, it isn't readily extensible. Add one more field and you have to add to the enum and add another Case statement to deal with the new field. Perhaps an even bigger problem is that this only makes sense in any way if all the fields are the same type. Otherwise, the return type of the function becomes seriously bad.

    So, I don't think that it's a good idea, but it is AN idea.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2018
    Posts
    159

    Re: [RESOLVED] Refer to structure element using variable

    Quote Originally Posted by Shaggy Hiker View Post
    Two things came to mind when I first saw the post. The first is a Dictionary, which doesn't look likely to suit you well at all, but they are good for things you want to look up.

    The other idea was an Enum that had the various fields:

    Code:
    Public Enum MyFields
     add1
     add2
     add3
     pcode
    End Enum
    Now you could write a function that would return something:

    Code:
    Public Function GetField(seekItm As yourStructure,someFields As MyField) As ....Something
     Select Case someFields
       Case add1
         Return seekItm.add1
       Case add2
         Return seekItm.add2
       Case add3
         Return seekItm.add3
       Case pcode
         Return seekItm.pcode
      End Select
    End Function
    Obviously, that isn't nearly as simple as what you started with, but you could improve on it by making it a member of the structure itself, in which case you could drop the first argument and the call would look like:

    Dim someValue = yourStructureInstance(add1)

    which isn't far from what you started out with. There are a couple problems with this design, though. For one thing, it isn't readily extensible. Add one more field and you have to add to the enum and add another Case statement to deal with the new field. Perhaps an even bigger problem is that this only makes sense in any way if all the fields are the same type. Otherwise, the return type of the function becomes seriously bad.

    So, I don't think that it's a good idea, but it is AN idea.
    Thanks - I've never used an Enum before, so worth looking at even if I don't use it for this particular requirement.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [RESOLVED] Refer to structure element using variable

    Quote Originally Posted by Precision999 View Post
    I've never used an Enum before
    I doubt that very much, although you may well not have declared your own such type before. If you have ever set a property for a form or control in the designer from a drop-down then, other than Booleans, it would have been an Enum, e.g. the Anchor and Dock properties of a control or the BorderStyle of a form. The MessageBox.Show method has several parameters that are Enum types and it returns a value of the DialogResult Enum. Etc, etc.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: [RESOLVED] Refer to structure element using variable

    Yeah, you've almost certainly USED them, but if you haven't created one, they can be a bit odd to get your head around. They're super simple, but kind of strange to think about.
    My usual boring signature: Nothing

  10. #10
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: [RESOLVED] Refer to structure element using variable

    Don't we use them when we call the colors ? i.e "colors.blue" or is it a class (the MSDN says it is a class but it really looks like the example given)
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: [RESOLVED] Refer to structure element using variable

    Color has member methods like FromARGB, so it isn't an Enum.
    My usual boring signature: Nothing

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [RESOLVED] Refer to structure element using variable

    EDIT: I was referring to the System.Drawing.Color structure below, which is what Windows Forms uses. WPF separates the equivalent functionality into the System.Windows.Media.Color and System.Windows.Media.Colors classes.

    ORIGINAL:
    Quote Originally Posted by Delaney View Post
    Don't we use them when we call the colors ? i.e "colors.blue" or is it a class (the MSDN says it is a class but it really looks like the example given)
    Color is a structure with A, R, G, and B properties to represent the alpha, red, green and blue components of the colour. Color.Blue and the like are Shared properties that return Color instances with specific values for those properties. You can also create your own Color instance with whatever values you want for those properties. An Enum doesn't have properties or methods. It simply has a set of fields, each with a name that represents a numeric value. The point of an Enum is to provide a specific set of valid values for a particular scenario that are easy for human beings to identify using the names and easy for the system to process using the numbers. If you were to use Integer values instead then you'd not be limited other than by the range of the Integer type and you'd have to memorise what each number meant, whereas if you used String values then you'd also be able to use any String rather than a specific set. Enums let you easily define a limited set of easily identifiable values. For instance, the DialogResult Enum has the following fields with corresponding numeric values:

    None: 0
    OK: 1
    Cancel: 2
    Abort: 3
    Retry: 4
    Ignore: 5
    Yes: 6
    No: 7

    When you write code, you use the field names and the code becomes self-documenting. It's not hard to remember and understand what OK and Cancel mean and you're also limited to just those meaningful values. When the code is compiled though, the system uses the Integer values, which is the fastest data type possible.

    If you read the documentation for the Color structure, it's nothing like that.

  13. #13
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: [RESOLVED] Refer to structure element using variable

    Ok thank you.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width