Creating new variable or object everytime a method is called

Netsid

Member
Joined
Feb 10, 2020
Messages
21
Programming Experience
Beginner
Is there some neat way of creating a new variable or object every time a method is called? Im trying to think of some way i can create a new variable, actually an int, when i call a method and name that variable 1, 2, 3, and so on depending on how many times the method is called. Anybody have any good ideas?
 
As I told you on your other topic to read up on scoping in C#. This is the most basic fundamental question you've just asked. Don't look for people to spoon feed you, because the documentation exists for the sole purpose of reading it, so that you can learn from it. If everyone was to shovel code at you; you would learn nothing. That said, if you understood scoping, you wouldn't be asking this question.
 
You should probably start by understanding the difference between a variable and an object and also the difference between value types and reference types. You can't "create a new variable". Variables exist in your code where you declare them. If you have member variables declared in a class then they exist once for each instance of that class that exists. If you have local variables declared in a method then they exist as long as that method is executing. If you don't a variable declared in code somewhere then you can't magically make it exist by calling a method. If you want to have an arbitrary number of items of the same type and be able to add and/or remove items then that is exactly what a collection is for. You only need one variable to refer to one collection object and then you can add and remove as required. You can use a simple collection that acts as a dynamic array or you can have a complex collection that allows you to label each item so that you can identify it by more than just index. Any beginner tutorial will cover collections so it seems that you are trying to accomplish things without having done the appropriate learning tasks first, as @Sheepings suggests. A lot of people like to just jump in and start coding because that's the fun part, while reading documentation and tutorials is more boring. That stuff is written so that it only needs to be written once though, rather than people like us having to answer the same questions over and over again. Don't get me wrong, we are here in the first place because we like to help but there are limits. We want to help with the hard stuff that you can't do for yourself, not the easy stuff that you can be bothered learning how to do. Spend some time on tutorials and documentation to get a better grasp of the basics first and then you won't have to keep asking about the stuff that they routinely cover. There's a link to one popular tutorial in my signature below.
 
Last edited:
You nailed that explanation nicely John.

It's not that we don't want to help you @Netsid. It's the fact that you are not helping yourself and you asked a question which would have been extremely obvious to you, had you looked into object scoping and declarations and most importantly how to use them. Skipping the basics, which Is something I personally frown upon, because you are doing yourself an injustice to becoming a better programmer for yourself. We are here to teach you and help you and everyone else who comes to these forums for coding help with their worst case of coding horrors. And I know you may not see these replies as helpful, but we are helping you now by giving these types of replies; and you will thank us in the long run.

We are helping you more by getting you to educate yourself by directing you to the relevant materials, links, documentation and tutorials which will give you a much clearer understanding of how to get started with the C# language. These are the two links you need to read : Visual C# .NET for Complete Beginners - a free programming course but specifically the ones from Microsoft's Get Started - C# Guide also. Some 30 minutes into those tutorials, and you will hopefully demonstrate to us how you answered your own question. ;)

A way to look at our replies would be...
Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.
 
Back
Top Bottom