Autoincrement Class Objects

Hello, farely new to Javascripting and this forum, so bare with me… :slight_smile:

I’m playing around with javascript, al l from the console point of view, so a lot of System.out.println()'s in my code. Now I’m trying to create some sort of log so i have created a class called ‘activiteitenRegel’, now I’m trying to create objects based on this class, but I would like the objects name to auto increment. So the first time i create an object of this class, i get ‘log: 0’, the second time ‘log: 1’, etc. etc.

        String logRegel = "log:" + actRegel++;
        activiteitenRegel logRegel = new activiteitenRegel();
            logRegel.teamNr = sessieTeam;
            logRegel.inOUt = "IN";
            logRegel.postNr = postNr;
            logRegel.datum = new Date();

Please see my code above, the error I receive is “Variable logRegel is already defined in scope”.
Does anyone know if it is possible what I want?



At a glance: You declare logRegel twice.

First here as the Type: String

String logRegel = "log:" + actRegel++;

Then here as the Type: activiteitenRegel (which is an object)

activiteitenRegel logRegel = new activiteitenRegel();

Those should probably be two separate vars. Maybe this would work:

        String logRegel = "log:" + actRegel++;
        activiteitenRegel regel = new activiteitenRegel();
            regel.teamNr = sessieTeam;
            regel.inOUt = "IN";
            regel.postNr = postNr;
            regel.datum = new Date();

Also, if this is JUST JavaScript and not some abstraction of JavaScript such as Typescript may I suggest:

var logRegel = "log:" + actRegel++;
var regel = new activiteitenRegel();
regel.teamNr = sessieTeam;
regel.inOUt = "IN";
regel.postNr = postNr;
regel.datum = new Date();

Based off your reference to System.out.println(), I believe you mean Java, which is very different from JavaScript.

Hope this helps

Hi Shadowfox476,

Thanks for your quick reply, being new to this, i always use java and javascript crosswise, my excuses.
You are correct about the double declaration but first I need to assign the currnet numner + 1 to the Logregel and then use this value in the declaration of the object.

step wise:
Create name for object = log + autoincrement nr.
Use name for declaration of object.

In your suggestion, the variable ‘regel’ is not auto incremented value, but always the same?

I am not seeing where you would use the name in the code?

I only see you intializing it:

String logRegel = "log:" + actRegel++;

Where is this used? You would have to update it later:

Somehwere else in your code. This would update the value to the next increment. Notice there is not a String declaration. Its already been declared and does not and can not be declared a second time. :

logRegel = "log:" + actRegel++;