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. General Programming
  3. C#
  4. C# Member initialization question

C# Member initialization question

Scheduled Pinned Locked Moved C#
questioncsharp
4 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.
  • J Offline
    J Offline
    jayart
    wrote on last edited by
    #1

    Hi All, What is difference between these two initializations of _obj: 1) public class Class1 { public Class1() { // // constructor logic here // } private Class2 _obj = null; } 2) public class Class1 { public Class1() { // // constructor logic here // _obj = null; // initialization inside constructor } private Class2 _obj = null; } Would there be difference between 1) and 2), if Class2 is a class from another referenced dll?

    L 1 Reply Last reply
    0
    • J jayart

      Hi All, What is difference between these two initializations of _obj: 1) public class Class1 { public Class1() { // // constructor logic here // } private Class2 _obj = null; } 2) public class Class1 { public Class1() { // // constructor logic here // _obj = null; // initialization inside constructor } private Class2 _obj = null; } Would there be difference between 1) and 2), if Class2 is a class from another referenced dll?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, with your code it is the same because both contain a line private Class2 _obj = null; furthermore an object reference is null by default when it is a class member. There are circumstances where there would be a difference; examples: 1) when _obj has to be initialized to something other than null, and its value is required before an object of Class1 is being instantiated (maybe its needed by some public static methods/properties). 2) when the constructor would fail (throw an exception) and hence not reach the initialization statement. :)

      Luc Pattyn [My Articles] [Forum Guidelines]

      J 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, with your code it is the same because both contain a line private Class2 _obj = null; furthermore an object reference is null by default when it is a class member. There are circumstances where there would be a difference; examples: 1) when _obj has to be initialized to something other than null, and its value is required before an object of Class1 is being instantiated (maybe its needed by some public static methods/properties). 2) when the constructor would fail (throw an exception) and hence not reach the initialization statement. :)

        Luc Pattyn [My Articles] [Forum Guidelines]

        J Offline
        J Offline
        jayart
        wrote on last edited by
        #3

        Me too thought that the initilizations in 1) and 2) were same. But I had a problem in Design mode when 2) was used. I could solve the problem when 1) was used instead. I am just too confused. I dotn find any reason why it behaves differntly. Let me explain in deatil. MyClass1 is a UserControl class. MyClass1 is to be used in frmMain (Main Form)class. MyClass2 is a class in another dll which is referenced in the project and used in MyClass1. I have two Confusions: Confusion-A) The designer of MyClass1 opens fine even if 1) or 2) is used. Confusion-B) assuming I use method 2) - When I try to add the UserControl class MyClass1 in the designer of frmMain, I get an error that it could not add the control. Mysteriously enough if I use method 1) instead, everything worked fine. Since MyClass2 is in another dll, would it initialize it differently if initialized in constructor or while declaring it? Note: Both ways the _obj is initialized as null.

        L 1 Reply Last reply
        0
        • J jayart

          Me too thought that the initilizations in 1) and 2) were same. But I had a problem in Design mode when 2) was used. I could solve the problem when 1) was used instead. I am just too confused. I dotn find any reason why it behaves differntly. Let me explain in deatil. MyClass1 is a UserControl class. MyClass1 is to be used in frmMain (Main Form)class. MyClass2 is a class in another dll which is referenced in the project and used in MyClass1. I have two Confusions: Confusion-A) The designer of MyClass1 opens fine even if 1) or 2) is used. Confusion-B) assuming I use method 2) - When I try to add the UserControl class MyClass1 in the designer of frmMain, I get an error that it could not add the control. Mysteriously enough if I use method 1) instead, everything worked fine. Since MyClass2 is in another dll, would it initialize it differently if initialized in constructor or while declaring it? Note: Both ways the _obj is initialized as null.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          ArtiGujare wrote:

          Confusion-A) The designer of MyClass1 opens fine even if 1) or 2) is used.

          That's good.

          ArtiGujare wrote:

          Confusion-B) assuming I use method 2)

          OK, I am not sure but this is what I expect is happening: - the line private Class2 _obj=null; does not do much: it reserves a 4 (or 8) byte reference which is defaulted to zero; it does not need to know what Class2 actually is, so the compiler will insist on knowing Class2, but at run-time Class2 is not needed, since there is no code corresponding to this line. - the line _obj=null; in the constructor is assigning to a variable of type Class2; at run-time this is the first time Class2 is referenced, so the JIT will try to load and compile it (although the statement does not require any knowledge of Class2, by definition this is when Class2 needs to get initialized). Now at design time (which also runs the constructor), it will fail to find the dll containing Class2 since it searches relative to the location of Visual itself, not your project. This I guess is the issue you must solve, but no I do not know how. Hope this helps. :)

          Luc Pattyn [My Articles] [Forum Guidelines]

          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