Solved

Declaration of Global Object Reference in a Class

Posted on 2007-11-17
14
223 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now