如何使用函数分配公共变量 [英] How to assign a public variable using a function

查看:55
本文介绍了如何使用函数分配公共变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,其中有一个变量(非静态)和一个用于分配该变量的函数。我正在做如下,但它没有分配。该怎么办?



  public   class  myClass 
{
public int a ;

public void setit()
{
a = 5 ;
}
}





显示 a = 0 不是 5

但我想在这里 5

解决方案

您是否实例化 myClass 然后调用 setit



 var c = new myClass(); 
c.settit();
Console.WriteLine(ca);





如果你想要它初始化施工,然后只需使用:



public int a = 5;

< br $>
或写一个构造函数:



 public myClass()
{
a = 5;
}





Marc


你的问题告诉我你很困惑关于C#编程的基本方面。这不是一个坏的事情......记住,这里的每个人,曾经是初学者。



我强烈建议你写一本关于C#的好书并翻阅基础知识; Charles Petzold的.NET Book Zero可以免费下载......这里:[ ^ ]。



要点了解:



1. A类定义,就像你的'myClass一样,是一种抽象;它是构建类的实例的蓝图。



2.您创建实例具有'新运算符的类:



myClass InstanceOfMyClass = new myClass();



3.一旦有了实例,就可以使用/ to / using(分配,调用,访问)操作类的公共字段,属性和方法:



 InstanceOfMyClass.a = 100; //'a将具有值#100< br /> 
//或< br />
InstanceOfMyClass.Setit(); //'a将具有值#5





4.通过使用参数创建公共方法,您可以启用其他行为:



< br /> 
public void SetAValue(int newavalue)< br />
{< br />
a = newavalue;< br />
}





5.您还可以使用公共财产获取/设定值:



 private int _a;< br /> 
public int A {< br />
set {_a = value; }< br />
get {return _a; }< br />
}





这里私有整数变量'_a被称为支持字段属性。 .NET提供了一种更简单的方法来创建具有(隐藏)私有支持字段的Property:



public int A {set;得到; }



现在,是开始工作的时候了;你需要掌握一定的基础知识,只有可以做到这一点。获取Petzold的书,或另一本关于启动C#的书,然后开始试验。大多数人通过学习和反复试验的组合来学习。



您可以获得免费的Visual Studio包,并且......开始使用。


i have a class in which there is a variable(not static) and a function which is used to assign that variable. i am doing as follows but it fails to assign. how to do it?

public class myClass
    {
        public int a;        

        public void setit()
        {
           a = 5;
        }
    }



it shows a = 0 not 5
but i want here 5

解决方案

Are you instantiating myClass and then calling setit ?

var c = new myClass();
c.settit();
Console.WriteLine(c.a);



If you want it initialized at the time of construction, then just use:

public int a = 5;

or write a constructor:

public myClass()
{
  a = 5;
}



Marc


Your question suggests to me that you are very confused about basic aspects of programming in C#. That is not a "bad" thing ... remember that everyone here, at one time, was a beginner.

I strongly suggest you get a good book on C# and go over the "basics;" Charles Petzold's ".NET Book Zero" can be downloaded ... free ... here: [^].

Points to understand:

1. A Class definition, like your 'myClass, is a kind of abstraction; it's a blue-print for the construction of instances of the Class.

2. You create an instance of a Class with the 'new operator:

myClass InstanceOfMyClass = new myClass();

3. Once you have an instance you can then operate with/to/using (assign to, invoke, access) the public fields, properties, and methods of the Class:

InstanceOfMyClass.a = 100; // 'a will have the value #100<br />
// or<br />
InstanceOfMyClass.Setit(); // 'a will have the value #5



4. by creating public methods with parameters, you enable additional behavior:

<br />
public void SetAValue(int newavalue)<br />
{<br />
   a = newavalue;<br />
}



5. you can also use public properties to get/set values:

private int _a;<br />
public int A {<br />
    set { _a = value; }<br />
    get { return _a;  }<br />
}



Here the private integer variable '_a is called the "backing field" of the Property. .NET offers a simpler way to create a Property with a (hidden) private backing field:

public int A { set; get; }

Now, it's time for you to get to work; there is a certain amount of basic knowledge you need to master, and only you can do this. Get the book by Petzold, or another book on starting C#, and start experimenting. Most people learn by a combination of study and trial-and-error experiments.

You can get the free Visual Studio package, and ... get started.


这篇关于如何使用函数分配公共变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆