I dont know why this code didnt work.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace po
{
public delegate void godmode(int a, int b, out int c, out int d);
public class Class1
{
public int a;
public int b;
public int c;
}
public class Program
{
Class1 myclass = new Class1();
Program pro = new Program();
// myclass.a didnt work
static void go1(int a, int b, out int c, out int d)
{
c = a + b;
d = (a + b) * 10;
}
static void go2(int a, int b, out int c, out int d)
{
c = a;
d = b;
}
static void go3(int a, int b, out int c, out int d)
{
c = a * 100;
d = b * 200;
}
static int outofgod(int a,out int result1,out int result2, godmode beaman)
{
int b = a*2;
int c = a*3;
int re1, re2;
beaman(b,c,out re1,out re2);
result1 = re1 * 10;
result2 = re2;
}
static void Main(string[] args)
{
int a = 1;
int b = 2;
int c = 0;
int d = 0;
Console.WriteLine("a = {0}.....b = {1}..... c = {2}.... d = {3}",a,b,c,d);
godmode mode1;
godmode mode2;
godmode mode3;
mode1 = new godmode(go1);
mode2 = new godmode(go2);
mode3 = new godmode(go3);
outofgod(a,out c,out d,mode1);
Console.WriteLine("a = {0}.....b = {1}..... c = {2}.... d = {3}", a, b, c, d);
outofgod(a, out c, out d, mode2);
Console.WriteLine("a = {0}.....b = {1}..... c = {2}.... d = {3}", a, b, c, d);
outofgod(a, out c, out d, mode3);
Console.WriteLine("a = {0}.....b = {1}..... c = {2}.... d = {3}", a, b, c, d);
}
}
}
**
myclass is isntanc of Class1. so i want to use a of instance"myclass" . but when i type myclass, nothing appear.
Your methods are static. You cant access non static field from static method.
static void YourMethod(....) // notice static keyword
{
}
To solve this problem you can make myclass static field.
static Class1 myclass = new Class1();
You can probably use it like this
public static class Class1
{
public static int a;
public static int b;
public static int c;
}
and then you can access the variables like
Class1.a
because if you normally try to do this will be
An object reference is required for the non-static field, method, or property 'myclass'
myClass is an "instance variable" since Program class is not static. Also, since your methods are static you can not access to a instance variable, you should to do this:
public class Program
{
Class1 myClass = new Class1();
public Class1 MyClass{get{return myClass;}}
}
static void godmode()
{
Program p = new Program();
var myClass = p.MyClass;
}
But it sucks. So if you want to use Class1 you'll have to instantiate it inside the static method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.