I'm trying to run a method created in a different class using the "public" access modifier. It seems not to be working like fields. Here's a simple example, could you lead me to a solution?
Thank you.
class Program
{
void Main(string[] args)
{
Hello();
}
}
class Example
{
public void Hello() //Error?
{
Console.WriteLine("Hello world");
}
}
You need to create an instance of the 'Example' class in able to call it's 'Hello' method:
class Program
{
void Main(string[] args)
{
var example = new Example();
example.Hello();
}
}
class Example
{
public void Hello() //Error?
{
Console.WriteLine("Hello world");
}
}
Or: make the Hello method static:
class Program
{
void Main(string[] args)
{
Example.Hello();
}
}
class Example
{
public static void Hello()
{
Console.WriteLine("Hello world");
}
}
just as an example: you need to instantiate an object of the class first to be able to access its members (unless they are static):
class Program
{
void Main(string[] args)
{
Example ex = new Example();
ex.Hello();
}
}
class Example
{
public void Hello()
{
Console.WriteLine("Hello world");
}
}
Related
class Program
{
static void Main(string[] args)
{
//I want to call the bird method here
}
public void bird(Program program)
{
birdSpeech();
}
public void birdSpeech()
{
Console.WriteLine("Chirp Chirp");
}
}
How do I call bird in the main, I also tried to call the method from a object of the class but that didn't work
Does it even make sense to do this (I try to avoid static methods as much as possible).
If a method can be made static without changing the code, you should make it static. To answer your question though, you can create an instance of Program in your Main function and call the non-static methods through it.
class Program
{
static void Main(string[] args)
{
var p = new Program();
p.bird();
}
public void bird()
{
birdSpeech();
}
public void birdSpeech()
{
Console.WriteLine("Chirp Chirp");
}
}
Could do something like this
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var p = new Program();
p.bird(p);
}
public void bird(Program program)
{
birdSpeech();
}
public void birdSpeech()
{
Console.WriteLine("Chirp Chirp");
}
}
Asking the same question as
Using delegates with non static methods [no picked answer]
so as to bring a closure to it.
So I use #Adam Marshall's solution, it works, but as soon as I start using it, i.e., Testit():
using System;
public class TestClass
{
private delegate void TestDelegate();
TestDelegate testDelegate;
public TestClass()
{
testDelegate = new TestDelegate(MyMethod);
}
public static void Testit()
{
testDelegate();
}
private virtual void MyMethod()
{
Console.WriteLine("Foobar");
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
TestClass.Testit();
}
}
It started to give the followig Error:
A object reference is required for the non-static field, method, or property
You can test it out here .
How to fix it? (Please fix it if possible instead of directing me to other posts, I've read them but am not able to understand them) Thx.
Either everything has to be static or everything has to be instance. You're getting in trouble because you are mixing and matching.
Everything static:
using System;
public class TestClass
{
private delegate void TestDelegate();
static TestDelegate testDelegate; //<-- static
static TestClass() //<-- static
{
testDelegate = new TestDelegate(MyMethod);
}
public static void Testit()
{
testDelegate();
}
private static void MyMethod()
{
Console.WriteLine("Foobar");
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
TestClass.Testit();
}
}
Everything instanced:
using System;
public class TestClass
{
private delegate void TestDelegate();
TestDelegate testDelegate;
public TestClass()
{
testDelegate = new TestDelegate(MyMethod);
}
public void Testit()
{
testDelegate();
}
private void MyMethod()
{
Console.WriteLine("Foobar");
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
var t = new TestClass();
t.Testit(); //<-- non-static
}
}
Output (same in both examples):
Hello World
Foobar
You could use action C# internal delegate. This way you do not have specify the delegate. Then in your static method you could new up your object.
using System;
public class TestClass
{
Action testDelegate;
public TestClass()
{
testDelegate = new Action(MyMethod);
}
public static void Testit()
{
TestClass ts = new TestClass();
ts.testDelegate();
}
private void MyMethod()
{
Console.WriteLine("Foobar");
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
TestClass.Testit();
}
}
Output:
Hello World
Foobar
Hello I am new to programming.
Basically I need to store a method A from class A in variable A from class B inside a method from class B but I cannot seem to find a way.
To give an example:
Class A
public void methodA()
{
*method*
}
Class B
Delegate variableA; //I believe using Delegate is wrong
public void methodB();
{
variableA = ClassA.methodA();
}
Then in Class B there would be another method that will utilize the variable with the stored method.
public void methodC();
{
variableA;
}
This isn't the exact code I have but this is basically the gist of it. Any help is appreciated :)
Edit: Thanks for the help everyone!
ClassA definition:
public class ClassA
{
public void MethodA()
{
Console.WriteLine("Hello World!");
}
}
ClassB definition:
public class ClassB
{
private Action VariableA { get; set; }
public void MethodB(ClassA classA)
{
VariableA = classA.MethodA;
}
public void MethodC()
{
VariableA();
}
}
Program definition:
static void Main(string[] args)
{
ClassA classA = new ClassA();
ClassB classB = new ClassB();
classB.MethodB(classA);
classB.MethodC();
Console.ReadLine();
}
Here is an example:
public class Test
{
private Action<int> hiddenMethod = new Action<int>((i) =>
{
Console.WriteLine(i);
});
public void PublicMethod(int i)
{
hiddenMethod(i);
}
}
And using it:
class Program
{
static void Main(string[] args)
{
Test t = new Test();
t.PublicMethod(21);
Console.Read();
}
}
Use reflection:
public class A
{
public void MethodA()
{
Console.WriteLine("MethodA");
}
public static void StaticMethodA()
{
Console.WriteLine("StaticMethodA");
}
}
public class B
{
MethodInfo mv = typeof(A).GetMethod("MethodA");
MethodInfo smv = typeof(A).GetMethod("StaticMethodA");
public void CheckA(bool useStatic)
{
if (useStatic) smv.Invoke(null, null);
else mv.Invoke(new A(), null);
}
}
class MainClass
{
[STAThread]
public static void Main(string[] args)
{
var b = new B();
b.CheckA(true);
b.CheckA(false);
}
}
See details in MSDN.
I guess I have I miss-conception. I try to override the behavior of a class in a sub-class by replacing a public function. In the program below I expect "B" to be written to the console, but the program prints "A". Where do I think wrong? And how can I achieve it. (In my real case I cannot change class A).
class Program
{
class A { public void F() { Console.WriteLine("A"); } }
class B : A { public new void F() { Console.WriteLine("B"); } }
static void Main(string[] args)
{
A x;
x = new B();
x.F();
Console.ReadLine();
}
}
The desired behaviour can be obtained by using a virtual method and overriding it at follows; note the override keyword.
class Program
{
class A { public virtual void F() { Console.WriteLine("A"); } }
class B : A { public override void F() { Console.WriteLine("B"); } }
static void Main(string[] args)
{
A x;
x = new B();
x.F();
Console.ReadLine();
}
}
If you happen to have a Java background, note that this type of inheritance is the default behaviour in Java, but in C# it has to be explicitly declared.
I have a method in a class in another file that I want to take a dynamic method. I'm having some difficulty negotiating the setup. Any help would be appreciated, thanks!
for example...
File #1:
class DoSomethingClass
{
// define delegate
public delegate void DelegateMethod();
public void Main()
{
DelegateMethod d = Func1;
AnotherClass.CallsDynamicMethod("Test1", d);
d = Func2;
AnotherClass.CallsDynamicMethod("Test2", d);
// will this work?
// AnotherClass.CallsDynamicMethod("Test3", DoSomethingClass.instance.Func3);
}
// candidate methods for delegation
void Func1()
{ Console.WriteLine("calling Func1"); }
void Func2()
{ Console.WriteLine("calling Func2"); }
public void Func3()
{ Console.WriteLine("calling Func3"); }
}
File #2:
class AnotherClass
{
public static void CallsDynamicMethod(string words, DelegateMethod dynamicMethod)
{
Console.WriteLine("this is a " + words + " to call...");
dynamicMethod();
}
}
Hope this answers your problem
class Program
{
static void Method()
{
Console.WriteLine("Method");
}
static void Main(string[] args)
{
Action a = Method;
MyClass.SomeMethod(a);
MyClass.SomeMethod(Method);
Console.ReadLine();
}
}
class MyClass
{
public static void SomeMethod(Action del)
{
del();
}
}