473,396 Members | 1,918 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,396 software developers and data experts.

Assign Public or Private variable thru Reflection vb.net/asp.net

I try to initialize some Public Variable in a class use for code-behind

The idea is to check all uxMsgXXX variables, declare in top of my class and
assign the string value from a DB request. Why do That ? Multilinguage
apps...

If I'm able to do so, I already have an interface to enter the translation
for each variables. Also, this way I don't need to deploy extra files for the
translation.

I can get all my fields from my class with System.Reflection.FieldInfo but
I'm not able to getvalue or setvalue. I can not find the good method/syntax
to do so.

Please help !!!

Ex.:
Public Class Extension
Inherits IcbWebform

public uxMsgError as string = "English Error Message"

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
Dim oTrans As New Translation
InitializeComponent()
oTrans.LoadLngString(GetType(Extension))
End Sub
End class

So when Page_Init() is finish, uxMsgError = "Message d'erreur Francais"

oTrans.LoadLngString()

Public Sub LoadLngString(ByVal oPage As Type)
Dim fieldInfo() As System.Reflection.FieldInfo
Dim value As String
Dim o As Type
Dim i As Integer
o = oPage
fieldInfo = oPage.GetFields()
For i = 0 To fieldInfo.Length - 1

Dim fd As String = fieldInfo(i).Name
o.GetType().InvokeMember(fd, Reflection.BindingFlags.SetField,
Nothing, o, New Object() {"set string value"})
Dim rtStr As String = CType(o.GetType.InvokeMember(fd,
Reflection.BindingFlags.GetField, Nothing, o, Nothing), String)
Next
End Sub


Nov 21 '05 #1
2 8899
André wrote:
I can get all my fields from my class with System.Reflection.FieldInfo but
I'm not able to getvalue or setvalue. I can not find the good method/syntax
to do so. Dim fd As String = fieldInfo(i).Name
o.GetType().InvokeMember(fd, Reflection.BindingFlags.SetField,
Nothing, o, New Object() {"set string value"})


You probably just need to make sure you are specifying the right
combination of BindingFlags using the Or operator. For setting/getting
a private member, you probably need something like this:

o.GetType().InvokeMember(fd, BindingFlags.SetField Or
BindingFlags.Instance Or BindingFlags.NonPublic, Nothing, o, New
Object() {"set string value"})

Look at the docs for InvokeMember and there is an example.

Nov 21 '05 #2
"André" <An**@discussions.microsoft.com> wrote in message
news:53**********************************@microsof t.com...
The idea is to check all uxMsgXXX variables, declare in top of my
class and assign the string value from a DB request. Why do That ?
Multilinguage apps...


So you want to initialise every /possible/ error message when you
create an instance of your web form? Strikes me as a bit overkill,
especially if nothing every goes wrong! ;-)

I would suggest providing a unique way of identifying each
language-dependent message (say, a numeric prefix) which you
hard-code into your application, then add a "Translating" class
which takes that "tagged" message and translates it, on demand,
via the database lookup, if necessary?

Just a thought ...

HTH,
Phill W.
Nov 21 '05 #3

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

Similar topics

6
by: Joanna Carter \(TeamB\) | last post by:
Hi folks I have a Generic Value Type and I want to detect when the internal value changes. /////////////////////////////// public delegate void ValueTypeValidationHandler<T>(T oldValue, T...
10
by: Abelardo Vacca | last post by:
Hi, The title sums up the question pretty much. I would like to access all private members of a class including the private members of its base classes.( I already have the ReflectionPermission )...
8
by: Floris van Haaster | last post by:
Hi All! I have a question, i have a web application and I store some member information in a variable i declared in a module like: Public some_info_variable as string in module1.vb But...
13
by: Shelby | last post by:
Hi, if I have : rs.Fields("firstname").Value = "John" rs.Fields("middlename").Value = "D" rs.Fields("lastname").Value = "Paul" Is it possible to Dim the column name and make it a variable and...
2
by: Cary | last post by:
In my project, I want to display the version number on the splash screen and in the help section of my application. I have a variable I created called appVersion. When I go to my deployment project...
11
by: David Veeneman | last post by:
I need to get a parent's private variable from within a control. Here's what I'm doing: I have a control that I want to be able to detect the presence of a particular (System.ComponentModel)...
4
by: Steve Goldman | last post by:
Even asking this question probably demonstrates that I have a fundamental misunderstanding of how values and references work in C#, but here goes: I'd like to assign a reference to an arbitrary...
11
by: Yarco | last post by:
For example: <?php class Test { private $name = 'yarco'; } $p = new ReflectionPropery('Test', 'name'); print $p->getValue();
9
by: raylopez99 | last post by:
Just an observation: pens for drawing lines in Win Forms are tricky when assignment is inside the paint handler. inside of the Paint handler, but not inside a "using" brace (that is, outside of...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.