Share via

Namespace problem

BenTam-3003 686 Reputation points
Mar 9, 2022, 7:26 AM

Dear All,

I create a function "String()" and put it into a separate file "library.cs" and assign the same "namespace NewTims" to it. However, if I paste it into the same page where the "String()" function resides, it works.

Library.cs
181343-library.gif

Error
181276-namespace.gif

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,343 questions
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 47,781 Reputation points
    Mar 9, 2022, 8:01 AM

    The String method is a member of the Library class. To use the method without creating an instance of the Library class make it a static method. Then, you can call it within the FillDataListView method -- Library.String(" ", 100)


1 additional answer

Sort by: Most helpful
  1. Sreeju Nair 12,626 Reputation points
    Mar 9, 2022, 11:11 AM

    String method you defined is a member function in the Library class. So you need to create the instance of Library class to call the String method.

    Library lib = new Library();
    lib.String(................);
    

    Since the method you used is not dependent on any property of Library class, making it a static function will simplify the function call.

    public static string String(.......)
    {
    
    }
    

    Now you can clall this method as below.

    Library.String(..........);
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.