Click here to Skip to main content
15,905,414 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How does one programatically establish an instance name that has a numeric appended to it when you instantiate it?

All I see on the internet regarding using more than one instance of a given class is this kind of example:

SyncJob syncJob1 = new SyncJob();
SyncJob syncJob2 = new SyncJob();
SyncJob syncJob3 = new SyncJob();
SyncJob syncJob4 = new SyncJob();

But what if you don’t know ahead of time how many instances your app will have to create?

I was thinking about doing something like this:

Int jobNumber = 1;
String jobName = “syncJob” + jobNumber.ToString();
jobName = new SyncJob();

But jobName is a string and you can’t use a string to create an instance.
Posted

1 solution

You don't.

You should create a List<syncjob> or an array of SyncJob that stores enough slots for the sync jobs you are using. You then refer to them by index in the array or list.

C#
public class YourClass
{
    private List<SyncJob> _syncJobs = new List<SyncJob>();    //Declare as a member

    public void NewSyncJob() 
    {
        SyncJob sj = new SyncJob();
        //Initialize

        _syncJobs.Add(sj);
    }

    public void SyncJobUser()
    {
        int jobNum = 4;  //Or whatever SyncJob you want to reference, could be a parameter

        _syncJobs[jobNum].Whatever = something;
    }

    public void RemoveSyncJob(SyncJob job)
    {
        _syncJobs.Remove(job);
    }
}
 
Share this answer
 
v3
Comments
rfresh 12-Sep-13 19:56pm    
So, I'm stuck having to still pre-declare the names (syncJob1, syncJob2) like this:

SyncJob syncJob1 = new SyncJob();
SyncJob syncJob2 = new SyncJob();

ahead of time, before use?
Ron Beyer 12-Sep-13 20:39pm    
No, I updated the solution with an example, you would just call NewSyncJob each time you needed a new one. When you are finished with it, delete it from the list (using the _syncJobs.Remove(SyncJob) method). You don't need to have separate names for each one.
Sergey Alexandrovich Kryukov 12-Sep-13 20:51pm    
My 5. Actually, I currently have no idea how I would explain things to OP, as it looks like a big confusion of everything: the idea of access by name, variables and their names, pre-allocation, all about very basic programming, very far from parallel computing and other more complex things. Yet another case of trying to do something well above one's head and inability to divide work and learning into feasible steps.
—SA
Ron Beyer 12-Sep-13 20:52pm    
Thanks Sergey. With the example its probably as far as I can take it without giving a "fundamentals of programming" lesson. I recommend (to the OP) that he picks up a beginners C# book (or even online tutorial) and goes through it.
Sergey Alexandrovich Kryukov 12-Sep-13 22:38pm    
Great idea.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900