English 中文(简体)
在C#中设立一个类别,使用未转让变量
原标题:creating a class in C#, use of unassigned variable
  • 时间:2011-04-11 18:52:00
  •  标签:
  • c#

我将“在InfoPeople使用未经签名的版本”。 我不敢肯定,为什么把我的班子作为教科书的榜样。 有些人可以对此稍作说明。 感谢。

  class personInfo
    {
        public string fName;
        public string lName;
        public int personID;
    }
    class Program
    {
        static void Main(string[] args)
        {
            personInfo InfoPeople;
            InfoPeople.fName = "jeff";
            Console.WriteLine("the fName is: " + " " + InfoPeople.fName);
        }
    }
最佳回答

阁下:

personInfo InfoPeople = new personInfo();

I would try and conform to C# style type/casing conventions:

PersonInfo infoPeople = new PersonInfo();
问题回答

You need to new your object instance like this:

personInfo InfoPeople = new personInfo();

这只是C# syntax制造新物体的例子。

由于<代码>InfoPeople是一种可参照人信息的变量,但你从未实际创建人信息/代码>。

Fix it by saying:

personInfo InfoPeople = new personInfo();

By the way, your naming convention isn t very conventional. People generally PascalCase class names and camelCase variables. Try to follow that convention so that your code is easier to understand.

In C#, you must assign a value to a local variable before it is used -- local variables do not default to anything (not even the default value of the type!).

正如其他人所指出的,正确的解决办法很可能使用new personInfo(。 然而,在<代码>InfoPeople变量(在使用该代码的地方)上仅仅指定了null,便可删除汇编时的错误(在操作时间段可找到;-)

(例如,汇编者错误与InfoPeople无任何关系,其中包含“无效”价值——它只注意以前曾分配过t Value<>>。)

幸福 co。


摘图:

// C# allows one to instantiate a new object
// and assign members at the same time.
personInfo InfoPeople = new personInfo {
    fName = "jeff",
};

页: 1 你用“personInfoPeople说明”具体指明了标的类型,但没有分配记忆。 当你用新的()关键词来做时,它实际上制造了记忆中的物体,这是错误。

personInfo InfoPeople = new personInfo()

您宣布以下类型的地方变量:personInfo, 名称:,但您在尝试使用之前没有给它分配价值。 汇编者可以证明这一点,因此可以提出申诉。 足够简单。

有权立即审理某人的案件 信息

  class personInfo
{
    public string fName;
    public string lName;
    public int personID;
}
class Program
{
    static void Main(string[] args)
    {
        personInfo InfoPeople = new personInfo();
        InfoPeople.fName = "jeff";
        Console.WriteLine("the fName is: " + " " + InfoPeople.fName);
    }
}

页: 1 信息:

个人 信息信息数据库=新人信息数据库;

你们需要把这一点作为新事物来做吗?

“新个人信息”你必须从这几类物体中立即发送。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...