c# shared utility functions

Soldato
Joined
12 Jan 2004
Posts
6,824
Location
Londinium
Man, this c# asp.net stuff is not getting easier! Simple things in classic become so much more diffcult in asp.net!

Anyways, Im trying to write a simple utility class that will hold all of my common application functions. This class will be imported (or whatever) into all of my aspx files so they can make use of the functions. Also, I want the functions of this class to be 'shared', so I do not have to instantiate the class to use its functions.

Now heres the thing, Im using asp.net 1.1 WITHOUT visual studio! Thats right, im using textpad! Certainly makes things easier...!

So, what I need is someone to write me a simple example of a .cs file for the class, and instructions of how to compile it for use in aspx pages, and whether I then need to add a command in the aspx page to use it!

Wow, all of this could have been done in classic using a simple include command! Hell, that's progress for you! ;)

Any help would be greatly appreciated.
 
robmiller said:
Did you just do

Code:
public class Utility {
    static public Foo(int foo);
}

?

This is what it looks like at the mo:

Code:
namespace ComCalc
{
  using System;
  using System.Configuration;

  // The Utility class holds all utility functions for the comcalc application
  public class Utility
  {
    // ---------
    // VARIABLES
    // ---------




    // ---------
    // FUNCTIONS
    // ---------


    // Retrieves the environment name from the Web.config file
    public static string GetEnvironment()
    {
      return ConfigurationSettings.AppSettings["Environment"];
    }


    // Retrieves the version number from the Web.config file
    public static string GetVersion()
    {
      return ConfigurationSettings.AppSettings["Version"];
    }
  }
}
 
if you make it a static class you wan't have to make an instance of it each time you want to use a method / property.

Every project I've ever worked on has a static class called utility or handy_stuff etc.

;)

Paul
 
happytechie said:
if you make it a static class you wan't have to make an instance of it each time you want to use a method / property.

Every project I've ever worked on has a static class called utility or handy_stuff etc.

;)

Paul

It is static and I dont have to make an instance of it :confused:
 
There can be issues in using static methods if two different threads access the same method at the same time. Just to let you know :)
 
growse said:
There can be issues in using static methods if two different threads access the same method at the same time. Just to let you know :)

So is it better to instantialise utility classes?
 
nero120 said:
Now heres the thing, Im using asp.net 1.1 WITHOUT visual studio! Thats right, im using textpad! Certainly makes things easier...!
How on Earth can that make things easier? :confused: You're rejecting what is arguably one of the best IDEs out there.

nero120 said:
So is it better to instantialise utility classes?
Not necessarily, as long as you write your code in a thread-safe manner.
 
Last edited:
growse said:
There can be issues in using static methods if two different threads access the same method at the same time. Just to let you know :)
That's not just static methods, that can be any method. In both cases it totally depends what the method is doing. I.e. if it isn't thread-safe then *boooom*.
 
Inquisitor said:
How on Earth can that make things easier? :confused: You're rejecting what is arguably one of the best IDEs out there.

And ending up with problems like this...

Seriously, download Visual C# 2005 Express Edition and Visual Web Developer 2005 Express Edition and learn the underlying things once you've got a handle on the high level stuff.
 
Inquisitor said:
How on Earth can that make things easier? :confused: You're rejecting what is arguably one of the best IDEs out there.

Sarcasm my dear! My work place (supposedly a world leading financial regulator) deems development tools a low priority. I put the request in for visual studio weeks ago. Not having it has made my life 10 times more difficult!

Not necessarily, as long as you write your code in a thread-safe manner.

Could you give me an example of that in this case?

Would it be wrong to simply say:

Code:
String EnvStr = Utility.GetEnvironment;
 
robmiller said:
And ending up with problems like this...

Seriously, download Visual C# 2005 Express Edition and Visual Web Developer 2005 Express Edition and learn the underlying things once you've got a handle on the high level stuff.

Exactly! As I said in my last post, my work is too crap to give developers decent tools (i.e. not textpad), and we are also using ASP.NET 1.1. :rolleyes:

Edit: By the way, I solved that little problem! It was to do with my compile syntax!
 
nero120 said:
Sarcasm my dear! My work place (supposedly a world leading financial regulator) deems development tools a low priority. I put the request in for visual studio weeks ago. Not having it has made my life 10 times more difficult!
Ah, sorry :o In that case, I feel your pain, I really do.

As for thread-safety, it's just a frame of mind that you should program in. The majority of code will run without problem in a multi-threaded environment, but in some cases it won't, so you should always keep this in mind, and think about how the code will run in such an environment.

A classic example of thread-unsafe code is a simple counter:
Code:
public class SomeClass
{
    private static int m_Counter = 0;

    public int DoSomething()
    {
        int counter = m_Counter;

        // Do some time consuming operation here...

        m_Counter = counter + 1;
    }
}
This works fine if there's only one thread trying to use the method, but if you have more than one trying to call it at the same time, they interfere with eachother. For example, if one thread gets the m_Counter variable and stores it in counter, then another does the same, when they come to update it at the end of the method, they'll both update it to the same number.

This is a very trivial example, but it demonstrates how you have to be careful when multithreading is an issue, which is especially relevant in a stateless, request based web environment.
 
Last edited:
Inquisitor said:
This is a very trivial example, but it demonstrates how you have to be careful when multithreading is an issue, which is especially relevant in a stateless, request based web environment.

Thanks for this mate, I'll watch out! ;)
 
Goksly said:
is there a keyword to make the function thread safe? like in java - 'synchronized'?
No, synchronisation must be handled manually. Gives a much finer degree of control over how the code behaves. There's a lock construct, which allows you to synchronise a block of code by making any thread that tries to enter it have to attempt to obtain a lock on the specified object. If it can't then the thread will block until it can. There are a load of other thread synchronisation tools as well, but this is the most useful. :)
 
nero120 said:
Thanks for this mate, I'll watch out! ;)
No problems! Be aware, however, that thread-safety is very much situation specific, and problems arising from multi-threading can be very obscure and difficult to debug, which is why C# offers no all-in-one synchronisation keyword for methods. There are simple measures that can be taken to avoid the problem in the example I posted, such as the lock construct, as I mentioned above, although these methods introduce their own problems, such as deadlocks.

There's a very extensive tutorial on multithreading in .NET here, which would make good reading:
http://www.yoda.arachsys.com/csharp/threads/
 
Back
Top Bottom