views:

34

answers:

4

Is there any way to be able to create multiple class objects based on how many the user wants created? I was able to do this but then that instance is only available under the method that created it

    public void Create()
    {
        //create class objects here
    }

and now I wouldn't be able to use it in another method for example

    public void Use()
    {  
        //can't use class objects
    }
+1  A: 

Without stopping to ask you about your design decisions, here is a straight answer to demonstrate the simple technique of using a field:

public class MyMainClass {
    private List<MyOtherClass> _instances = new List<MyOtherClass>();

    private void Create(int number) {
        for (int i = 0; i < number; i++) {
            this._instances.Add(new MyOtherClass());
        }
    }

    private void Use() {
        // use the first instance I created
        MyOtherClass other = this._instances[0];

        other.DoSomething();
    }
    ...
}

The _instances field is scoped to the MyMainClass. This means it is available to all instance methods (non-static) on the class to which it belongs.

As to why you're trying to do this, I'll leave up to someone else.

Update: an alternative techique is demonstrated by Hemant in another answer whereby the Create method returns instances to the caller. However I decided to stick with fields to demonstrate a more fundamental technique.

x0n
+1  A: 

Try this:

public MyObject[] Create()
{
    //create class objects here and return them
    return new[] {new MyObject(), new MyObject()};
}

//later
public void Use()
{  
    MyObject[] objs = Create();
    //use your objects as you like
}
Hemant
A: 

I think you mean instance objects of a particular class. See here for an introduction to classes and instances.

but in essence:

public class SomeClass{
   int someData;
   void SetData(int data) {
     someData = data;
   }
   int GetData() {
      return data;
   }
}

// create some instances of the 'SomeClass'

var instance1 = new SomeClass(); var instance2 = new SomeClass();

instance1.SetData(2); isntance2.SetData(3);

Preet Sangha
A: 

In Create() you are creating a class but, as you say, not able to us it outside of the class. The first thing you need to do is to change it to return the object:

public SomeType Create()
{
  //code that creates the object here.
  //Lets say it gets put into a variable called ret
  return ret;
}
public void Use()
{
  SomeType st = Create();
  //use st here.
}

Alternatively, instead of the method that uses the object calling Create, the method that creates the object could pass it as a parameter to Use(SomeType st).

To do this with multiple objects, then we need to return not a SomeType, but an object that contains those multiple SomeType objects. Sometimes we need certain properties and methods about that entire collection (e.g. the count, the ability to move back and forward through the collection and so on). In this case we would return an array, a List<SomeType>, a HashSet<SomeType> or perhaps something like Dictionary<string, SomeType> as appropriate.

If we don't need such operations on the collection as a whole then it is simpler and more efficient to return an IEnumerable<SomeType> which is an object that allows one to work through a sequence of such objects but not much else. C# has a syntax precisely for doing this. Say we're going to return 10 such objects:

public IEnumerable<SomeType> Create()
{
  for(int i = 0; i != 10; ++i)
  {
    //code that creates the object here.
    //Lets say it gets put into a variable called ret
    yield return ret;
  }
}
public void Use()
{
  foreach(SomeType st in Create())
  {
    //use st here.
  }
}

The yield keyword nicely hides a lot of complexity about IEnumerable<SomeType> and lets you just write the code that passes out each new object (that complexity is still there in the produced code, but not your problem to worry about).

Jon Hanna