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 Lost User

    LOL - I am genuinely crying (with a little laughter and a lot of WTF!)

    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
    Jorgen Andersson
    wrote on last edited by
    #41

    Just pulling your leg a little bit. Shorthand for ReferenceEquals in VB.Net is IS. = is the shorthand for Equals. So the VB code wasn't the same as your C# code. :) I'v seen this error a few times in code that's been converted between VB and C#. C# == is not the same as VB =, but oh so easy to miss if you only know one of the languages. Should add that this is a standard error in all automatic converters I've tried.

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

    S 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')

      T Offline
      T Offline
      TheCoolCoder
      wrote on last edited by
      #42

      I used this article for reference: link

      object arg1 = true;
      object arg2 = true;
      List<object> collection = new List<object>() { arg1 };

      The keyword object doesnt make 'arg1' or 'arg2' a reference type, they are both value types. And as the article clearly states "If the current instance is a value type, the Equals(Object) method tests for value equality. Value equality means the following: 1.The two objects are of the same type. 2.The values of the public and private fields of the two objects are equal." when checking

              bool first = (collection.Contains(arg1));
              bool second = (collection.Contains(arg2));
      

      as both the conditions are satisfied obviously 'Contains' will return true. As i understand this is exactly the same as doing

              int a = 1;
              int b = 1;
      
              List intcollection = new List() {a };
              bool first = (intcollection.Contains(a));
              bool second = (intcollection.Contains(b));
              Console.WriteLine(string.Format("{0} {1}", first, second));
      

      in which case we all agree that 'True True' is the output. I think the confusion occurs because of the tendency to assume object arg1 and object arg2 as reference types. I understand this can cause confusion and bigger underlying problems in a framework scenario but the only reason(AFAIK) causing this is whoever coded it dint realize this beforehand. but i appreciate this post as I know I would never have come across such a ascenario in years.Please feel free to correct me if I am wrong as I am one of the young players only :-).

      L 1 Reply Last reply
      0
      • J Jorgen Andersson

        Just pulling your leg a little bit. Shorthand for ReferenceEquals in VB.Net is IS. = is the shorthand for Equals. So the VB code wasn't the same as your C# code. :) I'v seen this error a few times in code that's been converted between VB and C#. C# == is not the same as VB =, but oh so easy to miss if you only know one of the languages. Should add that this is a standard error in all automatic converters I've tried.

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

        S Offline
        S Offline
        Stefan_Lang
        wrote on last edited by
        #43

        And then people complain about the confusing use of pointers and pointees in C/C++ ...

        J 1 Reply Last reply
        0
        • S Stefan_Lang

          And then people complain about the confusing use of pointers and pointees in C/C++ ...

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

          Pointers and Pointees have a point, but in 95% of the cases the compiler would do a better job in handling it for you, and you can concentrate on the programming instead. Just my 2c.

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

          S 1 Reply Last reply
          0
          • T TheCoolCoder

            I used this article for reference: link

            object arg1 = true;
            object arg2 = true;
            List<object> collection = new List<object>() { arg1 };

            The keyword object doesnt make 'arg1' or 'arg2' a reference type, they are both value types. And as the article clearly states "If the current instance is a value type, the Equals(Object) method tests for value equality. Value equality means the following: 1.The two objects are of the same type. 2.The values of the public and private fields of the two objects are equal." when checking

                    bool first = (collection.Contains(arg1));
                    bool second = (collection.Contains(arg2));
            

            as both the conditions are satisfied obviously 'Contains' will return true. As i understand this is exactly the same as doing

                    int a = 1;
                    int b = 1;
            
                    List intcollection = new List() {a };
                    bool first = (intcollection.Contains(a));
                    bool second = (intcollection.Contains(b));
                    Console.WriteLine(string.Format("{0} {1}", first, second));
            

            in which case we all agree that 'True True' is the output. I think the confusion occurs because of the tendency to assume object arg1 and object arg2 as reference types. I understand this can cause confusion and bigger underlying problems in a framework scenario but the only reason(AFAIK) causing this is whoever coded it dint realize this beforehand. but i appreciate this post as I know I would never have come across such a ascenario in years.Please feel free to correct me if I am wrong as I am one of the young players only :-).

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

            TheCoolCoder wrote:

            whoever coded it dint realize this beforehand.

            in fact, when the framework was written, I think there were only a very limited number of objects that were intended to be passed - and all of these would have been reference objects. So, not necessarily that he didn't realise it, but more that he didn't consider anyone might try to pass a bunch of booleans!

            TheCoolCoder wrote:

            the tendency to assume object arg1 and object arg2 as reference types.

            Spot on! Looked at in isolation, that is exactly what most people assume (myself included!). This would be a great example of where TDD would possibly have been useful - as in setting up tests with a variety of parameter types, this issue may have been spotted before going into production!

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

            1 Reply Last reply
            0
            • J Jorgen Andersson

              Pointers and Pointees have a point, but in 95% of the cases the compiler would do a better job in handling it for you, and you can concentrate on the programming instead. Just my 2c.

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

              S Offline
              S Offline
              Stefan_Lang
              wrote on last edited by
              #46

              I didn't intend to turn this into a C++ vs C# discussion, just point out that this specific case is just as confusing* as pointer use, which happens to be one of the stronger arguments against using C/C++. I'm not saying this is a common problem in C#, just gloating over the realization that you cannot completely avoid the heritage. ;P

              J 1 Reply Last reply
              0
              • S Stefan_Lang

                I didn't intend to turn this into a C++ vs C# discussion, just point out that this specific case is just as confusing* as pointer use, which happens to be one of the stronger arguments against using C/C++. I'm not saying this is a common problem in C#, just gloating over the realization that you cannot completely avoid the heritage. ;P

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

                I we would make a list of confusing parts of programming languages, any language, we would run out of harddisk space pretty soon. :-)

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

                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')

                  S Offline
                  S Offline
                  Super Lloyd
                  wrote on last edited by
                  #48

                  2 questions: 1. what is the problem, nothing really surprising in your program behaviors... (except, maybe newbie might be surprised by "((object)"A") != "A"") 2. your program doesn't seem to match your 1 line English sentence above?!?

                  A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

                  L 1 Reply Last reply
                  0
                  • S Super Lloyd

                    2 questions: 1. what is the problem, nothing really surprising in your program behaviors... (except, maybe newbie might be surprised by "((object)"A") != "A"") 2. your program doesn't seem to match your 1 line English sentence above?!?

                    A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

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

                    2 answers 1. The problem is that it is easy to be misled about what is happening in this sort of circumstance; Bearing in mind that, in the real world, the 'contains' code would be in a method that simply has a collection of objects as its parameter (so it's not obvious what values may be in those objects). If you are just saying that you would never be fooled by something like 'contains' finding some objects and not others, then I bow to your superior intellect. Oh, and the newbies being surprised thing is just off topic. I am certain that many a newbie (and quite a few oldies)_ would be fooled by the 'contains' issue with a mixture of objects, some of which are value and some reference, ESPECIALLY as the issue (in the case identified in some of my other responses) rears its head only when the values of two arguments happens to be equal - which leads to an irregular failure of a part of the framework. 2. Are you referring to 'a trick for young players'? If so, maybe this is an Aussie phrase. It means 'something to beware of for the inexperienced'

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

                    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