Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Return a class instance from a static method?

Former Member
0 Likes
1,927

Hi All,

I have a question about OOP Abap;

How can i write (best way) inner of ZCML_USER=>LOGIN method for using like at  doLogin method.

(There is a c# example under the Abap codes )

Thanks.

ABAP USER CLASS


class ZCMCL_USER definition

   public

   final

   create public .

public section.

   class-methods LOGIN

     importing

       value(USERNAME) type ZCM_DUS_USERNAME

       value(PASSWORD) type ZCM_DUS_PASSWORD

     returning

       value(STATUS) type ref to ZCMCL_USER .

ABAP - STATIC METHOD OF USER CLASS


method LOGIN.

  

   ????????

  

endmethod.

ABAP - CALLING STATIC USER METHOD


method DOLOGIN.

   DATA theUser  TYPE REF TO ZCMCL_USER.

   theUser = ZCMCL_USER=>LOGIN( USERNAME = 'kdr'  password = '123' ).

   endmethod.

C# EXAMPLE

C# - USER CLASS


    public class User

    {

        public int Id { get; set; }

        public string Name { get; set; }

        public string Surname { get; set; }

        public static User Login(string username, string password)

        {

            //if.....

            User usr = new User();

            usr.Name = "Ahmet";

            usr.Surname = "AKIN";

            usr.Id = 1;

            return usr;

        }

    }

C# - CALLING STATIC USER METHOD


    public void doLogin()

    {

        User theUser = User.Login("ahmt", "123456");

        Console.WriteLine(theUser.Name);

    }

1 ACCEPTED SOLUTION
Read only

SharathYaralkattimath
Contributor
0 Likes
1,069


ABAP USER CLASS

  1. class ZCMCL_USER definition 
  2.    public 
  3.    final 
  4.    create public
  5. public section. 
  6.    METHODS CONSTRUCTOR importing
  7.         value(USERNAME) type ZCM_DUS_USERNAME 
  8.          value(PASSWORD) type ZCM_DUS_PASSWORD. 
  9.    class-methods LOGIN 
  10.      importing 
  11.        value(USERNAME) type ZCM_DUS_USERNAME 
  12.        value(PASSWORD) type ZCM_DUS_PASSWORD 
  13.      returning 
  14.        value(STATUS) type ref to ZCMCL_USER . 

ABAP - STATIC METHOD OF USER CLASS

    1. method LOGIN. 
    2.    
    3.    CREATE OBJECT status
    4.        EXPORTING
    5.             username = username
    6.             password = password.
    7.     
    8. endmethod. 

    9. method CONSTRCUTOR.
    10.          "Copy the importing parameters to Class variables so that it could be used in other methods
    11. ENDMETHOD.

    Thanks,

    Sharath

    2 REPLIES 2
    Read only

    SharathYaralkattimath
    Contributor
    0 Likes
    1,070


    ABAP USER CLASS

    1. class ZCMCL_USER definition 
    2.    public 
    3.    final 
    4.    create public
    5. public section. 
    6.    METHODS CONSTRUCTOR importing
    7.         value(USERNAME) type ZCM_DUS_USERNAME 
    8.          value(PASSWORD) type ZCM_DUS_PASSWORD. 
    9.    class-methods LOGIN 
    10.      importing 
    11.        value(USERNAME) type ZCM_DUS_USERNAME 
    12.        value(PASSWORD) type ZCM_DUS_PASSWORD 
    13.      returning 
    14.        value(STATUS) type ref to ZCMCL_USER . 

    ABAP - STATIC METHOD OF USER CLASS

      1. method LOGIN. 
      2.    
      3.    CREATE OBJECT status
      4.        EXPORTING
      5.             username = username
      6.             password = password.
      7.     
      8. endmethod. 

      9. method CONSTRCUTOR.
      10.          "Copy the importing parameters to Class variables so that it could be used in other methods
      11. ENDMETHOD.

      Thanks,

      Sharath

      Read only

      indra_dewaji
      Explorer
      1,069

      Hi Abdulkadir,

      I believe you want to create a singleton class which can be accessible for all other places during runtime.

      First you can create a singleton class. Here the sample code:

      class yide_cl_fpm_singleton definition

        public

        final

        create public .

      public section.

        data:

          username type sy-uname.

        class-methods get_instance

          returning

            value(re_instance) type ref to yide_cl_fpm_singleton .

      protected section.

      private section.

        class-data mo_instance type ref to yide_cl_fpm_singleton .

      endclass.

      private section.

         class-data mo_instance type ref to yide_cl_gaf_singleton .

      method get_instance.

         if mo_instance is initial.
           create object mo_instance.
         endif.

         re_instance = mo_instance.

      endmethod.

      And then, in your program, you can call this singleton class by calling as static method get_instance. Once your instance has been instantiated, you can access your public data from this class. You can get or set via this singleton class.

      Hope it's fit to your requirement.

      Regards,

      Indra