How to include a function call by multiple Classes 
Author Message
 How to include a function call by multiple Classes

I have some little functions wish to be shared by multiple classes and could
not figure out how.

For example, the function is like:

-----------------------------------------------------------------------
public string IntToIntString(int i)
  {

   string s;

   if ( i > 9 )
   {
    s = i.ToString();
   }
   else
   {
    s = "0" + i.ToString();
   }
   return s;
  }
----------------------------------------------------------------------------
-----

I want to call this function in more than one class and do not want to put
the above codes into every one of them.

How could I do that ?

I tried to create a new separated class:

----------------------------------------------------------------------------
using System;

namespace Utilities
{
     /// <summary>
     /// Summary description for StringFunction.
     /// </summary>
     public class StringFunctions
     {
          public StringFunctions()
          {
           //
           // TODO: Add constructor logic here
           //
           }

          public string IntToIntString(int i)
          {

           string s;

           if ( i > 9 )
           {
            s = i.ToString();
           }
           else
           {
            s = "0" + i.ToString();
           }
           return s;
          }
      }

Quote:
}

---------------------------------------------------------

But still could not easily call the function in other class  like:

  string s1 = IntToIntString(j);  // won't work.

Thanks.



Wed, 19 Jan 2005 06:02:37 GMT  
 How to include a function call by multiple Classes
You should declare yor IntToXXX function as a static method:

namespace Utilities {
  public class StringFunctions
  public static string IntToIntString(int i)
  {
  ...
  }

Quote:
}

then calling that function will look like this:

  string q = Utilities.StringFunctions.IntToIntString(i);

Hello, Sam!
You wrote  on Fri, 2 Aug 2002 15:02:37 -0700:

 S> For example, the function is like:

 S> -----------------------------------------------------------------------
 S> public string IntToIntString(int i)
 S>   {

 S>    string s;

 S>    if ( i > 9 )
 S>    {
 S>     s = i.ToString();
 S>    }
 S>    else
 S>    {
 S>     s = "0" + i.ToString();
 S>    }
 S>    return s;
 S>   }
 S> ------------------------------------------------------------------------
 S> -----

 S> I want to call this function in more than one class and do not want to
 S> put the above codes into every one of them.

 S> How could I do that ?

 S> I tried to create a new separated class:

 S> ------------------------------------------------------------------------
 S> using System;

 S> namespace Utilities
 S> {
 S>      /// <summary>
 S>      /// Summary description for StringFunction.
 S>      /// </summary>
 S>      public class StringFunctions
 S>      {
 S>           public StringFunctions()
 S>           {
 S>            //
 S>            // TODO: Add constructor logic here
 S>            //
 S>            }

 S>           public string IntToIntString(int i)
 S>           {

 S>            string s;

 S>            if ( i > 9 )
 S>            {
 S>             s = i.ToString();
 S>            }
 S>            else
 S>            {
 S>             s = "0" + i.ToString();
 S>            }
 S>            return s;
 S>           }
 S>       }
 S> }
 S> ---------------------------------------------------------

 S> But still could not easily call the function in other class  like:

 S>   string s1 = IntToIntString(j);  // won't work.




Wed, 19 Jan 2005 06:35:07 GMT  
 How to include a function call by multiple Classes
Incidentally you might want to collapse that function to:

return string.Format("{0:00}", i);


Quote:
> You should declare yor IntToXXX function as a static method:

> namespace Utilities {
>   public class StringFunctions
>   public static string IntToIntString(int i)
>   {
>   ...
>   }
> }

> then calling that function will look like this:

>   string q = Utilities.StringFunctions.IntToIntString(i);

> Hello, Sam!
> You wrote  on Fri, 2 Aug 2002 15:02:37 -0700:

>  S> For example, the function is like:

 S> -----------------------------------------------------------------------

- Show quoted text -

Quote:
>  S> public string IntToIntString(int i)
>  S>   {

>  S>    string s;

>  S>    if ( i > 9 )
>  S>    {
>  S>     s = i.ToString();
>  S>    }
>  S>    else
>  S>    {
>  S>     s = "0" + i.ToString();
>  S>    }
>  S>    return s;
>  S>   }

 S> ------------------------------------------------------------------------
Quote:
>  S> -----

>  S> I want to call this function in more than one class and do not want to
>  S> put the above codes into every one of them.

>  S> How could I do that ?

>  S> I tried to create a new separated class:

 S> ------------------------------------------------------------------------

- Show quoted text -

Quote:
>  S> using System;

>  S> namespace Utilities
>  S> {
>  S>      /// <summary>
>  S>      /// Summary description for StringFunction.
>  S>      /// </summary>
>  S>      public class StringFunctions
>  S>      {
>  S>           public StringFunctions()
>  S>           {
>  S>            //
>  S>            // TODO: Add constructor logic here
>  S>            //
>  S>            }

>  S>           public string IntToIntString(int i)
>  S>           {

>  S>            string s;

>  S>            if ( i > 9 )
>  S>            {
>  S>             s = i.ToString();
>  S>            }
>  S>            else
>  S>            {
>  S>             s = "0" + i.ToString();
>  S>            }
>  S>            return s;
>  S>           }
>  S>       }
>  S> }
>  S> ---------------------------------------------------------

>  S> But still could not easily call the function in other class  like:

>  S>   string s1 = IntToIntString(j);  // won't work.





Fri, 21 Jan 2005 19:18:53 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. destructor is called before copy-constructor on temporary class (allocated on stack during function call)

2. newbie One class calling functions in another class

3. Inherited class function calls from a pointer assigned to the base class

4. calling multiple base constructors in my derived class construtors

5. question about including class and function?

6. What does Standartd say about multiple function calling?

7. style question: multiple function calls in if expression

8. Calling multiple functions in a mac

9. Calling multiple functions in a macro.

10. Create a thread to call a function taking multiple arguments

11. Multiple inheritance of abstract classes with same member functions

12. Multiple inheritance of abstract classes with same member functions

 

 
Powered by phpBB® Forum Software