-
Mar 29th, 2019, 08:18 AM
#1
Thread Starter
Member
[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.
-
Mar 29th, 2019, 08:37 AM
#2
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:
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.
-
Mar 29th, 2019, 08:52 AM
#3
Thread Starter
Member
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
-
Mar 29th, 2019, 09:24 AM
#4
Re: How to call a static method from a different class?
Are they in the same project or different projects?
-
Mar 29th, 2019, 09:45 AM
#5
Thread Starter
Member
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
-
Mar 29th, 2019, 09:59 AM
#6
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:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Class1 { public static string GetString() { return string.Empty; } } }
Everything there is default added by VS except the method. I then called that method in the default Program class like so:
csharp Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { var str = Class1.GetString(); } } }
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.
-
Mar 29th, 2019, 10:05 AM
#7
Thread Starter
Member
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
-
Mar 31st, 2019, 12:13 PM
#8
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|