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. got another codebase to work on... its full of surprises!!!

got another codebase to work on... its full of surprises!!!

Scheduled Pinned Locked Moved The Weird and The Wonderful
question
18 Posts 6 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.
  • A Ankush Bansal

    public class Field
    {

    private readonly string _field;
    private readonly string _table;

    Field()
    {}

    public Field(Field s)
    {
    _table = string.Copy(s.Table);
    _field = string.Copy(s.Field);
    }

    }

    string.Copy for what?:mad:

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

    So that the immutable string value can be changed without affecting this class, obviously! Just because you make something that can't be changed readonly doesn't mean you should take chances... :laugh:

    Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

    "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

    A 1 Reply Last reply
    0
    • OriginalGriffO OriginalGriff

      So that the immutable string value can be changed without affecting this class, obviously! Just because you make something that can't be changed readonly doesn't mean you should take chances... :laugh:

      Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

      A Offline
      A Offline
      Ankush Bansal
      wrote on last edited by
      #3

      Yeah... original dev didn't took chances, but fore sure I have to. I'll kick on his a** next time i see him.

      1 Reply Last reply
      0
      • A Ankush Bansal

        public class Field
        {

        private readonly string _field;
        private readonly string _table;

        Field()
        {}

        public Field(Field s)
        {
        _table = string.Copy(s.Table);
        _field = string.Copy(s.Field);
        }

        }

        string.Copy for what?:mad:

        A Offline
        A Offline
        Ankush Bansal
        wrote on last edited by
        #4

        Someone told me that String.Copy is faster than raw assignment... So

        _field = string.Copy(s.Field);

        might actually perform better than

        _field = s.Field

        Is he crazy or am I mad to not have known this?? :~ :doh:

        1 Reply Last reply
        0
        • A Ankush Bansal

          public class Field
          {

          private readonly string _field;
          private readonly string _table;

          Field()
          {}

          public Field(Field s)
          {
          _table = string.Copy(s.Table);
          _field = string.Copy(s.Field);
          }

          }

          string.Copy for what?:mad:

          B Offline
          B Offline
          Bernhard Hiller
          wrote on last edited by
          #5

          That guy used to be a C/C++ programmer. He wants to make sure that he copies the value, instead of referencing the original value. Just think of those terrible char *. C--!

          A 1 Reply Last reply
          0
          • B Bernhard Hiller

            That guy used to be a C/C++ programmer. He wants to make sure that he copies the value, instead of referencing the original value. Just think of those terrible char *. C--!

            A Offline
            A Offline
            Ankush Bansal
            wrote on last edited by
            #6

            What if I tell you he is an experienced C# developer:cool:

            1 Reply Last reply
            0
            • A Ankush Bansal

              public class Field
              {

              private readonly string _field;
              private readonly string _table;

              Field()
              {}

              public Field(Field s)
              {
              _table = string.Copy(s.Table);
              _field = string.Copy(s.Field);
              }

              }

              string.Copy for what?:mad:

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

              Only I noted the private default constructor that make this class impossible to instantiate? or there are more constructors?

              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)

              R 1 Reply Last reply
              0
              • S Sentenryu

                Only I noted the private default constructor that make this class impossible to instantiate? or there are more constructors?

                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)

                R Offline
                R Offline
                RobCroll
                wrote on last edited by
                #8

                FYI The default accessor is internal not private.

                "You get that on the big jobs."

                S 1 Reply Last reply
                0
                • A Ankush Bansal

                  public class Field
                  {

                  private readonly string _field;
                  private readonly string _table;

                  Field()
                  {}

                  public Field(Field s)
                  {
                  _table = string.Copy(s.Table);
                  _field = string.Copy(s.Field);
                  }

                  }

                  string.Copy for what?:mad:

                  R Offline
                  R Offline
                  RobCroll
                  wrote on last edited by
                  #9

                  This has got me a little interested. By not making a copy, could that stop the GC from disposing of Field s until this instance is also ready for collection?

                  "You get that on the big jobs."

                  OriginalGriffO 1 Reply Last reply
                  0
                  • R RobCroll

                    This has got me a little interested. By not making a copy, could that stop the GC from disposing of Field s until this instance is also ready for collection?

                    "You get that on the big jobs."

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

                    No - because it creates a binary copy of the string content rather than the reference: I have no idea why MS though copying an immutable object would be a necessary thing to do, but there you go1... Copying the reference could stop the GC, provided the reference didn't get dereferenced itself. 1Thinking about it, it could be handy in two cases: if there is ever a ReferenceEquals on the strings, then the copy will be different, and if the string is passed to unmanaged code which messes it up - the later being very, very nasty though. I would hand a StringBuilder instead just for safety.

                    Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                    "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

                    S 1 Reply Last reply
                    0
                    • R RobCroll

                      FYI The default accessor is internal not private.

                      "You get that on the big jobs."

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

                      Verify again, it's private, i just tested to make sure. ALL members of a class are private for default, there is no exception to constructors, i just tested like this: My class:

                      public class Form
                      {
                          Form() {
                      
                          }
                       }
                      

                      i used a project that i was working on, so this is a MVC 3 project, my controller:

                      public class SurveyController : Controller
                      {
                          public SurveyController() {
                          
                              Form form = new Form();
                          }
                      
                      }
                      

                      this yields this compiler error:

                      Error 1 'FormularioAvaliacaoSaude.Models.Formulario.Formulario()' is inaccessible due to its protection level C:\workspace\FormularioAvaliacaoSaude\FormularioAvaliacaoSaude\Controllers\PesquisaController.cs 22 31 FormularioAvaliacaoSaude

                      (sorry for don't translating the paths on the error message, but i fell like i'll screw it up if i do it...) by this error i make the conclusion that the default Access Modifier. ... Just run into this on MSDN:

                      MSDN wrote:

                      The declaration of the empty constructor prevents the automatic generation of a default constructor. Note that if you don't use an access modifier with the constructor it will still be private by default. However, the private modifier is usually used explicitly to make it clear that the class cannot be instantiated.

                      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)

                      R 1 Reply Last reply
                      0
                      • OriginalGriffO OriginalGriff

                        No - because it creates a binary copy of the string content rather than the reference: I have no idea why MS though copying an immutable object would be a necessary thing to do, but there you go1... Copying the reference could stop the GC, provided the reference didn't get dereferenced itself. 1Thinking about it, it could be handy in two cases: if there is ever a ReferenceEquals on the strings, then the copy will be different, and if the string is passed to unmanaged code which messes it up - the later being very, very nasty though. I would hand a StringBuilder instead just for safety.

                        Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

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

                        Maybe this creates another internalized copy for the string, if that is util or not, i can't say...

                        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)

                        OriginalGriffO 1 Reply Last reply
                        0
                        • S Sentenryu

                          Maybe this creates another internalized copy for the string, if that is util or not, i can't say...

                          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)

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

                          String.Copy does create a new internal copy of the string data (rather than copy the reference as a normal string assignment would). That is precisely why the original code is so bad! It creates unnecessary copies of the string data for no obvious reason where an assignment would be faster, better for memory use, and easier to read... :laugh:

                          Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                          "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

                          S 1 Reply Last reply
                          0
                          • OriginalGriffO OriginalGriff

                            String.Copy does create a new internal copy of the string data (rather than copy the reference as a normal string assignment would). That is precisely why the original code is so bad! It creates unnecessary copies of the string data for no obvious reason where an assignment would be faster, better for memory use, and easier to read... :laugh:

                            Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

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

                            So the String.Copy() must be used if one wants to pass a string to a P/Invoke call? i don't get why this would be better than passing a StringBuilder... well, i've got to far ;P

                            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)

                            OriginalGriffO 1 Reply Last reply
                            0
                            • S Sentenryu

                              So the String.Copy() must be used if one wants to pass a string to a P/Invoke call? i don't get why this would be better than passing a StringBuilder... well, i've got to far ;P

                              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)

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

                              Gawd no! Use a StringBuilder instead! Passing an immutable object to a function known to change it is very nasty indeed (as I said earlier).

                              Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

                              "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

                              S 1 Reply Last reply
                              0
                              • OriginalGriffO OriginalGriff

                                Gawd no! Use a StringBuilder instead! Passing an immutable object to a function known to change it is very nasty indeed (as I said earlier).

                                Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

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

                                I understand... well, there are some things that are better to do not know for what are used...

                                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
                                • S Sentenryu

                                  Verify again, it's private, i just tested to make sure. ALL members of a class are private for default, there is no exception to constructors, i just tested like this: My class:

                                  public class Form
                                  {
                                      Form() {
                                  
                                      }
                                   }
                                  

                                  i used a project that i was working on, so this is a MVC 3 project, my controller:

                                  public class SurveyController : Controller
                                  {
                                      public SurveyController() {
                                      
                                          Form form = new Form();
                                      }
                                  
                                  }
                                  

                                  this yields this compiler error:

                                  Error 1 'FormularioAvaliacaoSaude.Models.Formulario.Formulario()' is inaccessible due to its protection level C:\workspace\FormularioAvaliacaoSaude\FormularioAvaliacaoSaude\Controllers\PesquisaController.cs 22 31 FormularioAvaliacaoSaude

                                  (sorry for don't translating the paths on the error message, but i fell like i'll screw it up if i do it...) by this error i make the conclusion that the default Access Modifier. ... Just run into this on MSDN:

                                  MSDN wrote:

                                  The declaration of the empty constructor prevents the automatic generation of a default constructor. Note that if you don't use an access modifier with the constructor it will still be private by default. However, the private modifier is usually used explicitly to make it clear that the class cannot be instantiated.

                                  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)

                                  R Offline
                                  R Offline
                                  RobCroll
                                  wrote on last edited by
                                  #17

                                  Sorry my bad. I thought it was internal. You are right

                                  "You get that on the big jobs."

                                  B 1 Reply Last reply
                                  0
                                  • R RobCroll

                                    Sorry my bad. I thought it was internal. You are right

                                    "You get that on the big jobs."

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

                                    In Java it is package-visible (pretty close to internal) ... and bizarrely, there is no explicit way to specify this access level!

                                    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