Access Modifier Question

D

Doug

I am thinking there isn't a way to do this but will pose the question
anyways.

I have a component I'm building that contains my business entities. I
have an entity that I want to inherit properties from another. I have
done so like this:

public class FirstEntity : SecondEntity
{
private string _someValue = string.Empty;

public FirstEntity(string someValue, string inheritedValue)
{
InheritedValue = inheritedValue;
_someValue = _someValue;
}

public string SomeValue
{
get
{
return _someValue;
}
}
}

public class SecondEntity
{
string _inheritedValue = string.Empty;

public string InheritedValue
{
get
{
return _inheritedValue;
}
set
{
_inheritedValue = value;
}
}

The problem is, I would prefer not to make the SecondEntity a public
class. I don't want to give anyone the ability to create this class
themselves. I've tried various access modifiers...if I make the second
class an interface, then of course, I have to create the properties I
inherit from it within the FirstEntity, I'd prefer not to do it that
way. Internal or Protected prevent anyone using the FirstEntity class
to see the inherited properties.

Is there some way to do what I'm thinking?
}

/// <summary>Date the script was written.</summary>
public string Date
{
get
{
return _date;
}
set
{
 
G

Guest

Normally, if you don't want anyone to instantiate a class you would just have
a single private constructor for the class.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
 
D

Doug

That will definitely work, thank you. I was kind of wondering if there
was a way to make the properties non-public as well, but using a
private constructor should at least prevent them from being accessed
any way other than through the class that inherits them. Thanks!
 
D

Dave Sexton

Hi Doug,

It should be a single, protected constructor if you want to derive from the
class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top