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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. dependant #warning

dependant #warning

Scheduled Pinned Locked Moved C#
data-structuresperformancehelpquestion
5 Posts 2 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.
  • R Offline
    R Offline
    Rafael Fernandez Lopez
    wrote on last edited by
    #1

    Hi, I've got a generic class (Binary Tree) public class ABB : ICloneable where T : IComparable/*, ICloneable*/ { . . . } I adapted it so items that are not ICloneable can be introduced into the tree, but if they are not ICloneable and they are not like (int, float...), because is another class that is not ICloneable, I'd like to print a: #warning If the object is modified after introducing it into the tree, the object that the tree has saved will be changed too (if T is not a primitive type). If you wan't to solve this, make the class T implement ICloneable methods, and a copy of the original object will be saved into the tree, instead of copying its memory address (as is happening right now). But this warning only MUST BE SHOWED if the class T that has been instantiated like: ABB object = new ABB(); Well, it would be showed with old types too (int, float...) but there's no problem since it is a copy of the original type what is introduced onto the tree (as the warning says). As this: ABB object = new ABB(); Well, int is not ICloneable, but there's no problem with it. THANK YOU.

    J 1 Reply Last reply
    0
    • R Rafael Fernandez Lopez

      Hi, I've got a generic class (Binary Tree) public class ABB : ICloneable where T : IComparable/*, ICloneable*/ { . . . } I adapted it so items that are not ICloneable can be introduced into the tree, but if they are not ICloneable and they are not like (int, float...), because is another class that is not ICloneable, I'd like to print a: #warning If the object is modified after introducing it into the tree, the object that the tree has saved will be changed too (if T is not a primitive type). If you wan't to solve this, make the class T implement ICloneable methods, and a copy of the original object will be saved into the tree, instead of copying its memory address (as is happening right now). But this warning only MUST BE SHOWED if the class T that has been instantiated like: ABB object = new ABB(); Well, it would be showed with old types too (int, float...) but there's no problem since it is a copy of the original type what is introduced onto the tree (as the warning says). As this: ABB object = new ABB(); Well, int is not ICloneable, but there's no problem with it. THANK YOU.

      J Offline
      J Offline
      Josh Smith
      wrote on last edited by
      #2

      I don'think that the preprocessing facility in the C# IDE is flexible enough to satisfy your requirements. In the class's constructor you could test the runtime type of T, and if it falls into the category which requires a warning, use Debug.WriteLine to specify the warning. An advantage of this technique is that the developer's output window in VS won't be polluted with your warning message(s) on each build. I like a nice clean build - 0 errors, 0 warnings :) Josh

      R 1 Reply Last reply
      0
      • J Josh Smith

        I don'think that the preprocessing facility in the C# IDE is flexible enough to satisfy your requirements. In the class's constructor you could test the runtime type of T, and if it falls into the category which requires a warning, use Debug.WriteLine to specify the warning. An advantage of this technique is that the developer's output window in VS won't be polluted with your warning message(s) on each build. I like a nice clean build - 0 errors, 0 warnings :) Josh

        R Offline
        R Offline
        Rafael Fernandez Lopez
        wrote on last edited by
        #3

        Well, a clean output for clean designs. If your design (in this case, MY DESIGN) has troubles (if the class is not ICloneable), and the object that you think that is for sure can be modified by side-effects (if I wasn't able to call Clone(), then I copied the memory address), there must be a WARNING to let the programmer know that he has to be *VERY CAREFUL* with the original object, or make that class ICloneable, so warning message *WILL DISSAPEAR*. Well, if he does well, there will be 0 errors, 0 warnings. ;) Cheers !!


        Written by: Rafael Fernández López.

        Visit: http://www.maestroprogramador.com

        J 1 Reply Last reply
        0
        • R Rafael Fernandez Lopez

          Well, a clean output for clean designs. If your design (in this case, MY DESIGN) has troubles (if the class is not ICloneable), and the object that you think that is for sure can be modified by side-effects (if I wasn't able to call Clone(), then I copied the memory address), there must be a WARNING to let the programmer know that he has to be *VERY CAREFUL* with the original object, or make that class ICloneable, so warning message *WILL DISSAPEAR*. Well, if he does well, there will be 0 errors, 0 warnings. ;) Cheers !!


          Written by: Rafael Fernández López.

          Visit: http://www.maestroprogramador.com

          J Offline
          J Offline
          Josh Smith
          wrote on last edited by
          #4

          What does this have to do with your original question? Keep in mind that implementing ICloneable is not always an option. If your type contains a reference to another type which is not cloneable, then your type cannot provide true deep copy semantics. In that case, your annoying warning message will not disappear. Josh

          R 1 Reply Last reply
          0
          • J Josh Smith

            What does this have to do with your original question? Keep in mind that implementing ICloneable is not always an option. If your type contains a reference to another type which is not cloneable, then your type cannot provide true deep copy semantics. In that case, your annoying warning message will not disappear. Josh

            R Offline
            R Offline
            Rafael Fernandez Lopez
            wrote on last edited by
            #5

            You're just right. In that case the #warning message wouldn't be showed, and my class would't be able to provide a true deep copy, but in that case, and only in that case, the problem (the main trouble) would be that the developer that wrote it was wrong, because didn't think about implementing Clone() method to provide a deep copy. We sometimes just have to trust in developers that are gonna use our classes, so we cannot do more than we are doing right now. Far away, you're right, my warning would not appear when compiling. Thank you for your comments Josh. ;)


            Written by: Rafael Fernández López.

            Visit: http://www.maestroprogramador.com

            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