0

I want to give an object a name that's changes all the time. I have a foreach in which I set a pushpin. Each time there is being looped through the foreach, I want there to be a new name based on whats in my list (I'm looping through my list).

for example, this is my code:

Test test = new Test();
        foreach (var test in Test.allTests)
        {
            Pushpin pushpin = new Pushpin();
        }

I want to do something like this:

   Test test = new Test();
            foreach (var test in Test.allTests)
            {
                Pushpin test.testname = new Pushpin();
            }

I am unable to do this, because test.testname is a string..

user3478148
  • 433
  • 1
  • 8
  • 26

1 Answers1

1

I'm going to stick my neck out here and make some wild assumptions about what you actually want.

class Pushpin
{
    public Pushpin() { }
    // More Pushpin-related members and methods here
}
class Test
{
    public Test(string name) { Name = name; }
    public string Name { get; set; }
    // More Test-related members and methods here
}
class SO23174064
{
    Dictionary<string, Pushpin> pushpins = new Dictionary<string, Pushpin>();
    public Dictionary<string, Pushpin> CreatePushpins(IEnumerable<Test> tests)
    {
        foreach (Test test in tests)
            pushpins[test.Name] = new Pushpin();
        return pushpins;
    }
}

Then in your main program you can use things like this:

    Test[] tests = new Test[] { new Test("x"), new Test("y") };
    Dictionary<string, Pushpin> pins = new SO23174064().CreatePushpins(tests);
    // use pins["x"] etc here

Am I close?

ClickRick
  • 1,553
  • 2
  • 17
  • 37