Parameters
-
If one or more private methods of a class need a variable to perform some operation on, should one methods pass it to the other as parameters or should we make the parameter a private member of the class so both methods can access it? If we make it a class level variable then it will exist for the lifetime of the object. If we do not need it for the lifetime of the object, we are wasting resources. If we do not make it a class level variable then we have to pass it around to methods which need it. I am not sure what the disadvantage is here. If the class has 7 methods and 5 of them need the variable then we will pass it around to all these methods. If class level, then we will not need to pass it but it will exist for the lifetime of the object. Should it depend on how many different methods need the variable or should it depend on whether we need its existence for the lifetime of the object? What are your opinions? Any conventions?
CodingYoshi Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07
-
If one or more private methods of a class need a variable to perform some operation on, should one methods pass it to the other as parameters or should we make the parameter a private member of the class so both methods can access it? If we make it a class level variable then it will exist for the lifetime of the object. If we do not need it for the lifetime of the object, we are wasting resources. If we do not make it a class level variable then we have to pass it around to methods which need it. I am not sure what the disadvantage is here. If the class has 7 methods and 5 of them need the variable then we will pass it around to all these methods. If class level, then we will not need to pass it but it will exist for the lifetime of the object. Should it depend on how many different methods need the variable or should it depend on whether we need its existence for the lifetime of the object? What are your opinions? Any conventions?
CodingYoshi Visual Basic is for basic people, C# is for sharp people. Farid Tarin '07
Hi, it depends on the circumstances. If the parameters are like characteristics of the objects, then data members would be alright. If they are small, then having storage allocated for them for the lifetime of the object probably is irrelevant. If they more act like operational parameters, seems to me method parameters are in order; if they are many, you may want to create a separate struct or class to hold them and pass them around easily (kind of "job ticket"). :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.