Results 1 to 3 of 3

Thread: [RESOLVED] Retrieve value of all properties from an object

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    52

    Resolved [RESOLVED] Retrieve value of all properties from an object

    Hi, I'm wondering if there is another way of doing what I wanted to happen. I have an object which have 4 properties. I assigned value on those properties.

    Object Name: objEmployee
    Property: EmpID, EmpFirstName, EmpMidName, EmpLastName

    Code:
    With objEmployee
         .EmpID = 12345
         .EmpFirstName= "Russell"
         .EmpMidName= "Bell"
         .EmpLastName= "Champman"
    End With
    I need to manipulate the values assigned on each property. Currently, i'm manipulating the values using this code:

    Code:
    With objEmployee
         .EmpFirstName= Iff(IsNothing(.EmpFirstName), "Default Value", .EmpFirstName)
         .EmpMidName=  Iff(IsNothing(.EmpMidName), "Default Value", .EmpMidName)
         .EmpLastName=  Iff(IsNothing(.EmpLastName), "Default Value", .EmpLastName)
    End With
    What I wanted to happen is to manipulate those fields using a loop, something like this:

    Code:
    for i = 0 to 3
        objEmployee(i).value = Iff(IsNothing(objEmployee(i).value), "Default Value", objEmployee(i).value)
    loop
    Is there any way to do it? Thank you in advance.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Retrieve value of all properties from an object

    Firstly, there's no such thing as 'Iff'. Presumably you mean 'IIf', i.e. the inline If function. Secondly, don't use the IIf function in VB 2008 or later. There's an If operator that is always better than the IIf function. This:
    vb.net Code:
    1. With objEmployee
    2.      .EmpFirstName= Iff(IsNothing(.EmpFirstName), "Default Value", .EmpFirstName)
    3.      .EmpMidName=  Iff(IsNothing(.EmpMidName), "Default Value", .EmpMidName)
    4.      .EmpLastName=  Iff(IsNothing(.EmpLastName), "Default Value", .EmpLastName)
    5. End With
    would be better written like this:
    vb.net Code:
    1. With objEmployee
    2.      .EmpFirstName = If(.EmpFirstName Is Nothing, "Default Value", .EmpFirstName)
    3.      .EmpMidName = If(.EmpMidName Is Nothing, "Default Value", .EmpMidName)
    4.      .EmpLastName = If(.EmpLastName Is Nothing, "Default Value", .EmpLastName)
    5. End With
    The even better news is that there's another If operator that works as a null-coalescing operator, i.e. it says "use the value of this expression unless it's Nothing, in which case use the value of this other expression". That means that an even better way to write that code would be like this:
    vb.net Code:
    1. With objEmployee
    2.      .EmpFirstName = If(.EmpFirstName, "Default Value")
    3.      .EmpMidName = If(.EmpMidName, "Default Value")
    4.      .EmpLastName = If(.EmpLastName, "Default Value")
    5. End With
    As for the question, there is a way you can do that but it's a bad idea. You would do it using Reflection and it is convoluted enough that you would end up writing more code for three properties and it would execute slower too. There are times when you might want to use it, e.g. there are a very large number of properties or you want to be able to handle more than one type. For the record, it might look like this:
    vb.net Code:
    1. Dim type = objEmployee.GetType()
    2. Dim properties = type.GetProperties().Where(Function(pi) pi.PropertyType Is GetType(String))
    3.  
    4. For Each [property] In properties
    5.     [property].SetValue(objEmployee, If([property].GetValue(objEmployee), "Default Value"))
    6. Next
    Last edited by jmcilhinney; Sep 26th, 2018 at 02:53 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    52

    Re: Retrieve value of all properties from an object

    Hi @jmcilhinney! Yup, i was referring to 'IIf'.

    Thank you for the corrections/suggestions you've made, I will recode my program now and apply the learnings from you. Cheers!

Tags for this Thread

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