Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
public class ups
   {
       int i, k;
       public ups()
       {
           MessageBox.Show("hello");
 
       }
 
       public void pqr()
       {
           MessageBox.Show("Bye");
       }
   }
 
How can it possible that in above code instance method( pqr() ) get executed first then constructor ( ups() ).
 
plz suggest me asap.
Posted 7-Feb-13 6:40am
Edited 7-Feb-13 6:50am
ProgramFOX132.5K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

SOLUTION 1:
 
In your constructor just call the function before any other work.
public ups()
{
pqr();
MessageBox.Show("hello");
 
}
 
SOLUTION 2:
 
You are creating an object for the class ups from other forms like
ups objups = new ups();
The constructor is initialized. If you want to execute pqr() first then make pqr as static
public static void pqr()
{
MessageBox.Show("Bye");
}
and now you can do it this way. Class does not have to be intialized for this method
ups.pqr();
ups objups = new ups();
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

not possible. Because when we creating the object memory is allotted with the help of new keyword and constructor. Without these two how we cannot allot memory. With out allotting to call a non static function is not possible.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 420
1 OriginalGriff 230
2 DamithSL 135
3 Dave Kreskowiak 110
4 Suvendu Shekhar Giri 110
0 OriginalGriff 7,740
1 DamithSL 5,644
2 Sergey Alexandrovich Kryukov 5,404
3 Maciej Los 5,011
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 7 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100