How to reference class properties using MyClass("PropertyName")

  • Thread starter Thread starter Siv
  • Start date Start date
S

Siv

Hi,
I want to write a function that I pass a string to that is the name of one
of my properties in my class.

I.e. My class could have 3 properties:

Forname
Surname
Initials

etc

I want to call my function like this:

RetVal=MyFunction("Surname")

Inside the function I will instantiate the class, call a routine that
populates the class with data from a given record in my database and then
using the passed string variable identify if the property with that name has
any value in it from the record, I will then return a true or false to the
caller, in my example populating the RetVal variable.

I can't figure out how I use the passed string to get at the one particular
property the caller wants to check. In my head using pseudo code I want to
say:

if MyClass(ParameterVariable) <> "" then
return True
else
return False
end if

I assume I would have to do something to my class to make that sort of
notation work??

Any help appreciated.

Siv
 
Hello Siv,

You could access those properties like this:

CallByName(MyObject,"Surname",CallType.Get)

However, I wonder why you want to go this way as it will be much slower
than using something like:

Private Enum MyClassFieldNameConstants As Integer
Undefined = 0
FirstName = 1
Surname = 2
Initials = 3
End Enum

Private Function GetFieldValue(ByVal MyClassFieldName _
As MyClassFieldNameConstants) As String
Select Case MyClassFieldName
Case MyClassFieldNameConstants.FirstName
Return oMyClass.FirstName

Case MyClassFieldNameConstants.Initials
Return oMyClass.Initials

Case MyClassFieldNameConstants.Surname
Return oMyClass.Surname

Case Else
Return ""
End Select
End Function

Best regards,

Martin
 
Martley,

You know that this kind of non strongly typed programming has a worse name
because to the error chance

You are absolute not the first doing this.

It is called late binding, which is for a lot of things done automatically
with option strict of

Just to inform you, do with it what you want.

Cor
 
Cor,
Thanks fo rthe advice, clearly I would want to avoid this, so can you give
me an alternate way of doing things that avoids that?

Basically I have an application that uses an SQL Server database to store
its information. The users want to ensure that some staff who are designated
as checkers can open my application, review what the junior members of staff
have done and then tick a box to say it has been checked. Only staff of the
checker level will see this check box. The app stores the checked state and
also the user name of the checker.

The problem I have been having with this is that another checker may load
the job to review some other aspect update the screen and my existing routine
will update their user name as the person who checked it which is incorrect.

To avoid this I want to ensure that when the screen is updated, if there is
already a value in the database for the checkbox state and the checking user
then it shouldn't modify the existing details.

Now I could hard code the checking routine to just check those two fields
and react appropriately when updating the record, but I can see a use for
this function in lots of places elsewhere in the application and am trying to
create a function that you pass database field names (which are identical to
the properties of my class) and the function opens my class loads it with the
data from the existing database record (if there is one) and uses the passed
field name to check that field in the record that is now held in my class.
If there is already some data in that field I know this is not the first
checking user to be saving the record and so can skip updating the checker's
user name and the checkbox state.

For various reasons when a new record is created in my application the app
has to create a record almost immediately just so we have a main record ID
that can be used as a foreign key for other tables that are connected to the
main table. So by the time the user is updating the record for the first
time there always will be an existing record that contains minimum data.
When they hit the update button the vast majority of the data is updated.
This is why I cannot just check if there is an existing record.

Siv
 
The way to make this a general procedure is to create properties with
standard property names (eg, "Checker" and "Checked") for every class
involved in this type of processing. The controlling routine accesses these
properties to find the checked status of the class, and sets them to new
values (if appropriate) before updating the database. The property get/set
routines within the class do the required conversion between these standard
property values and the underlying database fields (or controls), which
could, of course, be different for each class or (depending on just what
your architecture is) could be dependant on which table is loaded. The
advantage of this approach is that the controlling routine does not even
have to know whether or not checking the checked state is necessary. For
instance, if the class does not need checking or check state updating, then
the properties for that class can be hardwired to always return Checked =
True and to simply ignore attempts to set the properties to some other
value. And that, in turn, means that the indication as to whether or not a
particular class (or table) needs Check State processing could reside in a
database table, instead of being hardcoded in either the class or the
controlling process.
 
Siv,

I am not sure if the answer from James fit you, it was my first idea too.

However, I think that I would create simply some wrapper classes that holds
as properties your own types (classes).
That wrapper class(es) has than the properties like James declares.

Cor
 
Back
Top