Solved

C#.NET static classes

Posted on 2015-01-26
5
188 Views
Last Modified: 2015-06-16
How do I convert the below classes into static classes. The purpose of the classes is to serve as a key & string lookup. I have not worked too much with static classes/methods/constructors, but from my readings and research understand them as follows:

Static constructors are called before any instance constructors are called. Also there can only be one static class whereas there could be multiple instances of a regular class. Having said that, I'm having trouble converting the below classes into static classes.

The reason I'm having a difficult time is that the static class implements a list of NON-STATIC objects. From my understanding, a static class contains only static members.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Gateways.AuthorizeNet
{
    public class AuthorizeNetGatewayResponseInfo
    {
        public List<ResponseInfo> ResponseInfoList = null;

        public AuthorizeNetGatewayResponseInfo()
        {
            ResponseInfoList = new List<ResponseInfo>()
            {
                { new ResponseInfo(1, "This transaction has been approved.") },
                { new ResponseInfo(2, "This transaction has been declined.") },
                { new ResponseInfo(3, "This transaction has been declined.") }

            };
        }

        public ResponseInfo GetResponseInfoByResponseCode(int resonCode)
        {
            return ResponseInfoList.FirstOrDefault(responseInfo => responseInfo.ReasonCode == resonCode);
        }
    }

    public class ResponseInfo
    {
        public int ReasonCode;
        public string ReasonText;

        public ResponseInfo(int reasonCode, string reasonText)
        {
            ReasonCode = reasonCode;
            ReasonText = reasonText;
        }

        public string GetReasonText
        {
            get
            {
                return ReasonText;
            }
        }
        
    }
}

Open in new window

0
Comment
Question by:pzozulka
5 Comments
 
LVL 11

Accepted Solution

by:
louisfr earned 500 total points
ID: 40570867
A static class can only have static members.
The constructor must either be changed into a static constructor or removed entirely, using an initializer instead of a constructor.
public static class AuthorizeNetGatewayResponseInfo
{
    public static List<ResponseInfo> ResponseInfoList = new List<ResponseInfo>()
        {
            { new ResponseInfo(1, "This transaction has been approved.") },
            { new ResponseInfo(2, "This transaction has been declined.") },
            { new ResponseInfo(3, "This transaction has been declined.") }

        };

    public static ResponseInfo GetResponseInfoByResponseCode(int resonCode)
    {
        return ResponseInfoList.FirstOrDefault(responseInfo => responseInfo.ReasonCode == resonCode);
    }
}

Open in new window

0
 
LVL 8

Author Comment

by:pzozulka
ID: 40570881
But what about the ResponseInfo class? Should it not be changed to static as well since the AuthorizeNetGatewayResponseInfo class can only have static members?

In other words, how can a static constructor implement instance members.
0
 
LVL 11

Assisted Solution

by:louisfr
louisfr earned 500 total points
ID: 40570900
ResponseInfo is not a member of AuthorizeNetGatewayResponseInfo.
AuthorizeNetGatewayResponseInfo only has three members: the ResponseInfoList field, the constructor, and the GetResponseInfoByResponseCode method.
A static method can use any class, static or not.
And a static class cannot be instanciated, so you cannot have a List containing instances of a static class, or a method returning an instance of a static class.
If you need instances of a class, you cannot make it static.
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 40571162
I just wonder why do you want to convert your classes to static ones? Static classes have some drawbacks, e.g.,:
- can’t use interfaces
- can’t use inheritance
- hard to test with mocking systems.

So, may be you need a "singleton"?
0
 
LVL 2

Expert Comment

by:brgdotnet
ID: 40833761
You could use and interface with a static class. See example below. Just a thought

interface IStorage
{
    void Store(string item);
}

static class Storage
{
    private static readonly IStorage _instance;

    static Storage()
    {
        var storageTypeString = ConfigurationManager.AppSettings["storageTypeString"];
        var storageType = Type.GetType(storageTypeString, true);
        _instance = (IStorage)Activator.CreateInstance(storageType);
    }

    public static void Store(string item)
    {
        _instance.Store(item);
    }
}
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

743 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

11 Experts available now in Live!

Get 1:1 Help Now