2

How do I configure a static class via Spring .NET?

Consider the following class:

static class Abc
{
   public Interface xyz
   {
       get;
       set;
   }

   public void Show()
   {
      xyz.show();
   }
}
George Stocker
  • 57,289
  • 29
  • 176
  • 237
Geeta
  • 69
  • 1
  • 8
  • Why do you need the static class? Can't you create a "normal" (non-static) class and instantiate with singleton scope? – Marijn Jul 21 '11 at 09:17
  • its utility class like tracing so it is static. – Geeta Jul 21 '11 at 09:55
  • 2
    This code won't compile, since property `xyz` and void `Show` are instance members, which cannot be declared in a static class. They should be declared static as well. – Marijn Jul 22 '11 at 08:01
  • Consider reading the following questions on the use of static classes: http://stackoverflow.com/questions/241339/when-to-use-static-classes-in-c/729805#729805 and http://stackoverflow.com/questions/576853/what-is-the-use-of-a-static-class. – Marijn Jul 22 '11 at 09:14

1 Answers1

2

Maybe a workaround can help.. This is not a static class but it works like one

namespace Nyan {
    public class Util{
        protected Util(){} //to avoid accidental instatiation

        public static string DATETIMEFORMAT = "HH:mm:ss";

        public static DateTime parseDate(string sDate)
        {
            return DateTime.ParseExact(sDate, DATETIMEFORMAT, CultureInfo.InvariantCulture);
        }
    }
}

<object id="Util" type="Nyan.Util, Nyan" singleton="true">
     <property name="DATETIMEFORMAT" value="HH-mm-ss" />
</object

and is used like any other static class:

protected void Page_Load(object sender, EventArgs e)
{
    DateTime sDate = Nyan.Util.parseDate("15-15-15"); //parsed with injected format
}
Jaguar
  • 5,929
  • 34
  • 48
  • Yes it may solve my problem partially.however here parseDate() is a static so it will work.but in my case Show() is not a static function. so how shal i make a call to it. – Geeta Jul 22 '11 at 04:16
  • yeah sorry for the mistake.i need to define xyz property and Show() method as static. – Geeta Jul 22 '11 at 12:27