Solved

C#.NET static classes

Posted on 2015-01-26
5
199 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
location of a form 2 25
Upgrading my SSIS package in VS 2012 6 62
Add a Condition in ASP Repeater 6 34
Problem to Office 1 18
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 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