0

I have worked on iOS project and now started with Windows Phone,

in iOS I have copied all common classes in one folder (lets say parent) and linked that folder in different projects, let's say child projects.

in common classes I have one class called Constants.m in that I have defied all #define statements with some values approximately 98 statements, I am using that across all classes.

And in child project I am using .pch file in that I have defined only project specific values #define statements let's say 10 values.

Now when I run the child project, whatever values I have defined in .pch file it will override that values with default one from Constants.m, so I will get child project specific values, and whatever values I didn't define in child project that code will pick default values from Constants.h.

I am trying to use similar kind of stuff in Windows phone application development, I am able to link the classes but I am not able get #define from other files.

How to use #define from another file?

Is there any way from that I can override defined values ? as just like iOS.

Community
  • 1
  • 1
Mac
  • 1,153
  • 2
  • 20
  • 34

1 Answers1

0

In C# you can't use #define to set value of a global constant.

Of the top of my head, there are two ways of having global constants in parent project and overriding them in child project:

1) use config (my personal preference)

2) use virtual properties (as mentioned in comments)

Config in it's simplest form would look like

<configuration>
 <appSettings>
  <add key="Setting1" value="Value1" />
  <add key="Setting2" value="Value2" />
 </appSettings>
</configuration>

and is used like so

string setting1 = ConfigurationSettings.AppSettings["Setting1"];

although I strongly recommend using custom configuration sections.

Virtual properties at their simplest are

// in parent project
class ParentValues
{
    public virtual int Key1
    {
        get { return 5; }
    }

    public virtual int Key2
    {
        get { return 10; }
    }
}

// in child project
class ChildValues : ParentValues
{
    public override int Key2
    {
        get
        {
            return 12;
        }
    }
}

and used like

// in child project
class ValueUser
{
    public int GetValue()
    {
        ChildValues cv = new ChildValues();
        return cv.Key2;
    }
}
user270576
  • 987
  • 10
  • 16