Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C#.NET static classes

Posted on 2015-01-26
5
Medium Priority
?
215 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 2000 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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

705 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