Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Is it a bad practice to access and even set a (member) variable from the class scope inside a function, when this variable hasen't been sent as an argument to the function ?
Posted

DiiJAY wrote:
inside a function

Do you mean a method of the class?
No, it is not a bad practice.
:)
 
Share this answer
 
Your second sample
C#
private void MyMethod(int MyVariable)
{
    MyVariable = 23;
}

makes no sense, MyVariable is an input parameter to the method not a variable member of the class. Your question is not very clear as to what you are trying to achieve. Most people nowadays use properties in C# to get and set variables, but there is no reason why you should not access them within a method of the class.
 
Share this answer
 
I mean like this...


class
{

private int _variable

private void MyMethod()
{
_variable = 23;
}

}


whithout having _variable as an argument in MyMethod like...

private void MyMethod(int MyVariable)
{
MyVariable = 23;
}
 
Share this answer
 

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