Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8785007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:14:38+00:00 2026-06-13T21:14:38+00:00

How to declare variable that is for generic type of instance? In controller, I

  • 0

How to declare variable that is for generic type of instance?

In controller, I need to create instance that depend on payment type, and each class has different type of parameter. That’s why I used generic type.
But I don’t know what type I need to set to define variable for each payment class.

Models for parameter

public class PaymentModel
{   
    public string orderNo { get; set;}
}

public class CCPaymentModel : PaymentModel
{
    public string CCNo {get; set;}
    public string expDate {get; set;}
}

public class PaypalPaymentModel : PaymentModel
{
    public string paypalID {get; set;}
}

public class GooglePaymentModel : PaymentModel
{
    public string googleID {get; set;}
} 

Interface class, I use Generic type parameter because each payment type need different type of parameter.

public interface IPayment<T> where T : PaymentModel
{
    void makePayment(string orderNo);
    void makeRefund(T refundInfo);
}

Models,

public class SagePayment
    : IPayment<CreditCardPaymentInfo>
{
    public void MakePayment( CreditCardPaymentInfo creditCardPaymentInfo ) {
        // make payment
    }

    public void MakeRefund( CreditCardPaymentInfo creditCardPaymentInfo ) {
        // make refund
    }
}

public class GooglePayment
    : IPayment<GooglePaymentModel>
{
    public void MakePayment( GooglePaymentModel paymentInfo ) {
        // make payment
    }

    public void MakeRefund( GooglePaymentModel paymentInfo ) {
        // make refund
    }
}

public class PaypalPayment
    : IPayment<PayPalPaymentModel>
{
    public void MakePayment( PayPalPaymentModel paymentInfo ) {
        // make payment
    }

    public void MakeRefund( PayPalPaymentModel paymentInfo ) {
        // make refund
    }
}

Controller (Create instance)

public void Charge(string paytype,orderNo){

    IPayment<???> paymentProcess; // //Error    1   Using the generic type 'com.WebUI.Models.IPayment<T>' requires 1 type arguments
    Object payinfo;  //

    if (Regex.IsMatch(paytype, "^Credit Card"))
    {
        paymentProcess = new SagePayment();
        payinfo = getPaymentInfo(paytype, orderNo); // it return CCPaymentModel type object
    }
    else if (Regex.IsMatch(paytype, "^PayPal"))
    {
        paymentProcess = new PayPalPayment();
        payinfo = getPaymentInfo(paytype, orderNo); // it return PaypalPaymentModel type object
    }
    else if (Regex.IsMatch(paytype, "^Google"))
    {
        paymentProcess = new GooglePayment();
        payinfo = getPaymentInfo(paytype, orderNo); // it return GooglePaymentModel type object
    }

    paymentProcess.MakePayment(payinfo);
}

To avoid error, I could this,

public void Charge(string paytype,orderNo){

    if (Regex.IsMatch(paytype, "^Credit Card"))
    {
        IPayment<CCPaymentModel> paymentProcess = new SagePayment();
        payinfo = getPaymentInfo(paytype, orderNo);
        paymentProcess.MakePayment(payinfo);
    }
    else if (Regex.IsMatch(paytype, "^PayPal"))
    {
        IPayment<PaypalPaymentModel> paymentProcess = new PayPalPayment();
        payinfo = getPaymentInfo(paytype, orderNo);
        paymentProcess.MakePayment(payinfo);
    }
    else if (Regex.IsMatch(paytype, "^Google"))
    {
        IPayment<GooglePaymentModel> paymentProcess = new GooglePayment();
        payinfo = getPaymentInfo(paytype, orderNo);
        paymentProcess.MakePayment(payinfo);
    }
}

public void Refund(string paytype,orderNo){

    IPayment<???> paymentProcess; // //Error    1   Using the generic type 'com.WebUI.Models.IPayment<T>' requires 1 type arguments
    Object payinfo;  //

    if (Regex.IsMatch(paytype, "^Credit Card"))
    {
        paymentProcess = new SagePayment();
        payinfo = getPaymentInfo(paytype, orderNo); // it return CCPaymentModel type object
    }
    else if (Regex.IsMatch(paytype, "^PayPal"))
    {
        paymentProcess = new PayPalPayment();
        payinfo = getPaymentInfo(paytype, orderNo); // it return PaypalPaymentModel type object
    }
    else if (Regex.IsMatch(paytype, "^Google"))
    {
        paymentProcess = new GooglePayment();
        payinfo = getPaymentInfo(paytype, orderNo); // it return GooglePaymentModel type object
    }

    paymentProcess.MakeRefund(payinfo);
}

But I know it is not right way.

Anybody know, please advice me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:14:39+00:00Added an answer on June 13, 2026 at 9:14 pm

    You could make Charge() and getPaymentInfo() be generic with respect to the type of PaymentModel:

    void Charge<TPaymentModel>(...) where TPaymentModel : PaymentModel {
        IPayment<TPaymentModel> payment = GetPayment<TPaymentModel>();
        // ...
        payment.MakePayment(getPaymentInfo<TPaymentModel>(...));
    }
    
    IPayment<TPaymentModel> GetPayment<TPaymentModel>() where TPaymentModel : IPaymentModel
    {
        // Create payment of appropriate type based on typeof(TPaymentModel)
    }
    
    TPaymentModel GetPaymentInfo(...) where TPaymentModel : PaymentModel
    {
        // Create payment model of appropriate type based on typeof(TPaymentModel)
    }
    

    This is still a little ugly, to resolve this you could introduce a few new classes to hide the correspondence between the types from the interfaces and make the design more sound:

    /// Abstracts over different ways of making payments
    interface IPaymentMaker 
    {
        void MakePayment(string payType, long orderNo);
        // MakeRefund etc.
    }
    
    /// Refactor code common to all payment types here, and handle the association 
    /// between payment and payment model.
    class PaymentMakerBase<TPaymentModel> : IPaymentMaker 
        where TPaymentModel : IPaymentModel 
    {
        void MakePayment(string payType, long orderNo) 
        {
            NewPayment().MakePayment(NewPaymentModel(payType, orderNo));
        }
    
        abstract IPayment<TPaymentModel> NewPayment();
        abstract TPaymentModel NewPaymentModel(string payType, long orderNo);
    }
    
    /// Handle only the differences between payment types that can't be put inside their
    /// implementations
    class PaypalPaymentMaker : PaymentMakerBase<PaypalPayment> 
    {
        IPayment<PaypalPayment> NewPayment() { ... }
        PaypalPayment NewPaymentModel(...) { ... }
    }
    
    static class PaymentMakerFactory 
    {
        /// The only "not type safe" part, handles parsing the payType string and 
        /// resolving it to the correct `PaymentMaker`
        public IPaymentMaker GetPaymentMaker(string payType) 
        {
            if (Regex.IsMatch(payType, ...)) 
            {
                // return appropriate payment maker for the payType
            }
            else if (...) 
            {
                // ...
            }
        }
    }
    

    Then, your controller code only looks like this:

    PaymentMakerFactory.GetPaymentMaker(payType).MakePayment(payType, orderNo);
    

    Obviously the above design could be improved by removing redundancies (payType probably isn’t needed everywhere I include it), making it more “objecty” (instead of passing identical argument lists around), or more convenient (PaymentMakerFactory could probably be changed into a facade that creates the right payment maker and then calls MakePayment right away).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to declare that the variable type of a generic class
I need to declare the query variable outside the switch statement that way I
I've read that you cannot declare static variables/methods inside a generic class and I
I want to declare a variable that is of the of the type returned
I am trying to create a generic Exists() Command class that is able to
How do I declare an object of a variable type? I know that I
I'm writing some code where I need to have a class variable that's a
I have a vector declared as a global variable that I need to be
I gather that in Objective-C I must declare instance variables as part of the
I'd like to declare a global variable that is scoped to only my rules

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.