Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Trick for young players.

Trick for young players.

Scheduled Pinned Locked Moved The Weird and The Wonderful
wpfcsharpcomarchitecture
49 Posts 18 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Look at the prog below - a collection has one(object)bool in it. And I want to find out if one of them exists in the collection. Using collection.Contains() would seem like a good idea. But isn't!

    class Program
    {
    static void Main(string[] args)
    {
    object arg1 = true;
    object arg2 = true;

    		List<object> collection = new List<object>() { arg1 };
    
    		bool first = (collection.Contains(arg1));
    		bool second = (collection.Contains(arg2));
    		Console.WriteLine(string.Format("{0} {1}", first, second));
    
    		first = false;
    		second = false;
    
    		for (int i = 0; i < collection.Count; i++)
    		{
    			if (collection\[i\] == arg1)
    			{
    				first = true;
    			}
    			if (collection\[i\] == arg2)
    			{
    				second = true;
    			}
    		}
    
    		Console.WriteLine(string.Format("{0} {1}", first, second));
    
    			Console.ReadKey();
    	}
    }
    

    MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

    G J T L J 10 Replies Last reply
    0
    • L Lost User

      Look at the prog below - a collection has one(object)bool in it. And I want to find out if one of them exists in the collection. Using collection.Contains() would seem like a good idea. But isn't!

      class Program
      {
      static void Main(string[] args)
      {
      object arg1 = true;
      object arg2 = true;

      		List<object> collection = new List<object>() { arg1 };
      
      		bool first = (collection.Contains(arg1));
      		bool second = (collection.Contains(arg2));
      		Console.WriteLine(string.Format("{0} {1}", first, second));
      
      		first = false;
      		second = false;
      
      		for (int i = 0; i < collection.Count; i++)
      		{
      			if (collection\[i\] == arg1)
      			{
      				first = true;
      			}
      			if (collection\[i\] == arg2)
      			{
      				second = true;
      			}
      		}
      
      		Console.WriteLine(string.Format("{0} {1}", first, second));
      
      			Console.ReadKey();
      	}
      }
      

      MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

      G Offline
      G Offline
      Guirec
      wrote on last edited by
      #2

      What would you expect? Looks all fine... arg2 is not contained in the collection but collection[0] is equal (not the same but nonetheless equal) to arg2

      Seulement, dans certains cas, n'est-ce pas, on n'entend guère que ce qu'on désire entendre et ce qui vous arrange le mieux... [^]

      L 1 Reply Last reply
      0
      • G Guirec

        What would you expect? Looks all fine... arg2 is not contained in the collection but collection[0] is equal (not the same but nonetheless equal) to arg2

        Seulement, dans certains cas, n'est-ce pas, on n'entend guère que ce qu'on désire entendre et ce qui vous arrange le mieux... [^]

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        So you would expect contains(arg2) to return false?

        MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

        G 1 Reply Last reply
        0
        • L Lost User

          So you would expect contains(arg2) to return false?

          MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

          G Offline
          G Offline
          Guirec
          wrote on last edited by
          #4

          For sure! your collection is a list of objects and the arg2 object is simply not in that collection...

          Seulement, dans certains cas, n'est-ce pas, on n'entend guère que ce qu'on désire entendre et ce qui vous arrange le mieux... [^]

          L B 2 Replies Last reply
          0
          • G Guirec

            For sure! your collection is a list of objects and the arg2 object is simply not in that collection...

            Seulement, dans certains cas, n'est-ce pas, on n'entend guère que ce qu'on désire entendre et ce qui vous arrange le mieux... [^]

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            You're obviously a young player. Run it!

            MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

            G 1 Reply Last reply
            0
            • L Lost User

              You're obviously a young player. Run it!

              MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

              G Offline
              G Offline
              Guirec
              wrote on last edited by
              #6

              Wtf? Goddamn Sh*t! I would not expect that either.... I am a young player!!

              Seulement, dans certains cas, n'est-ce pas, on n'entend guère que ce qu'on désire entendre et ce qui vous arrange le mieux... [^]

              L J 2 Replies Last reply
              0
              • G Guirec

                Wtf? Goddamn Sh*t! I would not expect that either.... I am a young player!!

                Seulement, dans certains cas, n'est-ce pas, on n'entend guère que ce qu'on désire entendre et ce qui vous arrange le mieux... [^]

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Guirec Le Bars wrote:

                Wtf? Goddamn Sh*t!

                That's almost exactly what I said when I realised what was going on!!!! This was found in the middle of an MVVM framework that was causing me an issue. Good one, eh?

                MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

                G 1 Reply Last reply
                0
                • L Lost User

                  Guirec Le Bars wrote:

                  Wtf? Goddamn Sh*t!

                  That's almost exactly what I said when I realised what was going on!!!! This was found in the middle of an MVVM framework that was causing me an issue. Good one, eh?

                  MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

                  G Offline
                  G Offline
                  Guirec
                  wrote on last edited by
                  #8

                  Then you're not much elder than me :)

                  Seulement, dans certains cas, n'est-ce pas, on n'entend guère que ce qu'on désire entendre et ce qui vous arrange le mieux... [^]

                  1 Reply Last reply
                  0
                  • L Lost User

                    Look at the prog below - a collection has one(object)bool in it. And I want to find out if one of them exists in the collection. Using collection.Contains() would seem like a good idea. But isn't!

                    class Program
                    {
                    static void Main(string[] args)
                    {
                    object arg1 = true;
                    object arg2 = true;

                    		List<object> collection = new List<object>() { arg1 };
                    
                    		bool first = (collection.Contains(arg1));
                    		bool second = (collection.Contains(arg2));
                    		Console.WriteLine(string.Format("{0} {1}", first, second));
                    
                    		first = false;
                    		second = false;
                    
                    		for (int i = 0; i < collection.Count; i++)
                    		{
                    			if (collection\[i\] == arg1)
                    			{
                    				first = true;
                    			}
                    			if (collection\[i\] == arg2)
                    			{
                    				second = true;
                    			}
                    		}
                    
                    		Console.WriteLine(string.Format("{0} {1}", first, second));
                    
                    			Console.ReadKey();
                    	}
                    }
                    

                    MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

                    J Offline
                    J Offline
                    J4amieC
                    wrote on last edited by
                    #9

                    Not sure what is confusing you, the implementation of Contains obviously uses the Equals object override, so sucessfully finds arg1 and arg2. However, your list is a list of object (rather than a list of bool) so when it comes to using the equality operator instead of Equals, then you're comparing object references (not values!) and clearly arg1 is not arg2.

                    J L 2 Replies Last reply
                    0
                    • G Guirec

                      Wtf? Goddamn Sh*t! I would not expect that either.... I am a young player!!

                      Seulement, dans certains cas, n'est-ce pas, on n'entend guère que ce qu'on désire entendre et ce qui vous arrange le mieux... [^]

                      J Offline
                      J Offline
                      Jorgen Andersson
                      wrote on last edited by
                      #10

                      == resolves to System.Object.ReferenceEquals while contains determines equality by using the default equality comparer, as defined by the object's implementation.

                      People say nothing is impossible, but I do nothing every day.

                      B F 2 Replies Last reply
                      0
                      • J J4amieC

                        Not sure what is confusing you, the implementation of Contains obviously uses the Equals object override, so sucessfully finds arg1 and arg2. However, your list is a list of object (rather than a list of bool) so when it comes to using the equality operator instead of Equals, then you're comparing object references (not values!) and clearly arg1 is not arg2.

                        J Offline
                        J Offline
                        Jorgen Andersson
                        wrote on last edited by
                        #11

                        I understand the difference and still I make that mistake occasionally. I guess my head isn't screwed on tightly enough.

                        People say nothing is impossible, but I do nothing every day.

                        1 Reply Last reply
                        0
                        • J J4amieC

                          Not sure what is confusing you, the implementation of Contains obviously uses the Equals object override, so sucessfully finds arg1 and arg2. However, your list is a list of object (rather than a list of bool) so when it comes to using the equality operator instead of Equals, then you're comparing object references (not values!) and clearly arg1 is not arg2.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          J4amieC wrote:

                          Not sure what is confusing you,

                          Seriously? Honestly? you can't see the confusion? Object1.equals(object2) == true; When object1 and object2 are different objects that have the same value? Assuming you didn't know what the objects were, are you telling me you honestly can't see a source of confusion?

                          MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

                          J 1 Reply Last reply
                          0
                          • L Lost User

                            J4amieC wrote:

                            Not sure what is confusing you,

                            Seriously? Honestly? you can't see the confusion? Object1.equals(object2) == true; When object1 and object2 are different objects that have the same value? Assuming you didn't know what the objects were, are you telling me you honestly can't see a source of confusion?

                            MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

                            J Offline
                            J Offline
                            J4amieC
                            wrote on last edited by
                            #13

                            _Maxxx_ wrote:

                            Object1.equals(object2) == true;
                             
                            When object1 and object2 are different objects that have the same value?

                            Exactly, the point is I understand valuetype equality semantics from referencetype equality semantics. I agree they're somewhat confusing to new programmers.

                            L 1 Reply Last reply
                            0
                            • J J4amieC

                              _Maxxx_ wrote:

                              Object1.equals(object2) == true;
                               
                              When object1 and object2 are different objects that have the same value?

                              Exactly, the point is I understand valuetype equality semantics from referencetype equality semantics. I agree they're somewhat confusing to new programmers.

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #14

                              Except object isn't a valuetype

                              MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

                              B S 2 Replies Last reply
                              0
                              • G Guirec

                                For sure! your collection is a list of objects and the arg2 object is simply not in that collection...

                                Seulement, dans certains cas, n'est-ce pas, on n'entend guère que ce qu'on désire entendre et ce qui vous arrange le mieux... [^]

                                B Offline
                                B Offline
                                BobJanova
                                wrote on last edited by
                                #15

                                bool is a value type, so it is in the collection, because false == false.

                                1 Reply Last reply
                                0
                                • L Lost User

                                  Except object isn't a valuetype

                                  MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

                                  B Offline
                                  B Offline
                                  BobJanova
                                  wrote on last edited by
                                  #16

                                  object is not a value type but the thing you've stored in the object is a value type.

                                  OriginalGriffO L 2 Replies Last reply
                                  0
                                  • J Jorgen Andersson

                                    == resolves to System.Object.ReferenceEquals while contains determines equality by using the default equality comparer, as defined by the object's implementation.

                                    People say nothing is impossible, but I do nothing every day.

                                    B Offline
                                    B Offline
                                    BobJanova
                                    wrote on last edited by
                                    #17

                                    == will resolve to ReferenceEquals for reference types, and if there is no == operator override. For a value type (like bool) it will do a content equality.

                                    J 1 Reply Last reply
                                    0
                                    • B BobJanova

                                      == will resolve to ReferenceEquals for reference types, and if there is no == operator override. For a value type (like bool) it will do a content equality.

                                      J Offline
                                      J Offline
                                      Jorgen Andersson
                                      wrote on last edited by
                                      #18

                                      Quite right. I referred to this specific case which of course wasn't obvious at all. :sigh:

                                      People say nothing is impossible, but I do nothing every day.

                                      1 Reply Last reply
                                      0
                                      • L Lost User

                                        Except object isn't a valuetype

                                        MVVM# - See how I did MVVM my way ___________________________________________ Man, you're a god. - walterhevedeich 26/05/2011 .\\axxx (That's an 'M')

                                        S Offline
                                        S Offline
                                        Sentenryu
                                        wrote on last edited by
                                        #19

                                        you clearly are forgetting about polymorphism :laugh: this is just one of the confusions he can cause... (see the << and >> operators in c++)

                                        I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)

                                        1 Reply Last reply
                                        0
                                        • B BobJanova

                                          object is not a value type but the thing you've stored in the object is a value type.

                                          OriginalGriffO Offline
                                          OriginalGriffO Offline
                                          OriginalGriff
                                          wrote on last edited by
                                          #20

                                          :thumbsup:

                                          If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

                                          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                                          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups