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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:50:53+00:00 2026-06-17T22:50:53+00:00

I hope the title of this question summarizes the problem accurately. Here are the

  • 0

I hope the title of this question summarizes the problem accurately. Here are the details:

I have two classes that share several properties and related methods, let’s MyFirstClass and MySecondClass. So, I put this intersecting set of properties into an abstract class that both my classes inherit from: MyAbstractBaseClass.

So far, so good. However, in a method elsewhere in my code, I am manipulating these shared properties and would like to be able to pass instances of both MyFirstClass and MySecondClass to this method by reference. Something like:

result1 = myMethod(ref MyFirstClass);
result2 = myMethod(ref MySecondClass);

I have tried using MyAbstractBaseClass as the parameter type for my method:

public bool myMethod(ref MyAbstractBaseClass anObject)

But this is not accepted by the compiler. I have also tried to extract the interface from MyAbstractBaseClass and have both MyFirstClass and MySecondClass inherit from MyAbstractBaseClass and implement the interface, like so:

public class MyFirstClass : MyAbstractBaseClass, IMyAbstractBaseClass 
public class MySecondClass : MyAbstractBaseClass, IMyAbstractBaseClass 

I was then expecting that the myMethod would be able to operate on both classes, if I make the parameter of the interface of type IMyAbstractBaseClass (after all, in OOP you’re supposed to code against interface when possible).

public bool myMethod(ref IMyAbstractBaseClass anObject)

IMyAbstractBaseClass myObject = new MyFirstClass();
result1 = myMethod(ref myObject);

This isn’t working either. The compiler says it is not possible to convert MyFirstClass to IMyAbstractBaseClass, which seems odd to me, because MyFirstClass implements the Interface and therefore I should be able to treat all objects of this class as type interface, no?

What am I missing here?

  • 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-17T22:50:54+00:00Added an answer on June 17, 2026 at 10:50 pm

    Do the parameters really need to be passed by ref?

    If they are reference types (i.e. classes) (and they are), and you are only setting properties on them, then they do not need to be passed by reference. (Passing a reference by referene is having a pointer to a pointer)

    Only in cases where you will be replacing the whole object do you need the ref:

    public bool myMethod(ref IMyAbstractBaseClass anObject)
    {
          myObject = new MyFirstClass();
          return true;
    }
    
    public bool myMethod2(MyAbstractBaseClass anObject)
    {
          myObject.SomeProperty = 5;
          return true;
    }
    
    IMyAbstractBaseClass myObject = new MyFirstClass();
    result1 = myMethod(ref myObject);
    result1 = myMethod2(myObject);
    

    myMethod2() will work exactly as you want.

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

Sidebar

Related Questions

I hope this is the appropriate title for this question. Right now I have
(I hope this is a valid question) As I stated in my title, I'm
I hope the title makes sense. I have a set of items that I
I hope the title is chosen well enough to ask this question. Feel free
I hope the title of this question is somewhat clear, I couldn't figure out
I hope this isn't too vague a question, but here goes. I want to
I hope the title of this question makes sense. What I want to know
I have 3 classes (simplified for this question): public class Page { public Guid
I had some problem formulating the question in the title, but i hope you
hope someone can help with this problem that's been driving me insane for ages.

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.