|
-
Sep 26th, 2018, 01:09 AM
#1
Thread Starter
Member
[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.
-
Sep 26th, 2018, 02:50 AM
#2
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:
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
would be better written like this:
vb.net Code:
With objEmployee .EmpFirstName = If(.EmpFirstName Is Nothing, "Default Value", .EmpFirstName) .EmpMidName = If(.EmpMidName Is Nothing, "Default Value", .EmpMidName) .EmpLastName = If(.EmpLastName Is Nothing, "Default Value", .EmpLastName) 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:
With objEmployee .EmpFirstName = If(.EmpFirstName, "Default Value") .EmpMidName = If(.EmpMidName, "Default Value") .EmpLastName = If(.EmpLastName, "Default Value") 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:
Dim type = objEmployee.GetType() Dim properties = type.GetProperties().Where(Function(pi) pi.PropertyType Is GetType(String)) For Each [property] In properties [property].SetValue(objEmployee, If([property].GetValue(objEmployee), "Default Value")) Next
Last edited by jmcilhinney; Sep 26th, 2018 at 02:53 AM.
-
Sep 26th, 2018, 04:02 AM
#3
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|