If so how?
Say you have a class like: -
public class MyClass
-
{
-
public double[] DataSet1;
-
public double[] DataSet2;
-
}
-
and i have a string called MyString that is "DataSet2".
Is it possible to somehow reference MyClass.DataSet2 dynamically by doing something like:
double[] MyOutput = MyClass.[MyString];
Thanks,
Jon
9 9658
Other then using a Dictionary or System.Reflection I'm not sure you can.
Should i be using some kind of string indexer instead?
Look at the NameValueCollection object, it should allow you supply a string for a name and store it with an object (any type of object)
Look at the NameValueCollection object, it should allow you supply a string for a name and store it with an object (any type of object)
I don't know how you guys are communicating here but I don't even understand the first post. Is MyString a variable in MyClass which is static?
I don't know how you guys are communicating here but I don't even understand the first post. Is MyString a variable in MyClass which is static?
We operate entirely telepathically ;)
The easiest way I can see of doing this is by using the Properties collection. Create a reference to the properties collection and add your properties into that. This way you can reference:
Msgbox(CType(Me.Properties("MyPropertyName"), Double)
Which I think in C# would be something like: - PropertyCollection MyProperties = new PropertyCollection();
-
private void Form1_Load(object sender, EventArgs e){
-
MyProperties.Add("MyProperty1", null);
-
MyProperties.Add("MyProperty2", null);
-
MyProperties["MyProperty1"] = "Hello World";
-
MessageBox.Show((string)MyProperties["MyProperty1"]);
-
}
I'm not sure if this is achievable at design-time though...the properties aren't normally added to a properties collection unless you specify that in the constructor of your class. This adds to code maintenance - if you add a property to your class, you have to remember to add it to the properties collection which should be read only so that other classes can't change it...so basically instead of just adding properties into your class in one place, you now need to remember to add them in two places...so yeah. There are lots of ways to achieve this - but I think this is likely the easiest...and if you look at the constructor of a form once you're done designing it, you'll see that it's the same kind of method used by microsoft with your controls collection using the IContainer. The plumbing is hidden away in the Form class that you inherit from, but it exposes a Controls collection that gives you the list of controls.
I think you could equally use the IContainer instead of the PropertyCollection - the effect is the same.
It would be nice if the OP were to describe what they want to achieve.
I smell a simple design problem here.
Hi guys,
I was just Googling around for a similar topic, not sure if anyone is still subscribed to this thread but I thought I'd just post this little gem I just found by searching for "dynamic properties class C#".
I will be making extensive use of this code in a desktop application I'm currently building which needs a base class and methods for serialising various objects and their properties into XML documents to be submitted to a web service so thought I'd share my tuppence (plus I'm then subscribed to this thread so I don't lose the link!).
codegecko
You know, I think that the original question isn't about creating dynamic properties, but more along the lines of how to access the properties dynamically. It seems pretty straight forward that reflection is the key here.
For example (imports/using the System.Reflection namespace) -
Dim myString As String = "DataSet2"
-
Dim result() As Double = Nothing
-
-
Dim mc As New MyClass
-
Dim mcProperties() As PropertyInfo = GetType(MyClass).GetProperties
-
-
Dim dataSet2Property As PropertyInfo = Array.Find(mcProperties, Function(x As PropertyInfo) String.Compare(x.Name, myString, True) = 0)
-
-
If dataSet2Property IsNot Nothing
-
result = dataSet2Property.GetValue(mc, BindingFlags.GetField, Nothing, Nothing, Nothing)
-
End If
-
-Frinny
@codegecko
That was a very complex way of just using a Dictionary. WAY more work then you needed to do. And doesn't address the original issue.
And Frinny, you would post VBNET for a C# answer, lol
C#:(with less work :-P) -
string myString="DataSet2";
-
double result=null;
-
-
MyClass mc= new MyClass();
-
System.Reflection.PropertyInfo dataSet2Property =MyClass.GetType().GetProperty(myString);
-
if (dataSet2Property != null)
-
{
-
result = dataSet2Property.GetValue(mc, System.Reflection.BindingFlags.GetField, null, null, null);
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Simon |
last post by:
Am using the following code.
<script language="JavaScript1.2">
function setquantity(productindex,productquantity)
{
//create the reference object
irefname_none = eval("document." +...
|
by: jullag |
last post by:
Hi,
does anyone know of any javascript method that does the same job as
document.write(), but not necessarily at the end of the document? For
instance, insert some text inside an element that...
|
by: Nathan Bloomfield |
last post by:
Does anyone know if there is any documentation which relates to Access2k + ?
or can anyone help adjust the code?
I am having trouble converting the DAO references.
TITLE :INF: How to...
|
by: CAD Fiend |
last post by:
Hello,
Well, after an initial review of my database by my client, they have
completely changed their minds about how they want their form. As a
result, I'm having to re-think the whole process....
|
by: MikeY |
last post by:
Hi Everyone,
I am working in C#, windows forms.My question is this. All my button dynamic
controls properties are present and accounted for except for the"FlatStyle"
properties. I can't seem to...
|
by: Ron |
last post by:
I need to write a custom textbox control that references an object located
in the Global.asax class, but I can't compile the control into a DLL without
including the reference to the project DLL...
|
by: Jerry Spence1 |
last post by:
I am trying to create timers on demand by doing the following:
Dim NewTimer As New System.Timers.Timer
NewTimer.Interval = 10000
AddHandler NewTimer.Elapsed, AddressOf Timeup
NewTimer.Enabled =...
|
by: YYZ |
last post by:
In my program, a user can open up many different "Loans" -- each one is
loaded into a dynamically created usercontrol (ucLoan) -- in order for
them to be able to switch between the ones they have...
|
by: Javilen |
last post by:
Hello,
Maybe one of you can offer a possible solution to the problem I have below.
The request to me is to build a dynamically populating dropdown list from a text box.(something similar to...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |