Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 7-Feb-13 5:50am
v2

SOLUTION 1:

In your constructor just call the function before any other work.
C#
public ups()
{
pqr();
MessageBox.Show("hello");

}


SOLUTION 2:

You are creating an object for the class ups from other forms like
C#
ups objups = new ups();

The constructor is initialized. If you want to execute pqr() first then make pqr as static
C#
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
C#
ups.pqr();
ups objups = new ups();
 
Share this answer
 
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900