Solved

Declaration of Global Object Reference in a Class

Posted on 2007-11-17
14
235 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
DevOps Toolchain Recommendations

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

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem with SelectList in .NET MVC application 1 27
parsing JSON help 1 20
Syntax Issue with SSIS module 26 104
SSRS 2016 Rendering HTML tables 3 27
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)…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

860 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