Reflection Question

C

Chad

2131 Public Property ContactId() As SqlInt32

2132 Get

2133 Return MyBase.SqlInt32Property("ContactId")

2134 End Get

2135 Set(ByVal Value As SqlInt32)

2136 MyBase.SqlInt32Property("ContactId") = Value

2137 End Set

2138 End Property



I have a short property shown above. My app is going to have maybe properties with various names, but they will all follow a common pattern: The string refrenced in the Get and Let routine SHOULD ALWAYS equal the property name.

A slightly better way of ensuring that the string is not mispelled, it to define a module level string constant, and to reference it in the GET and LET routines. However, it would be nice if the Constant were local to both the GET and the LET routines, not the whole module. Not to mention, there is always a chance of a cut and paste error where one is changed and the other is not, leading to an error.

--------------------------------------------------------------------------------


Private Const m_ContactId As String = "ContactId"

2131 Public Property ContactId() As SqlInt32
2132 Get

2133 Return MyBase.SqlInt32Property(ContactId)

2134 End Get

2135 Set(ByVal Value As SqlInt32)

2136 MyBase.SqlInt32Property(ContactId ) = Value

2137 End Set

2138 End Property


--------------------------------------------------------------------------------



Perhaps, what I am looking for is to, if it is possible with Reflection, to dynamically determine the name of the Property sub of the current context and pass that string.

Private Const m_ContactId As String = "ContactId"

2131 Public Property ContactId() As SqlInt32
2132 Get

2133 Return MyBase.SqlInt32Property(Reflection.GetCurrentPropertyName())

2134 End Get

2135 Set(ByVal Value As SqlInt32)

2136 MyBase.SqlInt32Property(Reflection.GetCurrentPropertyName()) = Value

2137 End Set

2138 End Property


That way, if we cut and paste the property code, we do not have to worry about updating the code to change the Property name in two places, and to make sure the spelling of the string passed exactly matches the name of the property in which it is enclosed.


Is something like this possible? I imagine it looks a bit unusual.
 
C

Chad

Sorry, not maybe properties, many properties.

2131 Public Property ContactId() As SqlInt32

2132 Get

2133 Return MyBase.SqlInt32Property("ContactId")

2134 End Get

2135 Set(ByVal Value As SqlInt32)

2136 MyBase.SqlInt32Property("ContactId") = Value

2137 End Set

2138 End Property


I have a short property shown above. My app is going to have maybe properties with various names, but they will all follow a common pattern: The string refrenced in the Get and Let routine SHOULD ALWAYS equal the property name.

A slightly better way of ensuring that the string is not mispelled, it to define a module level string constant, and to reference it in the GET and LET routines. However, it would be nice if the Constant were local to both the GET and the LET routines, not the whole module. Not to mention, there is always a chance of a cut and paste error where one is changed and the other is not, leading to an error.

------------------------------------------------------------------------------


Private Const m_ContactId As String = "ContactId"

2131 Public Property ContactId() As SqlInt32
2132 Get

2133 Return MyBase.SqlInt32Property(ContactId)

2134 End Get

2135 Set(ByVal Value As SqlInt32)

2136 MyBase.SqlInt32Property(ContactId ) = Value

2137 End Set

2138 End Property



------------------------------------------------------------------------------



Perhaps, what I am looking for is to, if it is possible with Reflection, to dynamically determine the name of the Property sub of the current context and pass that string.

Private Const m_ContactId As String = "ContactId"

2131 Public Property ContactId() As SqlInt32
2132 Get

2133 Return MyBase.SqlInt32Property(Reflection.GetCurrentPropertyName())

2134 End Get

2135 Set(ByVal Value As SqlInt32)

2136 MyBase.SqlInt32Property(Reflection.GetCurrentPropertyName()) = Value

2137 End Set

2138 End Property


That way, if we cut and paste the property code, we do not have to worry about updating the code to change the Property name in two places, and to make sure the spelling of the string passed exactly matches the name of the property in which it is enclosed.


Is something like this possible? I imagine it looks a bit unusual.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top