0

Can I create a virtually infinite number of instances of a class? Or can you tell me a better way to do this?

Here's my problem, I want to make a program that lets you create circles by clicking on the screen, they have to be displayed and to interact with one another. I'm pretty newbie, so I divided my project in some phases:

1 Display the form
2 Display circles on click with diameter and color chosen by the user
3 Make a separate class for single circles
4 Make them move
5 Make them interact with one another
6 Check for bugs etc.

I'm stuck between point 2 and 3. I'm not sure that making a Circle class and creating various instances is the smartest way to do this, but it's the only way I came up with that gave me the possibility to make infinite circles displayed. I could use an array, since I think no one will ever use more than 20 circles at a time. What's more, I don't even think I'll ever publish this, since I'm making this just to learn something of graphics. so, sooner or later, I'll need help with point 4.

jsirr13
  • 944
  • 2
  • 12
  • 38
  • 1
    20 != infinite. Infinite = System.OutOfMemoryException, but 20 (or some other arbitrary limit you decide to impose) will be fine. – James Thorpe Sep 24 '15 at 16:29
  • there no infinite in coding, no way to achive that. but you can create a really big number of instances. And yes a circle class is the way to go. Infinite is a concept, you cant have it. – bto.rdz Sep 24 '15 at 16:30
  • Creating a separate class Circle is perfectly fine. And don't worry about memory. If you your program don't need to work on 16Mb RAM its almost infinite for your task (as far as I understand it) – Szer Sep 24 '15 at 16:31
  • One cannot make the infinite out of the finite and a computer and its architecture is most certainly finite. I think you are asking if it is possible to make a list of something w/o knowing how long it will go. You need the List class to do this. It is a list of classes and the list is of unknown length. – Rabbit Guy Sep 24 '15 at 16:33
  • Drawing, moving and interacting with circles. Those are very specific problems found in games. I sugest you to also ask in http://gamedev.stackexchange.com/ – jean Sep 24 '15 at 16:34

2 Answers2

2

I think you are looking for List. Its like an array in that it can contain items, but items can be added and removed at any point. So it can contain both 1 and 10000 items. Only limit is memory.

Euphoric
  • 12,645
  • 1
  • 30
  • 44
  • I was about to answer this very thing. The OP is not looking for a way to create an "infinite" number of instances, but rather an open ended number of instances - ever changing. That is a good place to use a List. –  Sep 24 '15 at 16:32
  • Thanks, that's exactly what I needed! – Oxen Eknat Sep 26 '15 at 14:54
0

In programming you are always bound by resources and you should code with your machine's limitations in mind. You will never be able to create an infinite number of objects. If you think you really need massive amounts of resources, either your approach is wrong or your problem simply cannot be solved in an efficient manner. In both cases you should probably change your approach and take your constraints into account.

Now back to your question. In drawing problems you may benefit immensely from the use of a Stack<T>. Why? Because you can then undo your actions very easily by popping from the stack (and redo them by popping from another stack that you create specifically for the redo operation).

So the idea is that you push your actions onto the stack. This will be like a history of actions. You can have an IAction interface with do and undo methods and implement it in a few subclasses: Create (used for creating circles), Move (for moving circles) etc. Whenever you want to make an action, create an instance of the relevant class, call do and push the instance onto the stack. When you want to undo, pop and call undo on the object you've just popped.

I'm not sure that making a Circle class and creating various instances is the smartest way to do this

I this the OOP way and your intuition was correct. Create a new instance for each circle and use it to hold the state of your circle, such as its position, its colour and what now.

Sure, you can store them in a list, but remember your list will never be infinite. You may want to set a limit to the number of circles someone can draw, but since this number is probably really big, I wouldn't bother.

async
  • 1,537
  • 11
  • 28