Solved

C#.NET static classes

Posted on 2015-01-26
5
212 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
[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
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 30

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

624 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