What are ways of distinguishing objects of the same type for retrieval, comparison, etc in c#












1















I apologize for the strange title, I wasn't sure how to ask the question, so If you have any suggestion as to what would be more appropriate, let me know and I'll update it.



Often when programming, I would use Enums as keys to dictionaries whos values are data relating to the enum. I'd usually do this before I realize that there would be lots of data associated with that enum, which would then lead me on to create a data type.



For example, I might have the enum:



public enum AreaKey
{
Home,
Hospital,
Park,
TrainStation
}


and then a dictionary (Vector2 being a class with 2 integers):



Dictionary<AreaKey, Vector2> areaCoordinatesByAreaKey;


Later down the line, I decide I want a bunch of other data associated with this area, so instead I build a new class called:



    public class Area
{
Vector2 coordinates;
string name;
// more data
}


Now, to reference this data, I know I can just build a dictionary using the same enum as the key to retrieve the specified area, but this always feels cumbersome to me, and almost like it's not proper OOP.



I have the area objects. They have specific data that should make them unique from one and other. Why do I have to use an enum? Also I don't like having to break OCP everytime I realize I need a new area. It would be awesome to just be able to create an object and not have to worry about external management. order 1 retrieval is nice though.



Of course I could use a string key to retrieve it, but that means people have to know the strings and it's not very type safe... enums are easy to read. Help me stack overflow!










share|improve this question


















  • 1





    It largely depends on how you want to use them. Having a dictionary of Area objects keyed by AreaKey seems a little odd, because you will only ever have one area of each type.

    – Evan Trimboli
    Nov 21 '18 at 23:20






  • 1





    This is unnecessary obfuscation. What's wrong with a normal class/instance with properties? They read like a charm. "Why do I have to use an enum?"... for this, you don't. I don't understand why you ever did.

    – spender
    Nov 21 '18 at 23:30











  • I assume the use of enum stems from something related to "people have to know the strings". There's a whole part to this problem that you haven't really written about, which is how you got here in the first place (I imagine that it's some piece of UI from which users submit data).

    – spender
    Nov 21 '18 at 23:33











  • Using a Dictionary that could only ever store 4 objects is a heavy code smell. Hard to guess how this happened, brain-bugs tend to be hard to identify from a code snippet. You're only half-way discovering that something went wrong, think some more.about object identity.

    – Hans Passant
    Nov 21 '18 at 23:38













  • I think it might be related to the fact that we use singletons a lot in our code (not me personally, our team). The singleton would contain a bunch of data (like the Areas) and we would need to retrieve the areas, so we would use dictionaries with the enum key... Could this be related?

    – Sebastian King
    Nov 21 '18 at 23:41
















1















I apologize for the strange title, I wasn't sure how to ask the question, so If you have any suggestion as to what would be more appropriate, let me know and I'll update it.



Often when programming, I would use Enums as keys to dictionaries whos values are data relating to the enum. I'd usually do this before I realize that there would be lots of data associated with that enum, which would then lead me on to create a data type.



For example, I might have the enum:



public enum AreaKey
{
Home,
Hospital,
Park,
TrainStation
}


and then a dictionary (Vector2 being a class with 2 integers):



Dictionary<AreaKey, Vector2> areaCoordinatesByAreaKey;


Later down the line, I decide I want a bunch of other data associated with this area, so instead I build a new class called:



    public class Area
{
Vector2 coordinates;
string name;
// more data
}


Now, to reference this data, I know I can just build a dictionary using the same enum as the key to retrieve the specified area, but this always feels cumbersome to me, and almost like it's not proper OOP.



I have the area objects. They have specific data that should make them unique from one and other. Why do I have to use an enum? Also I don't like having to break OCP everytime I realize I need a new area. It would be awesome to just be able to create an object and not have to worry about external management. order 1 retrieval is nice though.



Of course I could use a string key to retrieve it, but that means people have to know the strings and it's not very type safe... enums are easy to read. Help me stack overflow!










share|improve this question


















  • 1





    It largely depends on how you want to use them. Having a dictionary of Area objects keyed by AreaKey seems a little odd, because you will only ever have one area of each type.

    – Evan Trimboli
    Nov 21 '18 at 23:20






  • 1





    This is unnecessary obfuscation. What's wrong with a normal class/instance with properties? They read like a charm. "Why do I have to use an enum?"... for this, you don't. I don't understand why you ever did.

    – spender
    Nov 21 '18 at 23:30











  • I assume the use of enum stems from something related to "people have to know the strings". There's a whole part to this problem that you haven't really written about, which is how you got here in the first place (I imagine that it's some piece of UI from which users submit data).

    – spender
    Nov 21 '18 at 23:33











  • Using a Dictionary that could only ever store 4 objects is a heavy code smell. Hard to guess how this happened, brain-bugs tend to be hard to identify from a code snippet. You're only half-way discovering that something went wrong, think some more.about object identity.

    – Hans Passant
    Nov 21 '18 at 23:38













  • I think it might be related to the fact that we use singletons a lot in our code (not me personally, our team). The singleton would contain a bunch of data (like the Areas) and we would need to retrieve the areas, so we would use dictionaries with the enum key... Could this be related?

    – Sebastian King
    Nov 21 '18 at 23:41














1












1








1








I apologize for the strange title, I wasn't sure how to ask the question, so If you have any suggestion as to what would be more appropriate, let me know and I'll update it.



Often when programming, I would use Enums as keys to dictionaries whos values are data relating to the enum. I'd usually do this before I realize that there would be lots of data associated with that enum, which would then lead me on to create a data type.



For example, I might have the enum:



public enum AreaKey
{
Home,
Hospital,
Park,
TrainStation
}


and then a dictionary (Vector2 being a class with 2 integers):



Dictionary<AreaKey, Vector2> areaCoordinatesByAreaKey;


Later down the line, I decide I want a bunch of other data associated with this area, so instead I build a new class called:



    public class Area
{
Vector2 coordinates;
string name;
// more data
}


Now, to reference this data, I know I can just build a dictionary using the same enum as the key to retrieve the specified area, but this always feels cumbersome to me, and almost like it's not proper OOP.



I have the area objects. They have specific data that should make them unique from one and other. Why do I have to use an enum? Also I don't like having to break OCP everytime I realize I need a new area. It would be awesome to just be able to create an object and not have to worry about external management. order 1 retrieval is nice though.



Of course I could use a string key to retrieve it, but that means people have to know the strings and it's not very type safe... enums are easy to read. Help me stack overflow!










share|improve this question














I apologize for the strange title, I wasn't sure how to ask the question, so If you have any suggestion as to what would be more appropriate, let me know and I'll update it.



Often when programming, I would use Enums as keys to dictionaries whos values are data relating to the enum. I'd usually do this before I realize that there would be lots of data associated with that enum, which would then lead me on to create a data type.



For example, I might have the enum:



public enum AreaKey
{
Home,
Hospital,
Park,
TrainStation
}


and then a dictionary (Vector2 being a class with 2 integers):



Dictionary<AreaKey, Vector2> areaCoordinatesByAreaKey;


Later down the line, I decide I want a bunch of other data associated with this area, so instead I build a new class called:



    public class Area
{
Vector2 coordinates;
string name;
// more data
}


Now, to reference this data, I know I can just build a dictionary using the same enum as the key to retrieve the specified area, but this always feels cumbersome to me, and almost like it's not proper OOP.



I have the area objects. They have specific data that should make them unique from one and other. Why do I have to use an enum? Also I don't like having to break OCP everytime I realize I need a new area. It would be awesome to just be able to create an object and not have to worry about external management. order 1 retrieval is nice though.



Of course I could use a string key to retrieve it, but that means people have to know the strings and it's not very type safe... enums are easy to read. Help me stack overflow!







c# oop object enums ocp






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 23:16









Sebastian KingSebastian King

646




646








  • 1





    It largely depends on how you want to use them. Having a dictionary of Area objects keyed by AreaKey seems a little odd, because you will only ever have one area of each type.

    – Evan Trimboli
    Nov 21 '18 at 23:20






  • 1





    This is unnecessary obfuscation. What's wrong with a normal class/instance with properties? They read like a charm. "Why do I have to use an enum?"... for this, you don't. I don't understand why you ever did.

    – spender
    Nov 21 '18 at 23:30











  • I assume the use of enum stems from something related to "people have to know the strings". There's a whole part to this problem that you haven't really written about, which is how you got here in the first place (I imagine that it's some piece of UI from which users submit data).

    – spender
    Nov 21 '18 at 23:33











  • Using a Dictionary that could only ever store 4 objects is a heavy code smell. Hard to guess how this happened, brain-bugs tend to be hard to identify from a code snippet. You're only half-way discovering that something went wrong, think some more.about object identity.

    – Hans Passant
    Nov 21 '18 at 23:38













  • I think it might be related to the fact that we use singletons a lot in our code (not me personally, our team). The singleton would contain a bunch of data (like the Areas) and we would need to retrieve the areas, so we would use dictionaries with the enum key... Could this be related?

    – Sebastian King
    Nov 21 '18 at 23:41














  • 1





    It largely depends on how you want to use them. Having a dictionary of Area objects keyed by AreaKey seems a little odd, because you will only ever have one area of each type.

    – Evan Trimboli
    Nov 21 '18 at 23:20






  • 1





    This is unnecessary obfuscation. What's wrong with a normal class/instance with properties? They read like a charm. "Why do I have to use an enum?"... for this, you don't. I don't understand why you ever did.

    – spender
    Nov 21 '18 at 23:30











  • I assume the use of enum stems from something related to "people have to know the strings". There's a whole part to this problem that you haven't really written about, which is how you got here in the first place (I imagine that it's some piece of UI from which users submit data).

    – spender
    Nov 21 '18 at 23:33











  • Using a Dictionary that could only ever store 4 objects is a heavy code smell. Hard to guess how this happened, brain-bugs tend to be hard to identify from a code snippet. You're only half-way discovering that something went wrong, think some more.about object identity.

    – Hans Passant
    Nov 21 '18 at 23:38













  • I think it might be related to the fact that we use singletons a lot in our code (not me personally, our team). The singleton would contain a bunch of data (like the Areas) and we would need to retrieve the areas, so we would use dictionaries with the enum key... Could this be related?

    – Sebastian King
    Nov 21 '18 at 23:41








1




1





It largely depends on how you want to use them. Having a dictionary of Area objects keyed by AreaKey seems a little odd, because you will only ever have one area of each type.

– Evan Trimboli
Nov 21 '18 at 23:20





It largely depends on how you want to use them. Having a dictionary of Area objects keyed by AreaKey seems a little odd, because you will only ever have one area of each type.

– Evan Trimboli
Nov 21 '18 at 23:20




1




1





This is unnecessary obfuscation. What's wrong with a normal class/instance with properties? They read like a charm. "Why do I have to use an enum?"... for this, you don't. I don't understand why you ever did.

– spender
Nov 21 '18 at 23:30





This is unnecessary obfuscation. What's wrong with a normal class/instance with properties? They read like a charm. "Why do I have to use an enum?"... for this, you don't. I don't understand why you ever did.

– spender
Nov 21 '18 at 23:30













I assume the use of enum stems from something related to "people have to know the strings". There's a whole part to this problem that you haven't really written about, which is how you got here in the first place (I imagine that it's some piece of UI from which users submit data).

– spender
Nov 21 '18 at 23:33





I assume the use of enum stems from something related to "people have to know the strings". There's a whole part to this problem that you haven't really written about, which is how you got here in the first place (I imagine that it's some piece of UI from which users submit data).

– spender
Nov 21 '18 at 23:33













Using a Dictionary that could only ever store 4 objects is a heavy code smell. Hard to guess how this happened, brain-bugs tend to be hard to identify from a code snippet. You're only half-way discovering that something went wrong, think some more.about object identity.

– Hans Passant
Nov 21 '18 at 23:38







Using a Dictionary that could only ever store 4 objects is a heavy code smell. Hard to guess how this happened, brain-bugs tend to be hard to identify from a code snippet. You're only half-way discovering that something went wrong, think some more.about object identity.

– Hans Passant
Nov 21 '18 at 23:38















I think it might be related to the fact that we use singletons a lot in our code (not me personally, our team). The singleton would contain a bunch of data (like the Areas) and we would need to retrieve the areas, so we would use dictionaries with the enum key... Could this be related?

– Sebastian King
Nov 21 '18 at 23:41





I think it might be related to the fact that we use singletons a lot in our code (not me personally, our team). The singleton would contain a bunch of data (like the Areas) and we would need to retrieve the areas, so we would use dictionaries with the enum key... Could this be related?

– Sebastian King
Nov 21 '18 at 23:41












1 Answer
1






active

oldest

votes


















1














It totally depends on how you intend to retrieve the object. For example typically when creating WinForms apps (before I knew about the magic of data binding) and I have added items to say a ListBox, I might want to remember each object's position in the list so that I can retrieve it easily later. I would create Dictionary<Foo, int>. Similarly, I may want to allow quick lookup based on the position in the list so I would have a Dictionary<int, Foo>. In other words don't try to store everything into one Dictionary<>. Define them by use case.



You have already defined one by AreaKey and this assumes no two objects have the same enum and so using a dictionary in this case makes sense. However, I think you are going down the wrong track trying to put everything into one key. Instead you might want to have another Dictionary<string, Area> where it refers to the Area.name so that not only can you lookup by enum, but also by name.



Later you might want to add PostCode so again another Dictionary<int, Area> makes sense.



In summary, define:



Dictionary<string, Area> mapAreasByName;
Dictionary<int, Area> mapAreasByPostCode;

var area = mapAreasByName["New York"];
area = mapAreasByPostCode[2000];





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421780%2fwhat-are-ways-of-distinguishing-objects-of-the-same-type-for-retrieval-comparis%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    It totally depends on how you intend to retrieve the object. For example typically when creating WinForms apps (before I knew about the magic of data binding) and I have added items to say a ListBox, I might want to remember each object's position in the list so that I can retrieve it easily later. I would create Dictionary<Foo, int>. Similarly, I may want to allow quick lookup based on the position in the list so I would have a Dictionary<int, Foo>. In other words don't try to store everything into one Dictionary<>. Define them by use case.



    You have already defined one by AreaKey and this assumes no two objects have the same enum and so using a dictionary in this case makes sense. However, I think you are going down the wrong track trying to put everything into one key. Instead you might want to have another Dictionary<string, Area> where it refers to the Area.name so that not only can you lookup by enum, but also by name.



    Later you might want to add PostCode so again another Dictionary<int, Area> makes sense.



    In summary, define:



    Dictionary<string, Area> mapAreasByName;
    Dictionary<int, Area> mapAreasByPostCode;

    var area = mapAreasByName["New York"];
    area = mapAreasByPostCode[2000];





    share|improve this answer






























      1














      It totally depends on how you intend to retrieve the object. For example typically when creating WinForms apps (before I knew about the magic of data binding) and I have added items to say a ListBox, I might want to remember each object's position in the list so that I can retrieve it easily later. I would create Dictionary<Foo, int>. Similarly, I may want to allow quick lookup based on the position in the list so I would have a Dictionary<int, Foo>. In other words don't try to store everything into one Dictionary<>. Define them by use case.



      You have already defined one by AreaKey and this assumes no two objects have the same enum and so using a dictionary in this case makes sense. However, I think you are going down the wrong track trying to put everything into one key. Instead you might want to have another Dictionary<string, Area> where it refers to the Area.name so that not only can you lookup by enum, but also by name.



      Later you might want to add PostCode so again another Dictionary<int, Area> makes sense.



      In summary, define:



      Dictionary<string, Area> mapAreasByName;
      Dictionary<int, Area> mapAreasByPostCode;

      var area = mapAreasByName["New York"];
      area = mapAreasByPostCode[2000];





      share|improve this answer




























        1












        1








        1







        It totally depends on how you intend to retrieve the object. For example typically when creating WinForms apps (before I knew about the magic of data binding) and I have added items to say a ListBox, I might want to remember each object's position in the list so that I can retrieve it easily later. I would create Dictionary<Foo, int>. Similarly, I may want to allow quick lookup based on the position in the list so I would have a Dictionary<int, Foo>. In other words don't try to store everything into one Dictionary<>. Define them by use case.



        You have already defined one by AreaKey and this assumes no two objects have the same enum and so using a dictionary in this case makes sense. However, I think you are going down the wrong track trying to put everything into one key. Instead you might want to have another Dictionary<string, Area> where it refers to the Area.name so that not only can you lookup by enum, but also by name.



        Later you might want to add PostCode so again another Dictionary<int, Area> makes sense.



        In summary, define:



        Dictionary<string, Area> mapAreasByName;
        Dictionary<int, Area> mapAreasByPostCode;

        var area = mapAreasByName["New York"];
        area = mapAreasByPostCode[2000];





        share|improve this answer















        It totally depends on how you intend to retrieve the object. For example typically when creating WinForms apps (before I knew about the magic of data binding) and I have added items to say a ListBox, I might want to remember each object's position in the list so that I can retrieve it easily later. I would create Dictionary<Foo, int>. Similarly, I may want to allow quick lookup based on the position in the list so I would have a Dictionary<int, Foo>. In other words don't try to store everything into one Dictionary<>. Define them by use case.



        You have already defined one by AreaKey and this assumes no two objects have the same enum and so using a dictionary in this case makes sense. However, I think you are going down the wrong track trying to put everything into one key. Instead you might want to have another Dictionary<string, Area> where it refers to the Area.name so that not only can you lookup by enum, but also by name.



        Later you might want to add PostCode so again another Dictionary<int, Area> makes sense.



        In summary, define:



        Dictionary<string, Area> mapAreasByName;
        Dictionary<int, Area> mapAreasByPostCode;

        var area = mapAreasByName["New York"];
        area = mapAreasByPostCode[2000];






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 '18 at 2:00

























        answered Nov 21 '18 at 23:44









        MickyDMickyD

        10.7k63352




        10.7k63352
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421780%2fwhat-are-ways-of-distinguishing-objects-of-the-same-type-for-retrieval-comparis%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]