r/programminghelp • • Apr 04 '19

Help passing new values to objects.

I want to be able to change the parameters of some objects that represent different animations at will. My current method accomplishes what I want but involves comparing strings, which I imagine is really slow. Is there a way to accomplish this same thing without needing to compare strings or better yet, just set the first value in the struct to the actual variable I would like to change?

Step 1: Set up struct to pass animation variables to object.

https://pastebin.com/YKPGfw7Z

Step 2: Put together array of new values and send to object.

https://pastebin.com/eEHHRMzU

Step 3: Update object values based on array of new values. An empty string signifies the end of the new value list.

https://pastebin.com/WDDBjhxy

3 Upvotes

4 comments sorted by

1

u/TreeBaron Apr 04 '19

Not sure of the context here but...

When you make a new class you can then treat the class as a variable. So tell the function you are passing it in a new variable of you object type and then pass it in.

//A class can even hold itself as variables

Class Dog

{

 string name = "max";

 int age = 4;

 Dog father;

 Dog mother;

}

int AgeOfDog(Dog max)

{

 return max.age;

}

1

u/pikipupiba Apr 04 '19

But then what if I want to programmatically change certain variable in other parts of the program?

1

u/TreeBaron Apr 04 '19

That starts to get into pass by reference vs. pass by value which would determine if a change to (using my example) max would change max in other areas. This depends on the language you are using.

Typically, changing an object in one place will change it in all places, but I would recommend testing this for yourself and looking into pass by reference, vs pass by value.

1

u/pikipupiba Apr 04 '19

Here is the relevant portion of my code I would need to optimize:

https://pastebin.com/F77bsbbB

I am changing the values of each object every time you update the pattern called ColorPulses().

So maybe there is better way to attach an Oscillator object to an object variable?