我们如何向对象添加值 [英] How can we add values to a object

查看:136
本文介绍了我们如何向对象添加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我们如何向对象添加值.我已经创建了一个对象,并且想为其分配一些值.

例如:

Hi,
How can we add values to a object. i have created a object and i want to assign some values to it.

Ex:

object obj;


我有两个值,说数字,移动.我想将这些值添加到对象obj.
如果我键入obj.number,obj.mobile,则值必须显示

问候,
S.inayat Basha.


I have two values say number,mobile. and i want to add these values to the object obj.
if i type obj.number,obj.mobile the values has to display

Regards,
S.inayat Basha.

推荐答案

在大多数情况下,这是通过属性"完成的:
Most of the time, this is accomplished by way of "properties":
public class MyObject 
{
    public int Number { get; set; }
    public string Mobile { get; set; }
}


然后您将它们设置如下:


And you set them like this:

MyObject myObject = new MyObject();
myObject.Number = 0;
myObject.Mobile = "Abcdefg";


这是所有EXTREMELY基本的.Net知识.我建议如果您不熟悉.Net,则可以买书或上课.


This is all EXTREMELY basic .Net knowledge. I recommend that if you''re not familiar with .Net, you buy a book or take a class.


object数据类型(即所有其他类型的母亲) (当然)没有这样的值"(即数据成员).您必须首先定义自己的类,例如:
The object data type (i.e. the mother of all the other types) has NOT (of course) such ''values'' (i.e. data members). You have to first define your own class, for instance:
class MyObject
{
   public string number;
   public string mobile;
   public MyObject( string n, string m ){ number = n; mobile = m;}
}



然后使用您喜欢的方式(如果合法),例如



and then use the way you prefer (if legal), for instance

object obj;
obj = new MyObject("666666666666", "999999999999");


您正在尝试对所有.NET语言完全陌生地使用该范例.您的思维类似于JavaScript,并且对所有经典的OP语言(包括所有.NET,C ++,Object Pascal,Python,Eiffel等)完全陌生.

此页面上的所有答案都是正确的,但您的想法与人们正在写的主题相距太远.我并不是说您的想法是错误的,它只是来自非常不同的背景,与.NET使用的经典OOP范例几乎没有共同点.您应该从一开始就学习经典的OOP范例.

看看: http://en.wikipedia.org/wiki/Object-oriented_programming [ ^ ].

然后,他们阅读有关C#和.NET的基本手册,并尝试了解其基本知识.实际上,这比您可能来自的范式更容易理解.

—SA
You are trying to use the paradigm totally foreign to all .NET languages. Your thinking resembles JavaScript and looks totally exotic to all classical OP languages including all of the .NET, C++, Object Pascal, Python, Eiffel, whatever.

All answers on this page are correct, but your thinking is too far from the topics people are writing about. I''m not saying your thinking is wrong, it''s just from a very different background, having almost nothing in common with classical OOP paradigm used by .NET. You should start with learning classical OOP paradigm from the very beginning.

Take a look: http://en.wikipedia.org/wiki/Object-oriented_programming[^].

And them, take an elementary manual on C# and .NET and try to understand the very basics. This is actually much easier to understand then the paradigm you might have come from.

—SA


这篇关于我们如何向对象添加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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