I have a function in file Code.xaml.cs.
public string send (string url)
{
//some code...
}
I want to call this function from another .cs file.
send("google.com");
But the debugger gives an error! How can I do this?
Whenever calling a dynamic method from a class, you will have to create an instance of that class.
class Test
{
public string send(string url) {}
}
class AnotherClass
{
public AnotherClass()
{
Test t = new Test();
t.send("google.com");
}
}
Otherwise you can simply use the static keyword.
public static string send(..);
If the send method isn't acting as a member function in the class that it's in, you can create a static Helper class.
public static class Helpers
{
public static string send(string url)
{
...
}
}
Then in any other .cs file you can call:
Helpers.send("www.google.com")
Related
I'm trying to use extension method, but the method is defined twice with same name.
Let's say A.Extensions.Ext() and B.Extensions.Ext() I need both references in my class and when trying
using A.Extensions;
using B.Extensions;
class C
{
public void Main()
{
somevalue.Ext();
}
}
I want to somehow define which method to use and I don't know how to do that.
Thanks for help!
There is a way to choose an extension method to be used
Assume you have two classes with extension methods for string
public static class Extension1
{
public static string GetLowerCaseResult(this string str)
{
return str.ToLowerInvariant();
}
}
public static class Extension2
{
public static string GetLowerCaseResult(this string str)
{
return str.ToLowerInvariant();
}
}
to call it in Main method you have to explicitly specify class and method in this way
static void Main(string[] args)
{
var str = "QWERTY";
Extension2.GetLowerCaseResult(str);
Console.Read();
}
I have the below code in the same namespace.
public static class usercls
{
public static int testc()
{
int s=1;
}
}
public class User : Page
{
private static User user;
int s=usercls.testc();//why not accessible here?
}
I'm unable to access the static class outside of the class. Can someone help me to identify this?
Hi the function testc() doesnt return any value.
It should look like this
public static int testc()
{
int s=1;
return s;
}
or like this
public static int testc()
{
return 1;
}
After that your code should compile.
Other classes cannot access the class functions of usercls because the compiler did not compiled it, because there was an error, once you fix that error, it will be accessible from all the other classes.
Also
You are trying to call it directly in your other class, the call should be within a function like so:
public class User : Page
{
private static User user;
public User()
{
int s=usercls.testc();//why not accessible here?
}
}
public class MyClass {
public static int SomeMethod(string param){
return .....;
}
public int SomeMethod(string param){
return MyClass.SomeMethod(param);
}
}
Here when I rename class, I should rename it in all the static method calls.
In Ruby I am able to do following:
class MyClass
def self.some_method(param)
#.....
end
def some_method(param)
self.class.some_method(param)
end
end
How to do the same in C# ?
C# doesn't have similar syntax, so you should rename all the method calls.
I've got a couple of extension functions that I want to transfer between classes.
I have a class called Helpers.cs that I want to have the following:
namespace XYZ
{
public class Helpers
{
public static string Encrypt(this string plainText){
//... do encrypting
}
}
}
In my other class Impliment.cs I want to have the following:
string s = "attack";
s.Encrypt();
How can I implement this?
You're close - extension methods need to be in a static class:
public static class Helpers
{
public static string Encrypt(this string plainText){
//... do encrypting
}
}
If you tried what you posted you'd get a pretty clear compiler error that says basically the same thing:
Extension method must be defined in a non-generic static class
Note that your usage will be slightly different that what you want. You can't do:
string s = "attack";
s.Encrypt();
becasue strings are immutable. Best you can do is overwrite the existing varialbe or store the result in a new one:
string s = "attack";
s = s.Encrypt(); // overwrite
or
string s = "attack";
string s2 = s.Encrypt(); // new variable
You need to make the class, static as well and use a using statement.
Example
FileA.cs:
namespace XYZ {
public static class Helpers {
public static string Encrypt(this string plainText){
//... do encrypting
return plainText;
}
}
}
FileB.cs:
using XYZ;
public class MainClass {
public static void Main() {
string s = "input";
s = s.Encrypt();
Console.WriteLine(s);
}
}
To create an Extension Method, the class must be static, but in general it has to follow these rules:
The class must be public and static
The method must be public and static
The type you want to create the extension method, in C#, must have the first parameter with the this keyword before the parameter
In your case, change the class to be static , for sample:
public static class Helpers
{
public static string Encrypt(this string plainText)
{
//... do encrypting
// return a string;
}
}
I also like to create the classes with name like Type and Extensions sufix, for sample: StringExtensions.cs, DateTimeExtensions.cs, etc.
Obs: Remember it is not a type method, it is just a static method, but you use as a method from a type.
I have basic knowledge of classes and methods. I can make classes, and define methods for them:
myClass.awesome("test"); (example)
But I saw a class have the following method:
anotherClass.something.methodName(arguments);
How do I create a method that has additional namespace(s). I tried:
public Class test
{
namespace subname
{
public void Test()
{
return;
}
}
//also i tried:
public void lol.Test()
{
return;
}
}
But they both say that its not to be done like that, how do it correctly, so I can order/group my methods better?
Please don't ask why, or give a alternative, I just want to make a class that has this kind of methods(Class.sub.Method() or Class.sub.sub....sub.Method())
Thank you for reading my question, and possibly giving ans answer :)
i saw a class have the following method:
anotherClass.something.methodName(arguments);
The method is not from the class anotherClass instead it is from the class of object something.
The class anotherClass has a field/property something which is of another class type, that class has the method methodName
What you think you have seen is not correct actually. It can be any of the followings:
anotherClass is namespace, something is class, methodName is static method
anotherClass is an object, something is a property of anotherClass, methodName is a method
If you want to group your methods you should consider using static classes like this:
public class Test
{
public static class InnerGroup
{
public static void Method1() { }
}
public static class AnotherInnerGroup
{
public static void Method2() { }
}
}
or class properties like this:
public class Test
{
public class InnerGroup
{
public static void Method1() { }
}
public class AnotherInnerGroup
{
public static void Method2() { }
}
public InnerGroup InnerGroup { get; set; }
public AnotherInnerGroup AnotherInnerGroup { get; set; }
public Test()
{
InnerGroup = new InnerGroup();
AnotherInnerGroup= new AnotherInnerGroup();
}
}
Hope you understood.