DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    manish Guest

    How to pass variable number of argumants to "method"(not main())


    hi
    i am devloping a program in which i have to pass variable number of arguments
    to the method defined in other class how would i do that
    for example,
    public class myClass{
    public static void main(){
    OtherClass oClass = new OtherClass();
    /**
    i want exectly the same implemantation
    as shown below
    */
    oClass.otherMethod("arg1","arg2","arg3"); //all the arguments
    oClass.otherMethod("arg4"); //are String
    }//!myClass

    //this class is defined in other file and dosnt have
    // main() method

    public class OtherClass{

    //what should i put as the arguments the otherMethod takes
    public otherMethod(?????????){....implemantation...}

    } //!OtherClass



  2. #2
    Paul Clapham Guest

    Re: How to pass variable number of argumants to "method"(not main())


    manish <mbd72@hotmail.com> wrote in message news:38db7cd1@news.devx.com...
    >
    > hi
    > i am devloping a program in which i have to pass variable number of

    arguments
    > to the method defined in other class how would i do that
    > for example,
    > public class myClass{
    > public static void main(){
    > OtherClass oClass = new OtherClass();
    > /**
    > i want exectly the same implemantation
    > as shown below
    > */
    > oClass.otherMethod("arg1","arg2","arg3"); file://all the arguments
    > oClass.otherMethod("arg4"); file://are String
    > }//!myClass
    >
    > file://this class is defined in other file and dosnt have
    > // main() method
    >
    > public class OtherClass{
    >
    > file://what should i put as the arguments the otherMethod takes
    > public otherMethod(?????????){....implemantation...}
    >
    > } file://!OtherClass
    >
    >


    public class OtherClass {
    public void otherMethod(String a, String b, String c)
    {...code for 3 arguments here...};
    public void otherMethod(String a)
    {...code for 1 argument here...};
    }

    will solve your problem if you only want to pass 1 or 3 strings as in the
    example. If you want to pass any number of strings, pass an array of
    strings.

    public class OtherClass {
    public void otherMethod(String[] a)
    {...code for n arguments here...};
    }




  3. #3
    manish Guest

    Re: How to pass variable number of argumants to "method"(not main())


    hi
    the first ex dosent work because i dont know the arguments
    it might be 1,2,3,...so on.

    in the second code i have to pass the arg. as as shown below

    oClass.otherMethod(new String []{"arg1","arg2","arg3"});
    but i dont want to do that i want to pass the arguments as
    oClass.otherMethod("arg1","arg2","arg3");

    is it possible

    thanks for your help
    manish


    "Paul Clapham" <pclapham@core-mark.com> wrote:
    >
    >manish <mbd72@hotmail.com> wrote in message news:38db7cd1@news.devx.com...
    >>
    >> hi
    >> i am devloping a program in which i have to pass variable number of

    >arguments
    >> to the method defined in other class how would i do that
    >> for example,
    >> public class myClass{
    >> public static void main(){
    >> OtherClass oClass = new OtherClass();
    >> /**
    >> i want exectly the same implemantation
    >> as shown below
    >> */
    >> oClass.otherMethod("arg1","arg2","arg3"); file://all the arguments
    >> oClass.otherMethod("arg4"); file://are String
    >> }//!myClass
    >>
    >> file://this class is defined in other file and dosnt have
    >> // main() method
    >>
    >> public class OtherClass{
    >>
    >> file://what should i put as the arguments the otherMethod takes
    >> public otherMethod(?????????){....implemantation...}
    >>
    >> } file://!OtherClass
    >>
    >>

    >
    >public class OtherClass {
    > public void otherMethod(String a, String b, String c)
    > {...code for 3 arguments here...};
    > public void otherMethod(String a)
    > {...code for 1 argument here...};
    >}
    >
    >will solve your problem if you only want to pass 1 or 3 strings as in the
    >example. If you want to pass any number of strings, pass an array of
    >strings.
    >
    >public class OtherClass {
    > public void otherMethod(String[] a)
    > {...code for n arguments here...};
    >}
    >
    >
    >



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links