0

hi how to I do the below?

int z = 1;
string one = "pc";
string two = z.ToString();
                    //what goes here
Console.Write("Host One:\tSent-{0}\tSuccess-{1}\tFail-{2}", xxxxx.numepings, pc1.numepings_s, pc1.numepings_f);
Console.WriteLine();

so in my code pc1 is an instance of an object, what can i use in the line //what goes here and then in place of the xxxxx so that I can call the instance from the concanatated string variable pc and int 1 ??

2
  • "pc1" is an instance of an object - No, instances do not have names. pc1 is a reference variable that happens to point to a particular instance. Commented Mar 26, 2012 at 15:06
  • I'm sorry, I totally don't understand your question. What are you trying to achieve here, and why? What have the variables z, one and two got to do with anything? Commented Mar 26, 2012 at 15:07

2 Answers 2

5

Basically, you don't. You can potentially use reflection, but it's a bad idea. Instead, you should use a collection whenever you want to store references to multiple objects and address them by some sort of key (whether that's an index, a name, whatever).

So instead of having:

Foo pc0;
Foo pc1;
Foo pc2;
...

You would have:

List<Foo> pcs;
...
Foo pc = pcs[z]; 
Sign up to request clarification or add additional context in comments.

5 Comments

mmmm I remember some thing like this in VBA, but that was a while ago, need to go take a look.
And if you want to access the objects by a string label/tag instead of an index, use a Dictionary instead of a List.
@DevilWAH: Yes, you may well be able to do it from VBA - that doesn't mean you should, or that you can use the same idiom in C#.
I mean the method of using a list to hold the refrences :) I jsut forgot I had done it before
@DevilWAH: Ah, I see. Yes, I'd certainly expect that to be possible in VBA too :)
0

See here: http://msdn.microsoft.com/en-us/library/1fce0hc8.aspx

// Create an instance of the SomeType class that is defined in this 
// assembly.
var oh = Activator.CreateInstanceFrom(Assembly.GetEntryAssembly().CodeBase, one + z /* as a  full type name */);

// Call an instance method defined by the SomeType type using this object.
dynamic st = oh.Unwrap();

st.DoSomething(5);

You can do something like this to create an object, from it's string name - but you can't cast it to a particular type without issues.

You can cheat, and use dynamic though.

Edit: sorry, this wasn't quite right originally - and I have fixed my example.

3 Comments

While this answers the literal question posted, I think this is bad advice to give.
I see that this might not be the best answer, given Jons reply - but it is still an answer to the question asked, explicitly.
Hah - agreed. :) - But it may come in handy for someone searching for an answer to a similar issue, that can't benefit from the above advice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.