?
Solved

Declaration of Global Object Reference in a Class

Posted on 2007-11-17
14
Medium Priority
?
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
14 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 1000 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 600 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

770 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