Click here to Skip to main content
15,559,682 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi experts.. :) As a newbie in C# programming, i wanna ask u about variables and the best way in the best programming. :)

When i declare some variable in my code, i want to those variables can be used by some method in the same time. I've made those variables other classes. Here is my code :


C#
//Class Variable
class Variables 
{
   public IDictionary<int, string> accNum = new Dictionary<int, string>();
  //-- other variables...
}

class Process
{
  public void Process1()
  {
    Variables vs = new Variables();
    foreach(KeyValuePair<int, string> pair in vs.accNum)
    {
      //-- processing...
    }
  }


  public void Process2()
  {
    Variables vs = new Variables();
    foreach(KeyValuePair<int, string> pair in vs.accNum)
    {
      //-- processing...
    }
  }

}


We assume that "accNum" has some value and we need to process it. My question is, is there any other way to implement this code which is certainly more reliable, fast, and efficient in memory ? May u help me master :confused:
Posted
Updated 26-Aug-10 0:22am
v2

Your idea looks fine and something new at first glance.

However, the "Encapsulation" principle of OOP says, you should Encapsulate your variables and methods within your Classes. So, each class should encapsulate and own their own properties (Variables) and methods that the class needs to access and process.

Another principle of Software Engineering is to Reduce "Coupling". The more you reduce dependency and coupling among the components in your system, the more manageable, reusable and extensible your code is. Now, if you have an exclusive class that encapsulates the variables (Like what you've shown in your example) and if you use object of that class to access those variables in different areas in your system, you are increasing coupling in your system. Different codes will be dependent on the same variables (Kind of Global variables) and a small change in these global variables would lead several impact in the entire system.

So, it is best to keep only related variables within the class where they correspond to. That means, each class will have their own set of variables, which may or may not be accessible by outside world, depending upon context.
 
Share this answer
 
v2
Comments
Teamsar Muliadi 26-Aug-10 6:26am    
Mmmmm.. it means that i have made "coupling".. is it right..?? Do u have some best practice for this problem..?? :) Thanks
Al-Farooque Shubho 26-Aug-10 8:57am    
I would not call it "Best practice", but, some general rules. Try not to use any Global variable, put variables where the logically belong (A Person class should only contain the variables denoting person attributes, not any other class), expose only those variables via public accessors which are needed to be set by outside world, use "private" modifier to the variables which are not required by the outside world..etc.

Besides, you might also be interested to learn the Object Oriented Design principles. You might find this article interesting : http://www.codeproject.com/KB/architecture/SOLIDPrinciplesInOOD.aspx

Best of luck.
Teamsar Muliadi 26-Aug-10 22:31pm    
Thank you for your explanation Al-Farooque.. :)
You don't want to expose your class level variables as public. Remember the rule of OOP, encapsulation. Make all your variables private and provide accessors (see here[^] for msdn help)

//Class Variable
class Variables 
{
   private IDictionary<int,> accNum = new Dictionary<int,>();
  //-- other variables...

  public IDictionary<int,> AccNum
      {
        get ;
        set ;
      }
}
 
Share this answer
 
Comments
Teamsar Muliadi 26-Aug-10 6:07am    
Is this accessor consume much memory ?? :( and based on your answer, we must make our variables private, it means that, i should have made 'each class would have their own set of variables'.. right??
If it's right, i have one question for u expert.. hehehehe.. :)
The many variables that we make, the more memory consumed.. is it right..??
Yusuf 26-Aug-10 10:43am    
I have no idea what you are asking? Why are you concerned with the accessors consuming memory. If you have intrinsic data type then it should not consume, but in your case you have a dictionary. Who knows how big is your dictionary? The bottom line is you seem to have a lack of understanding of the basics of programming. May I advice you pick a decent book and start learning. I am sure it will answer a vast number of your questions.
Teamsar Muliadi 26-Aug-10 22:34pm    
Mmm.. i think so.. :( My basic in programming is not good enough... :( I will try to start learning. Thank you.
ok..I'll try to answer it from what I think you're asking about,

You want to make a method have its scope extending beyond its member variables but rather to the whole class it is contained in..

ok..if thats what you're asking then its very possible, C# has the ref keyword which you'll place before a members argument list and it will then be able to reference, the variable outside its scope..so I'll apply it on your code..

class Variables 
{   
    public IDictionary<int, string> accNum = new Dictionary<int, string>();
  //-- other variables...
}

class Process{

Variables vs;  // Just create an instance of the Variable Class

// the ref keyword will open up the scope of the method to the instantiated variable above

public void Process1(ref vs)  
{       
   foreach(KeyValuePair<int, string> pair in vs.accNum)           {     
      //-- processing...    
}  

}  
<p></p>
public void Process2(ref vs)  
{   
 foreach(KeyValuePair<int, string> pair in vs.accNum) 
   {      
      //-- processing...    
   }  
}

}
 
Share this answer
 
Comments
Toli Cuturicu 26-Aug-10 6:15am    
Reason for my vote of 2
no
Teamsar Muliadi 26-Aug-10 6:23am    
Hhmmm... i'm not sure about your opinion expert.. :( Sorry.. :(
You should have given the best way based on your best practice, not based on my problem.
I'm afraid I'll miss some of the data if I use this way, coz' my project will process much data.. so i must implement the best way... Thank you expert.. :)
Lantei 26-Aug-10 11:26am    
ok thats good..I then guess you were looking for 'a BETTER way of doing it' instead of 'trying to resolve things from YOUR practise'..I think Answer 1 and 2 then explains it better and I would just add something little to it in terms of its code usage..
In terms of variable scope, there are a lot of things to do based on the level of exposure you want to give your variables. In my practise, I usually use the following :

1. the ref keyword : when I want to give my methods a wide scope within the whole class without re-creating them within each method, since by default methods encapsulate their member variables and make it accessible only within the method.

2. properties : which will create encapsulation of a variable and give you the power to limit how your users will use the variable (either a getter scope, setter scope or both)..Eg.

int emp_name;  // private by default and therefore in accessible outside class

public int Emp_Name
{
  get
  {
   return emp_name;  //getter
  }
  set
  {
   emp_name=value;  // setter
  }
}

3. Interfaces : And this is what I sometimes love using especially when I want to limit variable access

.
.
interface _InterfaceName
{
  void getData();    
  void sendData(string _arg1, int _arg2);

  // but remember that unlike classes interfaces have no member implementations
}

class Implementer : _interfaceName
{
  public void getData()
{
 // perform implementation here
}

 public void sendData(string _arg1, string _arg2)
{
  // perform implementation here
}

// and by making your class members implicit, you create encapsulation of its data. Explicit makes its open.
}
 
Share this answer
 
Comments
Lantei 27-Aug-10 4:46am    
I think these basic code principles help me in encapsulation and variable abstraction..and in terms of memory usage, the ref keyword is better suited..when u have a large amount of data since it wont be producing multiple objects..u just have a single object that everyone refers to..

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