Need help In Object Creation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NagarajanS
    New Member
    • Dec 2006
    • 39

    Need help In Object Creation

    Hi,
    I am developing some windows application tool.Here my doubt is,
    I have some 3 classes in the project,
    class one
    {
    Public string getName()
    {
    return "Nags"
    }
    }

    class three
    {
    public static object object1;
    }
    method one:
    -----------------
    class two
    {
    [STAThread]
    static void Main()
    {
    one obj=new one()
    MessageBox.Show (obj.getName);
    }
    }
    methos 2:
    ---------------
    class two
    {
    [STAThread]
    static void Main()
    {
    two obj= (two)three.obje ct1;
    MessageBox.Show (obj.getName);
    }
    }
    In the above two methods which one is better?and tel me why?
    if second one is better means what about the cpu usage(when i go for more objects)

    Regards
    Nags
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I don't believe that code would work, regardless of which one is better.

    Comment

    • NagarajanS
      New Member
      • Dec 2006
      • 39

      #3
      ok here is the complete tested code,

      private void Form1_Load(obje ct sender, EventArgs e)
      {
      --------------------------------------------------------------------
      method1:
      Class2 class2 = new Class2();

      MessageBox.Show (class2.GetName );
      ---------------------------------------------------------------------------
      method2:
      Class3.object1 = new Class2();
      Class2 newobj = (Class2)Class3. object1;
      MessageBox.Show (newobj.GetName );

      }
      class2:
      ----------
      class Class2
      {
      public string GetName
      {
      get
      {
      return "nags";
      }
      }
      }
      class3:
      -----------
      class Class3
      {
      public static object object1;
      }

      This is working fine.here i have given simple example ithink you will understand this time better.
      Regards,
      Nags.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Seems to me that method 2 is worse since it's doing all that extra work of casting things as objects.

        Comment

        • NagarajanS
          New Member
          • Dec 2006
          • 39

          #5
          Thanks for your reply.Ok if i am going for the Class4
          Class4
          {
          //want to access the Class2 data
          Step1
          -------
          Create object using new Class2();
          Step2:
          ---------
          Using the above method2
          }

          Now what will the issue?which one better method now?which one affect my performence?and why:
          Regards,
          Nags.

          Comment

          Working...