Results 1 to 8 of 8

Thread: [RESOLVED] How to call a static method from a different class?

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2018
    Posts
    42

    Resolved [RESOLVED] How to call a static method from a different class?

    Hi all,

    I've in a proyect a class called LibSTR that holds static functions that performs certain operations to a string. For example:

    Code:
    namespace MyProyect
    {
        class LibSTR
        {
            public static string DoSomethingWithThisStringThenReturn(string MyString)
            {
                MyString = MyString.ToUpper(); //it's just a code for this post, I want to perform any operation.
                return MyString;
            }
        }
    }
    From another function in the same class LibSTR I can access to it with:

    Code:
    ProcessedString = LibSTR.DoSomethingWithThisStringThenReturn(MyString);
    I also can do it in the onclick event of a button. The problem is I have a different class, and from this class I cant use DoSomethingWithThisStringThenReturn unless I write:

    Code:
    ProcessedString = MyProyect.LibSTR.DoSomethingWithThisStringThenReturn(MyString);
    I want to be able to access this function without having to do it, if later I change the name of the proyect or simply copy de class LibSTR to other proyect it will not work. Anyway, its annoying if I have to write proyect name dot class name everytime I want to use a function from this class.

    I've try to replace "class LibSTR" to "public class LibSTR" and "public static class LibSTR" but nothing changes.
    Any help?

    Many thanks
    Last edited by TassadarNET; Mar 29th, 2019 at 08:35 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: How to call a static method from a different class?

    You ALWAYS have to specify the namespace of any type you refer to one way or another. If the type you're writing the code in and the type you're referring to are in the same namespace then it is implicit but it's still there. If they are in different namespaces then you have two choices: you can either qualify the type name where you use it, as you have show above, or you can add a using directive to the top of the code file to import the namespace and then use the type unqualified. You already use that second option already. For instance, if you refer to the TextBox or Button types in a Windows Forms project, you will find that your code file has:
    csharp Code:
    1. using System.Windows.Forms;
    at the top. Without that, you'd have to use System.Windows.Forms.TextBox and System.Windows.Forms.Button in code.

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2018
    Posts
    42

    Re: How to call a static method from a different class?

    Manty thanks, jmcilhinney

    I've add "using MyProyect;" to the class I want to use those static funtions and now I can use:

    Code:
    ProcessedString = LibSTR.DoSomethingWithThisStringThenReturn(MyString);
    Question is.... it seems normal to me if the class LibSTR were in a different namespace, but the thing is that both classes belongs to namespace MyProyect.

    I mean, it's a bit stupid having yo use "using MyProyect;" in the classes of MyProyect, isn't it? I thought those classes should be availables by default since its the same namespace xD

    Regards

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: How to call a static method from a different class?

    Are they in the same project or different projects?

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2018
    Posts
    42

    Re: How to call a static method from a different class?

    That's the question, both classes are on the same proyect xDD

    To be honest, include the "using MyProyect;" on all the classes is not a big problem, but it seems very weird to me.

    Regards

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: How to call a static method from a different class?

    Unless your VS installation is broken, there's something else going on that you're not telling us about. I just created new C# Console Application project and added the following class to it:
    csharp Code:
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6.  
    7. namespace ConsoleApp1
    8. {
    9.     class Class1
    10.     {
    11.         public static string GetString()
    12.         {
    13.             return string.Empty;
    14.         }
    15.     }
    16. }
    Everything there is default added by VS except the method. I then called that method in the default Program class like so:
    csharp Code:
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6.  
    7. namespace ConsoleApp1
    8. {
    9.     class Program
    10.     {
    11.         static void Main(string[] args)
    12.         {
    13.             var str = Class1.GetString();
    14.         }
    15.     }
    16. }
    As you can see, there was no need to import the namespace. I suggest that you try the same thing and see whether it works the same way for you. If it does then there is something else different about your current project. Either something is broken or there's something about the project structure that you don't understand.

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2018
    Posts
    42

    Re: How to call a static method from a different class?

    Thanks again,

    Something must be broken in my VS. The name of the proyect is not MyProyect (I used this to make the post easier to read) but appart of this, it's just as I've told, I'm hiding nothing, hehe.

    At this moment I've to out, when I'm back I'll try in a new proyect and post result.

    Regards

  8. #8

    Thread Starter
    Member
    Join Date
    Jul 2018
    Posts
    42

    Re: How to call a static method from a different class?

    Problem found!!

    Moving my code to a new proyect I've see the problem: In the LibSTR Class by mistake I had delete the line "namespace MyProyect", the class existed but it didn't belong to the namespace despite exist in the proyect MyPeoyect.

    Regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width