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"];
    }
  }
}
 
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:
 
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?
 
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!
 
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! ;)
 
Back
Top Bottom