Author Topic: Select A Property Using a String  (Read 2570 times)

0 Members and 1 Guest are viewing this topic.

wannabe

  • Guest
Select A Property Using a String
« on: April 07, 2009, 05:07:41 PM »
Suppose I've got a method that needs to a select a file that's located in my Properties.Resources, and the best I can do is use a string whose name corresponds to the name of the property; is there any way I can use that string to select a property with an identical filename.

I understand at runtime that it will cause an Exception to be thrown if there is no property with that name because the method is contingent on a return value. However, in this instance I can guarantee that my string will replicate one of the files in the Properties.Resource; since the string is not entered at runtime.

Using Linq crossed my mind to populate a list taking each file from the Properties.Resource then grouping that variable by my specified criteria and working from there.

Any suggestions?

The callByName function in Visual Basic looks as though it might be the one. Unfortunately I couldn't find a parallel for C#.

SomeCallMeDave

  • Guest
Re: Select A Property Using a String
« Reply #1 on: April 07, 2009, 05:32:25 PM »
You may be able to use System.Reflection.  But I'm not sure that I totally understand the problem.

But using Reflection, one can call methods on objects using a string-representation of the method's name.


wannabe

  • Guest
Re: Select A Property Using a String
« Reply #2 on: April 08, 2009, 03:23:56 AM »
Instead of typing Properties.Resources.**Specified Property's name**.

I need to replace the name of the property with a string that should be indentiacally worded.

eg. If I had a property called picture1.png, to select with code/intellisense it would be Properties.Resources.Picture1.png.

What I want to do is do this.

string name = picture1.png

Properties.Resources.name; <----this is my string whose name corresponds witha  property in the Properties.Resources.

Sorry to appear in any way  condesceding.

MikeTuersley

  • Guest
Re: Select A Property Using a String
« Reply #3 on: April 09, 2009, 12:17:07 AM »
Maybe you should post some sample code because you still aren't making any sense. Properties hold values, not objects, so I'm wondering if you are referring to embedded resources??? Where your image is embedded at compile and now you are trying to access it???

Ricky_76

  • Guest
Re: Select A Property Using a String
« Reply #4 on: April 18, 2009, 01:29:33 PM »
Hi,

time ago I asked here the same Question.
Same help, but after few tries nothing to do.
Till now I used other methods to reach my goals, but today (2009.04.18)
I decide to find THE solution of the Case.

So let me show you my attempt:

-1- Defining the Class

Code: [Select]
Imports System.Reflection
Public Class Form1
    Public Class Person
        Public Name As String
        Public Surname As String
        Public Age As Short
        Public Gender As String
        Sub New()
            Name = String.Empty
            Surname = String.Empty
            Age = 0
            Gender = "'Undefined'"
        End Sub
        Sub New(ByVal sName As String, ByVal sSurname As String, Optional ByVal iAge As Short = 0, Optional ByVal sGender As String = "Undefined")
            Name = sName
            Surname = sSurname
            Age = iAge
                Gender = sGender
        End Sub
        Function Info() As String
            Dim sInfo As String = String.Empty
            Dim Tp As Type = Me.GetType
            For Each FI As FieldInfo In Tp.GetFields()
                If sInfo = String.Empty Then
                    sInfo = FI.Name & ": " & FI.GetValue(Me)
                Else
                    sInfo &= vbNewLine & FI.Name & ": " & FI.GetValue(Me)
                End If
            Next FI
            Return sInfo
        End Function
    End Class


-2- Asking the result

Code: [Select]
    Sub GetInfo()
        Dim P As New Person("Alfred", "Wilson")
        MsgBox(P.Info)
    End Sub

-3- The Class Function 'Info' returns a string containing all the Fields of the class, with associated values, like I need:

Name: Alfred
Surname: Wilson
Age: 0
Gender: 'Undefined'

Hope it helps you!
Let me know if something is not so clear.

/r

ps
I have not tryed this with 'My.Resources' ... but with little code changes ...
« Last Edit: April 20, 2009, 02:42:19 AM by Ricky_76 »

MikeTuersley

  • Guest
Re: Select A Property Using a String
« Reply #5 on: April 20, 2009, 10:23:10 AM »
Well...what is your question? Are you trying to get a property by using a string? Like "age" and you want Person.Age returned? If that's it, you need to use a switch [c#] or select case [vb] statement that tests the string and returnes the appropriate response. The limitation is that you have to know ahead of time what the string could/should be.

wannabe

  • Guest
Re: Select A Property Using a String
« Reply #6 on: April 20, 2009, 01:36:31 PM »
Imagine this:

There is a property in my resources called "pictureOne" which would be accessed by "Properties.Resources.pictureOne;"

string bitName = pictureOne;

Bitmap bitOne = Properties.Resources."pictureOne".

This is a simple example and not the full scenario.

Ricky_76

  • Guest
Re: Select A Property Using a String
« Reply #7 on: April 21, 2009, 08:11:37 AM »
Hi, again

last time I misunderstand your request, sorry!

btw try this

Code: [Select]
Public Class Form1
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    PictureBox1.Image = GetRes("ImageOne")
  End Sub
  Function GetRes(ByVal name As String) As Image
    Dim resSet As Resources.ResourceSet
    resSet = My.Resources.ResourceManager.GetResourceSet(My.Application.Culture, True, True)
    Dim enumerator As IDictionaryEnumerator = resSet.GetEnumerator
    While (enumerator.MoveNext)
      If TypeOf enumerator.Value Is Bitmap And enumerator.Key = name Then
        Dim image As Bitmap = Nothing
        image = enumerator.Value
        Return image
      End If
    End While
    Return Nothing
  End Function
End Class

The  'Sub Form1_Load' call the function GetRes passing the name of the Resource to set as image in the PictureBox1.
The function 'GetRes' iterate through the My.Resources to find a Bitmap corrisponding to the name passed as parameter.

Let us know
/r
« Last Edit: April 21, 2009, 08:16:10 AM by Ricky_76 »