locked
Call Class variable through a string RRS feed

  • Question

  • This may be a bit of a stupid or confusing qeustion but I can't figure it out for the life of me. I have a function, AddItem, that calls another function, GetInventorySlot. GetInventorySlot returns a string depending on which of the 21 slots that are empty. "_SETTINGS.i1", "_SETTINGS.i2", "_SETTINGS.i3", etc. I want to use that string to call that variable in my _SETTINGS class and then set that variable equal to the name of the item that I am adding.

    Any ideas?

    Friday, August 24, 2012 8:08 PM

Answers

  • Imports System.Reflection
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim settingsType = GetType(_SETTINGS)
    
            Dim prop As PropertyInfo = settingsType.GetProperty("i1")
            ' Get the current value of _SETTINGS.i1
            Dim currentValue As String = prop.GetValue(settingsType, Nothing).ToString
    
            MsgBox(currentValue)
    
            ' Set _SETTINGS.i1 to a new value
            prop.SetValue(settingsType, "Some New Value", Nothing)
    
            currentValue = prop.GetValue(settingsType, Nothing).ToString
    
            MsgBox(currentValue)
        End Sub
    End Class


    thanks for any help

    • Proposed as answer by Mark Liu-lxf Monday, August 27, 2012 6:46 AM
    • Marked as answer by Mark Liu-lxf Tuesday, September 4, 2012 7:59 AM
    Friday, August 24, 2012 10:43 PM
  • I think you would be better off using an array or list for i instead of having 21 variations of its name.

    --
    Andrew

    The other option here would be to use Dictionary(Of String, String) - you could then just use:

    _SETTINGS.Values[Setting] = item.Name

    This is assuming "Values" is a Shared Dictionary(Of String, String) in the _SETTINGS class.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Mark Liu-lxf Monday, August 27, 2012 6:47 AM
    • Marked as answer by Mark Liu-lxf Tuesday, September 4, 2012 7:59 AM
    Friday, August 24, 2012 11:11 PM

All replies

  • what are the my.settings names of those items?

    you can refer to my.settings items by string names:

    Dim something As something = My.Settings("propertyName")


    thanks for any help

    Friday, August 24, 2012 8:19 PM
  • I'm not using My.Settings

    I have a class called _SETTINGS with a bunch of properties. I'm trying to call these properties with the string.

    Friday, August 24, 2012 8:22 PM
  • ok. try this:

    Public Class Form1
    
        Dim s As New _SETTINGS
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            s.GetType().GetProperty("i1").SetValue(s, 20, Nothing) 'set value
            MsgBox(s.GetType().GetProperty("i1").GetValue(s, Nothing)) 'get value
        End Sub
    
    End Class


    thanks for any help

    Friday, August 24, 2012 8:29 PM
  • I'm not using My.Settings

    I have a class called _SETTINGS with a bunch of properties. I'm trying to call these properties with the string.

    You would need to use Reflection to accomplish this.  See: http://msdn.microsoft.com/en-us/library/ms173183.aspx

    The actual mechanism depends a lot on the class and properties (ie: if they're Shared or instance properties).


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by .paul. _ Friday, August 24, 2012 8:34 PM
    Friday, August 24, 2012 8:29 PM
  • Alright but is it possible to do this without creating a new instance of _SETTINGS, I'mtrying to do it so that it is all one instance throughout the whole program that way all the forms are insync.

    I tried just changing the s to _SETTINGS and removing the Dim s...etc but it gave me the error, reference to a non-shared member requires an object reference.

    Friday, August 24, 2012 8:52 PM
  • If they are instance properties (not Shared), then you need a way to get to the reference.  You don't have to create a new one - but you do need the reference to _SETTINGS.

    If they are shared proeprties, you can pass Nothing in for the instance, ie:

    Dim settingsType = TypeOf Settings
    
    settingsType.GetProperty("i1").SetValue(Nothing, "Some Value", Nothing)


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Friday, August 24, 2012 8:55 PM
  • @Paul Ishak

       The class is already declared public, "Public Class _SETTINGS"
       Unless the Module declaring is different.

    @Reed Copsey, Jr

       The 'Dim settingsType line gave a missing Is error and said that settingsType was not declared.

    I'm using shared properties in a public class, heres a snippet:

    Public Class _SETTINGS
        Public Shared Property Name As String
        Public Shared Property Level As Integer
        Public Shared Property hasSave As Boolean
        Public Shared Property i1 As String
        Public Shared Property i2 As String
        Public Shared Property i3 As String
        Public Shared Property i4 As String
        Public Shared Property i5 As String

    Friday, August 24, 2012 9:11 PM
  • Look at this screenshot, I hope it clears it up for you.

    Notice how you are able to access a variable declared in a module directly, whereas if its declared in the class, you cannot access it directly from another class unless you specify the class name followed by a period, then the property name.

    I think if you declare a public copy of your custom class from within a module, you can access it from within anywhere in your project.


    If you want something you've never had, you need to do something you've never done.



    • Edited by Paul Ishak Friday, August 24, 2012 9:22 PM
    Friday, August 24, 2012 9:19 PM
  • Oh okay, well that still brings me back to my original problem, I can't get the property out of the string to assign a value to it without VB wanting to edit the string instead.
    Friday, August 24, 2012 9:21 PM
  • Oh okay, well that still brings me back to my original problem, I can't get the property out of the string to assign a value to it without VB wanting to edit the string instead.

    What exactly does this mean? Please clarify.

    If you want something you've never had, you need to do something you've never done.

    Friday, August 24, 2012 9:24 PM
  • ok take a look at this pic to see if it clears it up for you....


    If you want something you've never had, you need to do something you've never done.

    Friday, August 24, 2012 9:27 PM
  • Are you trying to find a way to pass a function/sub as a parameter to another function/sub?

    If you want something you've never had, you need to do something you've never done.


    • Edited by Paul Ishak Friday, August 24, 2012 9:38 PM
    Friday, August 24, 2012 9:38 PM
  • Alright, lol I feel like I'm just confusing everyone and myself, so I'll try to be as specifc and clear when explaining this.

    I'm making a game and when I click a button it is suppossed to first call a function to  find the first open inventory slot. That inventory slot is returned through a string, its returned as "_SETTINGS.i1", "_SETTINGS.i2", etc. I'm trying to use this string to call the returned variable, in the _SETTINGS class, and assign a value to it. In this case it will be the name of the item (item.name).

    My problem is that I cannot get the variable out of the string to use it.

    _settings.GetProperty(OPENSLOT).SetValue(Nothing, item.name, Nothing)

    This would work, BUT I cannot use it without either creating a new instance of the _SETTINGS class, which I don't want to do because then not all forms will be able to acess those variables. I'm trying to keep it gobal and all in one spot. Or using this:

    Dim settingsType = TypeOf Settings

    But this gives me a 'Missing 'Is'' error

    heres some snippets if it will help:

    Fig 1. I click the button, the item is set to the joker helmet, it then goes to add the item.

    goto Fig 2.

        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            item = _ITEMS.jokerHelmet
            RpgInventory.AddItem(item)
            MsgBox(_SETTINGS.i1)
        End Sub

    Fig 2. Inventory spaces are checked for an emtpy slot, Setting is set to the nearest empty slot and is returned in this format: "i1", "i2", etc. else it is set to null and nothing happens.

    For testing purposes i1 is always open so the game proceeds to try and set that item into that slot if you do not already have that item.

    goto Fig 3

    Public Shared Function AddItem(item As RpgItem) As Boolean
      Dim Setting As String = _FUNCTIONS.GetSlot()
      If Setting = "null" Then
        MsgBox("You do not have the inventory space to hold this item.", vbOKOnly, "Oops.")
      End If
      If Not _ItemList.ContainsKey(item) Then
        _FUNCTIONS.SetItem(item, Setting)
        _ItemList.Add(item, 1)
        Return True
      End If

    Fig 3. This is where I'm having trouble I cant get the property out of the Setting string and set it equal to the item's name (item.name)

    Once the item is successfully set it is suppossed to jump back up to Fig 1 and display, in a MsgBox, _Settings.i1's value which will be the item's name.

    Public Shared Function SetItem(ByVal item As RpgItem, ByVal Setting As String)

    Dim settingsType = TypeOf Settings
    settingsType.GetProperty(Setting).SetValue(Nothing, item.name, Nothing) Return true

    End Function



    • Edited by magnite7 Friday, August 24, 2012 10:02 PM
    Friday, August 24, 2012 9:43 PM
  • So - the problem was I didn't know the name of your class - I assumed (incorrectly) that _SETTINGS was a variable name.  This should work:

    Dim settingsType = TypeOf _SETTINGS
    
    Dim prop as PropertyInfo = settingsType.GetProperty("i1")
    
    ' Get the current value of _SETTINGS.i1
    Dim currentValue as String = prop.GetValue(Nothing)
    
    ' Set _SETTINGS.i1 to a new value
    prop.SetValue(Nothing, "Some New Value")


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Friday, August 24, 2012 10:20 PM
  • _SETTINGS is expecting a 'Is' afterwards

    settingsType is saying that it is not declared

    prop.GetValue is giving this error: Overload resolution failed because no acessible 'GetValue' accepts this number of arguments.

    prop.SetValue is giving this error: Overload resolution failed because no acessible 'SetValue' accepts this number of arguments.


    • Edited by magnite7 Friday, August 24, 2012 10:35 PM
    Friday, August 24, 2012 10:35 PM
  • This is where I'm having trouble I cant get the property out of the Setting string and set it equal to the item's name (item.name)

    I think that this is what you are trying to do.  Note that the discussion is mostly about Methods, but it works just as well with Properties.

    http://msdn.microsoft.com/en-us/library/22x2chfx.aspx
    Calling a Property or Method Using a String Name

    Friday, August 24, 2012 10:40 PM
  • Imports System.Reflection
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim settingsType = GetType(_SETTINGS)
    
            Dim prop As PropertyInfo = settingsType.GetProperty("i1")
            ' Get the current value of _SETTINGS.i1
            Dim currentValue As String = prop.GetValue(settingsType, Nothing).ToString
    
            MsgBox(currentValue)
    
            ' Set _SETTINGS.i1 to a new value
            prop.SetValue(settingsType, "Some New Value", Nothing)
    
            currentValue = prop.GetValue(settingsType, Nothing).ToString
    
            MsgBox(currentValue)
        End Sub
    End Class


    thanks for any help

    • Proposed as answer by Mark Liu-lxf Monday, August 27, 2012 6:46 AM
    • Marked as answer by Mark Liu-lxf Tuesday, September 4, 2012 7:59 AM
    Friday, August 24, 2012 10:43 PM
  • _SETTINGS is expecting a 'Is' afterwards

    settingsType is saying that it is not declared

    prop.GetValue is giving this error: Overload resolution failed because no acessible 'GetValue' accepts this number of arguments.

    prop.SetValue is giving this error: Overload resolution failed because no acessible 'SetValue' accepts this number of arguments.


    Sorry - bad VB on my part - your routine should likely convert to:

    Public Shared Sub SetItem(ByVal item as RpgItem, ByVal Setting As String)
        Dim settingsType = GetType(_SETTINGS)
        Dim prop as PropertyInfo = settingsType.GetProperty(Setting)
    
        prop.SetValue(Nothing, item.Name)
    End Sub


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Friday, August 24, 2012 10:44 PM
  • I think you would be better off using an array or list for i instead of having 21 variations of its name.

    --
    Andrew

    Friday, August 24, 2012 11:09 PM
  • I think you would be better off using an array or list for i instead of having 21 variations of its name.

    --
    Andrew

    The other option here would be to use Dictionary(Of String, String) - you could then just use:

    _SETTINGS.Values[Setting] = item.Name

    This is assuming "Values" is a Shared Dictionary(Of String, String) in the _SETTINGS class.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Mark Liu-lxf Monday, August 27, 2012 6:47 AM
    • Marked as answer by Mark Liu-lxf Tuesday, September 4, 2012 7:59 AM
    Friday, August 24, 2012 11:11 PM
  • CallByName doesn't allow me to call a property from my _SETTINGS class, but thanks though. KNowing this is there may help me in the future.

    Thank you .paul. and Reed Copsey the code provided did the trick perfectly. Thank you very much.

    I may change the i into a list/dictionary now because it seems much easier to work with.

    Friday, August 24, 2012 11:16 PM