Home All Groups Group Topic Archive Search About

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

Author
26 Jun 2009 9:31 PM
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

--
Martley, Near Worcester, UK

Author
26 Jun 2009 10:04 PM
James Hahn
You can use the CallByName method to do this.  See:
http://msdn.microsoft.com/en-us/library/chsc1tx6.aspx


Show quoteHide quote
"Siv" <S**@discussions.microsoft.com> wrote in message
news:08EA751A-F8DE-48E1-9D4C-372ED5368A9F@microsoft.com...
> 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
>
> --
> Martley, Near Worcester, UK
Are all your drivers up to date? click for free checkup

Author
27 Jun 2009 8:35 AM
Siv
James,
Thanks, will have a go using that method.

Siv

--
Martley, Near Worcester, UK


Show quoteHide quote
"James Hahn" wrote:

> You can use the CallByName method to do this.  See:
> http://msdn.microsoft.com/en-us/library/chsc1tx6.aspx
>
>
> "Siv" <S**@discussions.microsoft.com> wrote in message
> news:08EA751A-F8DE-48E1-9D4C-372ED5368A9F@microsoft.com...
> > 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
> >
> > --
> > Martley, Near Worcester, UK
>
>
Author
27 Jun 2009 6:40 AM
Cor Ligthert[MVP]
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



Show quoteHide quote
"Siv" <S**@discussions.microsoft.com> wrote in message
news:08EA751A-F8DE-48E1-9D4C-372ED5368A9F@microsoft.com...
> 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
>
> --
> Martley, Near Worcester, UK
Author
27 Jun 2009 9:04 AM
Siv
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
--
Martley, Near Worcester, UK


Show quoteHide quote
"Cor Ligthert[MVP]" wrote:

> 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
>
>
>
> "Siv" <S**@discussions.microsoft.com> wrote in message
> news:08EA751A-F8DE-48E1-9D4C-372ED5368A9F@microsoft.com...
> > 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
> >
> > --
> > Martley, Near Worcester, UK
>
>
Author
27 Jun 2009 1:10 PM
James Hahn
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.

Show quoteHide quote
"Siv" <S**@discussions.microsoft.com> wrote in message
news:8F4557C4-F0A5-4F26-9DB8-98CC9E79E865@microsoft.com...
> 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
> --
> Martley, Near Worcester, UK
>
Author
28 Jun 2009 6:28 AM
Cor Ligthert[MVP]
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

Show quoteHide quote
"Siv" <S**@discussions.microsoft.com> wrote in message
news:8F4557C4-F0A5-4F26-9DB8-98CC9E79E865@microsoft.com...
> 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
> --
> Martley, Near Worcester, UK
>
>
> "Cor Ligthert[MVP]" wrote:
>
>> 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
>>
>>
>>
>> "Siv" <S**@discussions.microsoft.com> wrote in message
>> news:08EA751A-F8DE-48E1-9D4C-372ED5368A9F@microsoft.com...
>> > 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
>> >
>> > --
>> > Martley, Near Worcester, UK
>>
>>

Bookmark and Share