Solved

Declaration of Global Object Reference in a Class

Posted on 2007-11-17
14
232 Views
Last Modified: 2013-12-17
I have a class named   CEmployee.cs  (Csharp).. I have lets say 5 functions in the class and  I have all the
5 functions using a Common Object  say :   CUtil  Obj=null;    /  l Obj = new CUtil();
                                                                      I have this declaration in each and every function..
Now Can i have this CUtil Obj at the top of the class and instantiate it with NEW Keyword in each and every function?.......Can a skeleton example be shown how to use it the best way
0
Comment
Question by:dotnet0824
  • 7
  • 6
14 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 250 total points
ID: 20305709
you can do something like this:

class CEmployee
{
       private CUtil obj = null;

       void function1()
       {
            obj = new CUtil();
            // use obj
       }
       void function2()
       {
            obj = new CUtil();
            // use obj
       }
       void function3()
       {
            obj = new CUtil();
            // use obj
       }
}

But doesn't look as a well designed class, can you be more specific about the CUtil class purpose?
0
 
LVL 22

Assisted Solution

by:JimBrandley
JimBrandley earned 150 total points
ID: 20305738
There are several ways you can attack this one. It depends on what you want.
1. Unless you have some reason to instantiate a new CUtil class, most utilities declare their methods a static, so you do not need to instantiate the class. That looks like:
public class CUtil
{
   public static string Method1()
   {
   }
}

The CEmployee can do something like:
string value = CUtil.Method1();

If you want only one CUtil for each instance of CEmployee, you can:
public class CEmployee
{
   private CUtil mUtility = null;
   private CUtil Utility
   {
      get
      {
         if (mUtility == null)
             mUtility = new CUtil();
         return mUtility;
      }
   }
}

Then the employee instances just reference the property.

Finally, you can do as you proposed.

Generally, Utilities expose static methods. If you do not want that, the combination of member and property is a better bet than creating a new one each time an employee method is invoked.

Jim
 
0
 

Author Comment

by:dotnet0824
ID: 20305851
Sorry its not UTIL actually .. Its a DAL Layer.   From the DAL  I have ErrorClass being called which has all the custom Errors declared. So in each and every function in DAL i have to declare it with a variable like  CError ObjError;  ObjError = new CError(); in each and every function...........and from another class I have a static method declare which gets Database Connection  
protected static Database GetDatabase()
        {
            //SqlDatabase db = DatabaseFactory.CreateDatabase("DefaultDatabase") as SqlDatabase;
            SqlDatabase db = DatabaseFactory.CreateDatabase() as SqlDatabase;

            return db;
        }

Even this Static method is being used  as    Database _db = null';  in each and every function

Finally 1 have these 2 things 1) Using Error class Instatantiona being done in each and every function in CustomerClass and Static method  called in each and every function in CustomerClass
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:dotnet0824
ID: 20305874
I have increased the points.. Should I make the ErrorClass which I use in my customerClass a Static ErrorClass so that I dont need to instantiate it in CustomerClass... The same way I did for this GetDatabase method posted earlier...............Currently the ErrorClass is assinged to a variable and again with New and in finally method its Dissociated to NULL.
0
 

Author Comment

by:dotnet0824
ID: 20305882
If a static class is instantiated in CustomerClass  there is no need to Dissociate the instance in Try catch finally right......... Is it a good practise to find the classes being used in lets say CustomerClass and make all the classes static so that we dont need to instantiate and dissociate them in CustomerClass DAL layer
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20305884
So, I don't see any reason to do different than this:

class CEmployee
{
       void function1()
       {
            CUtil obj = new CUtil();  // declare and assign in the same line, no need to assign to null first
            // use obj
       }
       // etcetera
}

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20305886
>> finally method its Dissociated to NULL.
you don't need to do that, once an object is no used, it is discarded by the garbage collector.
0
 

Author Comment

by:dotnet0824
ID: 20305904
well..... When I started debugging in Catch Block I find that after assigning new keyword to the class the object is != Nothing  so  I set it to NULL.... Nothing Wrong right....
What about my Error classes  should I declare them as Static.............. What if i declare all the classes as static (I mean those which are being Xtensively used by CustomerClass) In that way there is no need of  NEW Keywords etc ............Is that wrong way to do .........For eg : ErrorClass being used by CustomerClass
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20305919
>>>What about my Error classes  should I declare them as Static.
That's depends on how you use your class. Could you post more about it?
0
 

Author Comment

by:dotnet0824
ID: 20305942
Well that ErrorClass has ENUM declared with all possible Errors and I have a  ThrowError(ENUM Str)  with all the possible errorMessages in the function... In my CustomerClass if there a business exception I call that  ThrowError(ENUM)  in all the 5 functions in my CustomerClass DAL LAYER............So I have to instantiate that ERROR OBJECT in each and every function.............So can I make it static........
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20305959
If it is just an enum, why to use a class?
Just put the enum inside some namespace.
0
 

Author Comment

by:dotnet0824
ID: 20307039
well... I have a function too in that class right  
public  string ThrowError(Enum strError)
Case  : Error.BANK_BALANCE
 Throw new exception("")
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20307655
Well, in this case a static class will be enough and easier to access.
0
 

Author Comment

by:dotnet0824
ID: 20307940
Thanks a lot
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question