John
John

Reputation: 733

What is the best way to avoid hardcoded keys of Application.Current.Properties?

In my Xamarin.Forms application, I added a .NET standard library in which I would like to use the Properties dictionary.

However, I would like to avoid the hardcoding of the key "id".

Application.Current.Properties ["id"] = someClass.ID;

What is the best way to go about it?

Upvotes: 0

Views: 190

Answers (2)

David Conlisk
David Conlisk

Reputation: 3492

I would create a static class for this, something along the lines of

public static class Constants
    {
        public const string Id = "id";
        // etc...
    }

Then update your code to

Application.Current.Properties [Constants.Id] = someClass.ID;

Upvotes: 2

Nikolaus
Nikolaus

Reputation: 1869

There is probably no best way, but one way is to use the nameof syntax, because if you rename a Property, this will be changed, too:

// I added ToLowerInvariant, because you wrote "id".
Application.Current.Properties[nameof(someClass.ID).ToLowerInvariant()] = someClass.ID;

Upvotes: 1

Related Questions