Hi,

I am very week in design and program to interface. I have a task to design few classes. I have some simple interfaces
like below. Sample interfaces.

 public interface IParameterValue
    {
        string ParameterName { get; set; }
        string ParameterValu { get; set; }
    }

the above interface is a kind of common interface.

The below interfaces should actually implement the above interface.

Area Parameter interface:

public interface IAreaParameter
    {
        string AreaId { get; set; }
    }

    public class AreaParameter : IAreaParameter
    {
       public string AreaId { get; set; }
    } public interface IParameterValue
    {
        string ParameterName { get; set; }
        string ParameterValu { get; set; }
    }

Plot paramtere interface:

 public interface IPlotParameter
    {
        string AreaId { get; set; }
        string PartId { get; set; }
    }

    public class PlotParameter : IPlotParameter
    {
        public string AreaId { get; set; }
        public string PartId { get; set; }
    }

Now the common interface IParameterValue has ParameterName and ParameterValu.
There will be one more incoming value scope. depending on the scope
the paramterValu will be applied Area wise or Plot wise.

Now, I think we have to implement IParameterValue interface to IAreaParameter and IPlotParamter.
My question is, how to implement in code and if we implement how to access all this paramters
from other class and assign a value to them.

Simple question i think but did'n work with interfaces more so...

Thanks.

Dani AI

Generated

Two common designs cover this scenario: have the scoped interfaces inherit the parameter interface, or keep parameter data as a separate object and use composition. Both are valid; the choice depends on whether a scope (Area/Plot) always has exactly one parameter (inheritance is simple) or may have none, many, or changing parameters over time (composition is more flexible). The code posted by is the inheritance idea, and ’s reply shows how an instance can be created and its properties set; ’s request for an example is addressed below.

Inheritance (polymorphic access) — useful when every scope must expose a single parameter:

public interface IParam { string Key { get; set; } string Value { get; set; } }

public interface IAreaParam : IParam { string AreaId { get; set; } }

public class AreaParam : IAreaParam
{
    public string AreaId { get; set; }
    public string Key { get; set; }
    public string Value { get; set; }
}

A consumer class can operate against the interface:

public class UserService
{
    public void Apply(IParam target, string key, string value)
    {
        target.Key = key;
        target.Value = value;
    }
}

Composition (recommended when parameters are optional or multiple):

public class ParamValue { public string Name { get; set; } public string Value { get; set; } }

public class Area
{
    public string AreaId { get; set; }
    public List<ParamValue> Parameters { get; } = new List<ParamValue>();
}

This makes adding, removing, serializing, or storing many parameters straightforward.

Guidance: prefer composition for flexibility; use interface inheritance only when the parameter is an intrinsic single property of every implementing type. Use clear names (avoid typos like ParameterValu), program to interfaces in consumer code (accept IParam or IAreaParam), and choose a collection/dictionary if multiple named parameters are needed.

Recommended Answers

All 3 Replies

Let me clarify my question in other way.

I know how to implement an interface.
My question is design related and also if we implement the interface like below, how to
access it and assign values to all the properties and also read them.

Say, Interface is implemented like this:

  public interface IParameterValue
    {
        string ParameterName { get; set; }
        string ParameterValu { get; set; }
    }


 public interface IAreaParameter
    {
        string AreaId { get; set; }
    }

 public interface IPlotParameter
    {
        string AreaId { get; set; }
        string PartId { get; set; }
    }

Implementaion:

 public class AreaParameter : IAreaParameter, IParameterValue
    {
       public string AreaId { get; set; }
       public string ParameterName { get; set; }
       public string ParameterValu { get; set; }
    }

  public class PlotParameter : IPlotParameter, IParameterValue
    {
        public string AreaId { get; set; }
        public string PartId { get; set; }

        public string ParameterName { get; set; }
        public string ParameterValu { get; set; }

    }

Do I need to implement IParameterValue interface in a saparate class ParameterValue??

How to assign or access a value to these fields from other class say a class "USER"?
Actually, i don'nt know how to code this for assigning and retriving values ?

Thanks.

I'm not entirely sure what you're asking, sorry. Is it possible for you to put a code example of what you'd like to do? The code doesn't have to work, it's just to explain exactly what you want.

You have everything you need already. You just need to create an instance your class.

        PlotParameter pm = new PlotParameter();
        pm.AreaId = "id-1";
        pm.ParameterName = "fred";
        pm.ParameterValu = "wilma";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.