Solved

C#.NET static classes

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 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