473,385 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Possible to set/reference a property 'dynamically'?

Hi all

I've got a number of classes already developed (basic entity classes)
like the following:

Public Class Contact
Private _firstname as String
Private _age as Integer

Public Property FirstName As String
Get
Return Me._firstname
End Get
Set
If (Me._firstname = value) Then
Return
End If
Me._firstname = value
End If
End Set
End Property

Public Enum PropertyList
FirstName,
Age
End Enum

.....

There are a number of classes like this that describe different
entities (Company, User etc etc) - all of which have a public Enum that
lists each available property for the entity.

I need to write a (separate) function that takes type-safe parameters
based on the properties available with these classes - e.g.

MyFunction(Firstname="John")
or
MyFunction(classname, parameter=value)

I imagined that to do this I would need to create a local instance of
the relevent class (i.e. Contact) in the function and (possibly) use
the param that's passed in the function call in a property setter call,
but that's where I fell over really. Firstly because I won't know the
name of the property to set (and didn't want to use a load of Select
Cases) and secondly because it wouldn't enforce type-safety in the
function call doing it this way).

Firstly, is this possible? If so, how could I go about a) limiting the
parameter that's passed to the function to those listed in the class
enum, and secondly, is it possible to enforce the property-type of the
passed parameter (i.e. if I pass a Firstname param, it will only accept
a string, if I pass an Age param, it'll only accept an Int)?

Sorry if these are stupid questions but I'm still trying to learn .Net
and OOP.

Thanks in advance
Martin

May 18 '06 #1
4 4017
Pritcham wrote:
Hi all

I've got a number of classes already developed (basic entity classes)
like the following:

Public Class Contact
Private _firstname as String
Private _age as Integer

Public Property FirstName As String
Get
Return Me._firstname
End Get
Set

Firstly, the set portion of a property includes the value type:
Set(byval value as string)
Anything other than a string will cause an error.
If (Me._firstname = value) Then
Return
End If
Me._firstname = value
End If
End Set
End Property

Public Enum PropertyList
FirstName,
Age
End Enum

....

There are a number of classes like this that describe different
entities (Company, User etc etc) - all of which have a public Enum that
lists each available property for the entity.

The actual properties of the class list all the properties available in
the class. What is the point of the enum?
I need to write a (separate) function that takes type-safe parameters
based on the properties available with these classes - e.g.

MyFunction(Firstname="John")
or
MyFunction(classname, parameter=value)

I imagined that to do this I would need to create a local instance of
the relevent class (i.e. Contact) in the function and (possibly) use
the param that's passed in the function call in a property setter call,
but that's where I fell over really. Firstly because I won't know the
name of the property to set (and didn't want to use a load of Select
Cases) and secondly because it wouldn't enforce type-safety in the
function call doing it this way).


In order to set a property value, you will have to know the property
name. As such, you can just set the property value directly.
This sounds overly convoluted to me.

Tom
May 18 '06 #2
Hi

Thanks for the reply.

To answer your question(s), the enum is there as the properties are all
based on field names within a db table and this/these are used for
other things as well - the enum lists the actual fields which happen to
also be the name of the properties - just trying to see whether I could
use them in another way. I'm basically trying to incorporate some
type-safe SQL querying into the DAL utilising the properties of the
entities in the BLL.

I've done some more 'experimenting' in the meantime and think it might
be a lot easier to have a kind of helper function for this, passing any
entity to the function (params constrained to those implementing the
base entity interface) so I can basically create a temporary entity for
the query, use this to complete the necessary field/property data
(which deals with the type-safety issue), and pass this to the helper
function along with the other required params that define what this
element of the SQL statement is doing, and built it that way. I know
this is probably doing things 'the wrong way' but it should get the job
done for the meantime.

Thanks again for your input - it is appreciated

Martin

May 18 '06 #3
Pritcham,
Have you looked at CallByName?

Something like:
Private Sub MyFunction(ByVal theClass As String, ByVal theProperty As
PropertyList, ByVal theValue As Object)
Dim theType As Type = Type.GetType(theClass)
Dim theObject As Object = Activator.CreateInstance(theType)
CallByName(theObject, theProperty.ToString(), CallType.Set,
theValue)
End Sub

MyFunction("Msi.Viewer.Contact", PropertyList.FirstName, 1)

The "quirk" is that the name of the class needs to include the namespace.
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Pritcham" <do******************@hotmail.com> wrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
| Hi all
|
| I've got a number of classes already developed (basic entity classes)
| like the following:
|
| Public Class Contact
| Private _firstname as String
| Private _age as Integer
|
| Public Property FirstName As String
| Get
| Return Me._firstname
| End Get
| Set
| If (Me._firstname = value) Then
| Return
| End If
| Me._firstname = value
| End If
| End Set
| End Property
|
| Public Enum PropertyList
| FirstName,
| Age
| End Enum
|
| ....
|
| There are a number of classes like this that describe different
| entities (Company, User etc etc) - all of which have a public Enum that
| lists each available property for the entity.
|
| I need to write a (separate) function that takes type-safe parameters
| based on the properties available with these classes - e.g.
|
| MyFunction(Firstname="John")
| or
| MyFunction(classname, parameter=value)
|
| I imagined that to do this I would need to create a local instance of
| the relevent class (i.e. Contact) in the function and (possibly) use
| the param that's passed in the function call in a property setter call,
| but that's where I fell over really. Firstly because I won't know the
| name of the property to set (and didn't want to use a load of Select
| Cases) and secondly because it wouldn't enforce type-safety in the
| function call doing it this way).
|
| Firstly, is this possible? If so, how could I go about a) limiting the
| parameter that's passed to the function to those listed in the class
| enum, and secondly, is it possible to enforce the property-type of the
| passed parameter (i.e. if I pass a Firstname param, it will only accept
| a string, if I pass an Age param, it'll only accept an Int)?
|
| Sorry if these are stupid questions but I'm still trying to learn .Net
| and OOP.
|
| Thanks in advance
| Martin
|
May 19 '06 #4
Pritcham,
Doh! (clicked send too soon).

I should add that you could use straight reflection also, something like:

Private Sub MyFunction(ByVal theClass As String, ByVal theProperty As
PropertyList, ByVal theValue As Object)
Dim theType As Type = Type.GetType(theClass)
Dim theObject As Object = Activator.CreateInstance(theType)
Dim thePropertyType As System.Reflection.PropertyInfo =
theType.GetProperty(theProperty.ToString())
theValue = System.Convert.ChangeType(theValue,
thePropertyType.PropertyType)
thePropertyType.SetValue(theObject, theValue, Nothing)
End Sub

Watch the case of the property name, as GetProperty is normally case
sensitive. Alternatively you could use:

Dim thePropertyType As System.Reflection.PropertyInfo =
theType.GetProperty(theProperty.ToString(), Reflection.BindingFlags.Instance
Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.IgnoreCase)
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Pritcham" <do******************@hotmail.com> wrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
| Hi all
|
| I've got a number of classes already developed (basic entity classes)
| like the following:
|
| Public Class Contact
| Private _firstname as String
| Private _age as Integer
|
| Public Property FirstName As String
| Get
| Return Me._firstname
| End Get
| Set
| If (Me._firstname = value) Then
| Return
| End If
| Me._firstname = value
| End If
| End Set
| End Property
|
| Public Enum PropertyList
| FirstName,
| Age
| End Enum
|
| ....
|
| There are a number of classes like this that describe different
| entities (Company, User etc etc) - all of which have a public Enum that
| lists each available property for the entity.
|
| I need to write a (separate) function that takes type-safe parameters
| based on the properties available with these classes - e.g.
|
| MyFunction(Firstname="John")
| or
| MyFunction(classname, parameter=value)
|
| I imagined that to do this I would need to create a local instance of
| the relevent class (i.e. Contact) in the function and (possibly) use
| the param that's passed in the function call in a property setter call,
| but that's where I fell over really. Firstly because I won't know the
| name of the property to set (and didn't want to use a load of Select
| Cases) and secondly because it wouldn't enforce type-safety in the
| function call doing it this way).
|
| Firstly, is this possible? If so, how could I go about a) limiting the
| parameter that's passed to the function to those listed in the class
| enum, and secondly, is it possible to enforce the property-type of the
| passed parameter (i.e. if I pass a Firstname param, it will only accept
| a string, if I pass an Age param, it'll only accept an Int)?
|
| Sorry if these are stupid questions but I'm still trying to learn .Net
| and OOP.
|
| Thanks in advance
| Martin
|
May 19 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Epson Barnett | last post by:
Hi, I'm new to C# and I'd like to be able to reference a field of an object using a variable instead of literal text. In the PHP scripting language, you can create a variable: $var = "name";...
0
by: Nejat Ozsu | last post by:
Hi, Short of emitting dynamically assemblies I would like to be able to add metdadata dynamically at run to a property of my serialization class based on dynamically loaded plugins. I...
2
by: Steve Franks | last post by:
According to the docs you tell ASP.NET to use cookieless sessions by setting a value in the config.web file. However, what if I wanted to determine at run time whether or not I wanted to use...
3
by: Sophos | last post by:
Hi, I have a basic aspx page, in the Page_Init procedure I have a server.execute of another aspx page that adds some html code to my first page. However it also adds a control (an htmlimage),...
7
by: Buddy Ackerman | last post by:
I created this class Public Class HTMLFileInput : Inherits System.Web.UI.HtmlControls.HtmlInputFile Public Property Data As String Get Return ViewState("HTMLFileInput.Data") End Get Set...
7
by: Samuel | last post by:
Hi, I am building a page that makes use of user control as a templating technique. The following is that I have in mind and it is actually working: Root/ -- login.aspx -- login.aspx.vb --...
7
by: mef526 | last post by:
I would like to reference a dynamically created control and I know the name. I would like to use the following: Dim strName as String = "txtControl1" ' This is the ".Name" used when textbox was...
11
by: Brent Ritchie | last post by:
Hello all, I have been using C# in my programming class and I have grown quite fond of C# properties. Having a method act like a variable that I can control access to is really something. As...
10
by: Arpan | last post by:
Consider the following code that adds a table to a DataSet dynamically: <script runat="server"> Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) 'Create an empty DataSet Dim dSet As...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.