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 3456196
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:42:24+00:00 2026-05-18T09:42:24+00:00

Ive ABSTRACT FACTORY pattern in one of my projects: http://www.dofactory.com/Patterns/PatternAbstract.aspx Code: public class QuestaoResposta

  • 0

Ive ABSTRACT FACTORY pattern in one of my projects:
http://www.dofactory.com/Patterns/PatternAbstract.aspx

Code:

public class QuestaoResposta : QuestaoBaseResposta, IQuestao,IQuestionario
{
    public int IDQuestaoResposta { get; set; }
}

public class QuestaoFactory : QuestoesFactory
{
    public override QuestaoBaseResposta CreateQuestao()
    {
        return new QuestaoResposta();
    }
}

public abstract class QuestoesFactory
{
    public abstract QuestaoBaseResposta CreateQuestao();
}

public class QuestaoBaseResposta : IQuestao, IMarcas, IQuestionario
{
    // Constructor where i want to create a concrete instance 
    // of any class that inherits QuestaoBaseResposta using QuestoesFactory 
    // abstract class, and assign it to current instance of
    // QuestaoBaseResposta class
    public QuestaoBaseResposta(QuestoesFactory qf)
    {
        this = qf.CreateQuestao();
    }
}

Problem is that i cant assing a value to current class using “THIS” keyword.

Example:

QuestaoBaseResposta qs = new QuestaoBaseResposta(new QuestaoFactory());

// Here i want the qs intance to be type of QuestaoResposta
// since im passing QuestaoFactory as argument,without cast anything.
qs.IDQuestaoResposta = 0;

What would you suggest to cast the QuestaoBaseResposta class to the inherit type (QuestaoResposta),without cast?

  • 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-05-18T09:42:25+00:00Added an answer on May 18, 2026 at 9:42 am

    No you can’t cast without cast. The usefulness of a factory comes from the fact that xyou do not need to know the exact derived class of the returned object. This is different if you donÄt use a factory but instead calling a constructor where you know the exact type.

    Why don’t you do it this way:

    QuestaoBaseResposta qs = new QuestaoFactory().CreateQuestao();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a quote-matching program in which two Abstract Factory Patterns are required,
I'm working on a database factory pattern for an application which should support Sql
I am trying to write a Factory Pattern to create either a MainMode or
I was working on an abstract class to save on some code for a
I've factored out common attributes from two classes into an abstract base class, however
I'm having some inheritance issues as I've got a group of inter-related abstract classes
I've been thinking about using extension methods as a replacement for an abstract base
Since I've started using NetBeans, I've learned of some powerful ways to abstract away
Recently I've been designing a Thread class library, I've made a Thread abstract class
Ive been smashing my head with this for a while. I have 2 completely

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.