Solved

Best way of comparing values from multiple instances of the same object

Posted on 2006-11-09
5
177 Views
Last Modified: 2010-04-23
Hi,

I'm trying to figure out the best way of doing this...  

On the business side, the application tracks budget information for Clients.  Each client can have 0..many Groups.  For a Client that has Groups, budgets can exist at just the Client level or the Client level and the Group level.

On the programming side...  I have an object, call it Budget, whose properties are populated with values from a stored proc when it's instantiated.  It can either take the form of a ClientBudget or a PolicyGroupBudget.  

If a Budget is being viewed for a Group, for each field on a form I want to set the value as follows.  If the field has a non-null/blank value at the Group level, then use that.  If not, then use the Client level value if it is non-null/blank.  Otherwise, make the field blank.

I tried to do something like this:

objClient = New BusinessObjects.Budget(Me.ClientID)
objGroup = New BusinessObjects.Budget(Me. GroupID)

MyField.Value = GetObjectValue(objGroup, objClient, FieldName)

And the function would look something like this:

Private Function GetObjectValue(ByVal objGroup As BusinessObjects.Budget, ByVal objClient As BusinessObjects.Budget, ByVal FieldName As String) As Object
        Try
            If mblnFormIsAClientBudget Then
                GetObjectValue = objClient.Fieldname
          (More code here…)

            End If
        Catch ex As Exception

        End Try
    End Function

The problem is, it won’t let me do the objClient.FieldName b/c of course, FieldName is a string holding the name of the field to pull.  

Does anyone know how to make this work?  If not, is there a better way of doing this?  I tried doing it right in the original Sub using Iif’s but it got messy.

Thanks so much for the help!
0
Comment
Question by:MrDeveloper
  • 3
5 Comments
 
LVL 5

Expert Comment

by:xersoft
ID: 17914552
Actually you *could* access the field name from just a string name kind of as you are doing using the following function. There are some situations where this will not work.

The function takes the object you wish to access and the property/field/method you wish to return a value from. It uses reflection to go and get the value and then returns it. If it can’t find a property/field/method with the given name or proper parameter configuration it will return nothing.

Make sure you check the return from this function before you use the value.

I tested the function on a simple object that had public fields, properties and methods that returned values (functions)

The functions and properties MUST not have parameters or they can not be used with this function.


    Public Function GetValue(ByVal Obj As Object, ByVal Name As String) As Object
        'use reflection to get the property or field name from the object
        Dim Members() As Reflection.MemberInfo = Obj.GetType.GetMember(Name, Reflection.MemberTypes.Field Or Reflection.MemberTypes.Property Or Reflection.MemberTypes.Method, Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public)
        If Members.Length < 1 Then Return Nothing

        Dim areturn As Object = Nothing

        'look at the members we found
        For Each member As Reflection.MemberInfo In Members

            If member.MemberType = Reflection.MemberTypes.Field Then
                'get the value of the field
                areturn = DirectCast(member, Reflection.FieldInfo).GetValue(Obj)

            ElseIf member.MemberType = Reflection.MemberTypes.Property Then
                'ensure we can read and then get the value of the property
                'only use properties that have no parameters
                Dim prop As Reflection.PropertyInfo = DirectCast(member, Reflection.PropertyInfo)
                If prop.CanRead AndAlso prop.GetIndexParameters.Length = 0 Then
                    areturn = prop.GetValue(Obj, Nothing)
                End If

            ElseIf member.MemberType = Reflection.MemberTypes.Method Then
                'ensure there is a return value and then call the function to get the value
                Dim method As Reflection.MethodInfo = DirectCast(member, Reflection.MethodInfo)
                If Not method.ReturnType Is Nothing AndAlso method.GetParameters.Length = 0 Then
                    areturn = method.Invoke(Obj, New Object() {})
                End If

            End If

            'if we found a value then drop out of loop
            If Not areturn Is Nothing Then Exit For
        Next


        Return areturn
    End Function
0
 
LVL 5

Assisted Solution

by:xersoft
xersoft earned 250 total points
ID: 17914572
Using the code above you could write your function like this:

   Private Function GetObjectValue(ByVal objGroup As BusinessObjects.Budget, ByVal objClient As BusinessObjects.Budget, ByVal FieldName As String) As Object
        Try
            Dim itm1 As Object = GetValue(objClient, FieldName)
            If Not itm1 Is Nothing Then Return itm1
            Dim itm2 As Object = GetValue(objGroup, FieldName)
            Return itm2
        Catch ex As Exception
            Throw New ApplicationException("Can not find a property, field or function with the name: " & FieldName & " that contains a valid value.")
        End Try
    End Function
0
 
LVL 24

Accepted Solution

by:
Jeff Certain earned 250 total points
ID: 17915839
A better OO approach might be to have GroupBudget.GetValue(), which calls GroupBudget.ClientBudget.GetValue if the value is null. Then your code just looks like this pseudo-code:

Class Group
Function GetValue
if _value is nothing then
  return Client.GetValue
else
  return _value
end if

Class Client
Function GetValue
Return _value
0
 
LVL 5

Expert Comment

by:xersoft
ID: 17916233
I agree with Chaosian, that would be a better way to do it. You'd have to set it up in each property but once it was done you'd be good to go.

Plus using reflection, although neat, is not always the best way to go due to speed concerns and other typing issues.
0
 

Author Comment

by:MrDeveloper
ID: 17916472
thanks so much guys.  Those are both really good solutions.  I'm going to use the GetValue (really cool...) but I'm splitting the points.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now