Adding to the correct property at runtime...please help...

A

almurph

Hi,

I have an object that has a number of properties. I have to add a
variable to the correct property at runtime (not compile time).
Problem is I do not know which one to add it to (if you see what I
mean). How do I do this at runtime. My system will return the name of
the property at runtime as follws:


propName = Some process that generates the at runtime;
objectName.propName = "some variable generated at runtime";


How do I do this?
Thanks,
Al.
 
J

Jeff Johnson

I have an object that has a number of properties. I have to add a
variable to the correct property at runtime (not compile time).
Problem is I do not know which one to add it to (if you see what I
mean). How do I do this at runtime. My system will return the name of
the property at runtime as follws:


propName = Some process that generates the at runtime;
objectName.propName = "some variable generated at runtime";

I have no idea what you're trying to do. Could you give a different, more
real-world example? Why do you even think you need to "add a variable to a
property" in the first place?
 
P

Peter Duniho

Hi,

I have an object that has a number of properties. I have to add a
variable to the correct property at runtime (not compile time).
Problem is I do not know which one to add it to (if you see what I
mean). How do I do this at runtime. My system will return the name of
the property at runtime as follws:


propName = Some process that generates the at runtime;
objectName.propName = "some variable generated at runtime";

I agree with Jeff, your question is hard to understand. The phrase "add
a variable" doesn't make much sense, given your pseudo-code example.
Nor does "variable generated at runtime" in any case. But, let's see if
we can figure it out.

Perhaps you simply mean "assign a value to" instead, where you have
something like this:

class TestClass
{
public string TestProperty { get; set; }
}

Then some code like this:

TestClass test = new TestClass();
string strProperty = "TestProperty";
string strNewValue = "some new value to assign to the property";

test.«something to resolve strProperty as an actual property here»
= strNewValue;

With the end result that the "TestProperty" property of the "test"
instance now contains the value "some new value to assign to the property".

Is that correct?

If so, then the simple answer is that you use reflection (see
Type.GetProperty() and PropertyInfo.SetValue() methods). But in
reality, that's a very poor, hard-to-maintain, inefficient way to manage
your data structure.

More typically, if you have this need for dynamic maintenance of values
by some name, you'd use a dictionary to store the values. IMHO that's a
much better choice. But if you can describe more precisely what you're
trying to do and why you think that having a named property that you
access dynamically is an important feature to implement, it's possible
someone could provide a more specific, useful suggestion.

And if my interpretation isn't correct, you really need to take more
time and write your question so that it's clear and unambiguous.

Pete
 
F

Family Tree Mike

Jeff Johnson said:
I have no idea what you're trying to do. Could you give a different, more
real-world example? Why do you even think you need to "add a variable to a
property" in the first place?

It's possible the OP is describing reflection and PropertyInfo.SetValue(),
but it is very confusing to understand why they are doing this.

Mike
 
F

Family Tree Mike

Peter said:
I agree with Jeff, your question is hard to understand. The phrase "add
a variable" doesn't make much sense, given your pseudo-code example. Nor
does "variable generated at runtime" in any case. But, let's see if we
can figure it out.

Perhaps you simply mean "assign a value to" instead, where you have
something like this:

class TestClass
{
public string TestProperty { get; set; }
}

Then some code like this:

TestClass test = new TestClass();
string strProperty = "TestProperty";
string strNewValue = "some new value to assign to the property";

test.«something to resolve strProperty as an actual property here» =
strNewValue;

With the end result that the "TestProperty" property of the "test"
instance now contains the value "some new value to assign to the property".

Is that correct?

If so, then the simple answer is that you use reflection (see
Type.GetProperty() and PropertyInfo.SetValue() methods). But in
reality, that's a very poor, hard-to-maintain, inefficient way to manage
your data structure.

More typically, if you have this need for dynamic maintenance of values
by some name, you'd use a dictionary to store the values. IMHO that's a
much better choice. But if you can describe more precisely what you're
trying to do and why you think that having a named property that you
access dynamically is an important feature to implement, it's possible
someone could provide a more specific, useful suggestion.

And if my interpretation isn't correct, you really need to take more
time and write your question so that it's clear and unambiguous.

Pete

That was my take on it, but MS seems to be acting up again. I
didn't/don't see your reply online (www.microsoft.com), but I see it in
my reader at home. In fact, I don't see some of your replies to other
posts there today.
 
P

Peter Duniho

Family said:
That was my take on it, but MS seems to be acting up again. I
didn't/don't see your reply online (www.microsoft.com), but I see it in
my reader at home. In fact, I don't see some of your replies to other
posts there today.

Hmm...well, that's odd. Since "the troubles" with the MS Communities
portal, I just gave up and switched over to Microsoft's own news servers
for posting. I do see that my post in this thread is visible on the MS
web site, so at the very least, it's propagating within their own
servers correctly.

The last time this came up, there didn't appear to be any problem
getting posts _out_ from Microsoft's servers to the rest of Usenet. But
maybe they have a whole new problem. On the other hand, that doesn't
seem to be what you're describing, so I don't really know.

Sure would be nice if it'd "just work". It's possible you're just
seeing some random, temporary glitch, but of course since there are
known problems with Microsoft's newsgroup implementation, the first
thing that comes to mind is some systematic bug.

One thing about your description puzzles me though: you say "I see it in
my reader at home". Does that mean that when you view this thread on
the MS Communities web site, you don't see my previous post? As I
mentioned, it shows up fine for me. So if you and I are both looking at
the same web site and don't see the same posts, that would be really weird.

Pete
 
F

Family Tree Mike

Peter said:
Hmm...well, that's odd. Since "the troubles" with the MS Communities
portal, I just gave up and switched over to Microsoft's own news servers
for posting. I do see that my post in this thread is visible on the MS
web site, so at the very least, it's propagating within their own
servers correctly.

The last time this came up, there didn't appear to be any problem
getting posts _out_ from Microsoft's servers to the rest of Usenet. But
maybe they have a whole new problem. On the other hand, that doesn't
seem to be what you're describing, so I don't really know.

Sure would be nice if it'd "just work". It's possible you're just
seeing some random, temporary glitch, but of course since there are
known problems with Microsoft's newsgroup implementation, the first
thing that comes to mind is some systematic bug.

One thing about your description puzzles me though: you say "I see it in
my reader at home". Does that mean that when you view this thread on
the MS Communities web site, you don't see my previous post? As I
mentioned, it shows up fine for me. So if you and I are both looking at
the same web site and don't see the same posts, that would be really weird.

Pete

When I posted my note, I did not see your first post, but now I do (at
http://www.microsoft.com/communitie....public.dotnet.languages.csharp&lang=en&cr=US).
I do not currently see your second post (the one I am replying to),
nor my reply to your first post.

Maybe the is working its way into some strange windows 7 advertisement :).

I think you said once before, that if you are expecting some posts, look
multiple places.
 
P

Peter Duniho

Family said:
When I posted my note, I did not see your first post, but now I do (at
http://www.microsoft.com/communitie....public.dotnet.languages.csharp&lang=en&cr=US).
I do not currently see your second post (the one I am replying to), nor
my reply to your first post. [...]

Could be that the web gateway is just slow.

Even between NNTP servers, newsgroup posts can sometimes take awhile to
show up. It's not unheard for someone to see at their own news server a
reply to a post that hasn't shown up on that server yet.

It wouldn't surprise me if newsgroup posts get out to the broader NNTP
network of news servers faster than they propagate into their own web site.

Pete
 

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