Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#在另一个类中创建一个类的实例_C#_Class_Properties - Fatal编程技术网

C#在另一个类中创建一个类的实例

C#在另一个类中创建一个类的实例,c#,class,properties,C#,Class,Properties,我有一个应用程序,它有多个动态自定义用户控件来收集DataGridView中的边名称和曲线偏移。我的目标是将它们输入到一个类中,并使用一些方法检索一些公共数据组以供以后处理 我有一个类定义了偏移的格式(因为它对于单条边和一系列边都是相同的),然后有另一个类将这些边分组为一个“示例”。第二个类中的方法将返回每个示例所需的公共数组 代码如下-符合ok-然后当我尝试设置偏移量的sName属性时,它返回一个NullReferenceException。我怀疑我没有正确初始化示例类中的内容,或者没有正确地

我有一个应用程序,它有多个动态自定义用户控件来收集DataGridView中的边名称和曲线偏移。我的目标是将它们输入到一个类中,并使用一些方法检索一些公共数据组以供以后处理

我有一个类定义了偏移的格式(因为它对于单条边和一系列边都是相同的),然后有另一个类将这些边分组为一个“示例”。第二个类中的方法将返回每个示例所需的公共数组

代码如下-符合ok-然后当我尝试设置偏移量的sName属性时,它返回一个
NullReferenceException
。我怀疑我没有正确初始化示例类中的内容,或者没有正确地公开或声明它们(静态、抽象、虚拟??)。offSets类工作正常,但在示例类中访问它时就不行了

请帮忙

    private class offSets
    {
        public string sName { get; set; }
        public double d0 { get; set; }
        public double d1 { get; set; }
        public double d2 { get; set; }
        public double d3 { get; set; }
        public double d4 { get; set; }
        public double d5 { get; set; }
        public double d6 { get; set; }
        public double d7 { get; set; }
        public double d8 { get; set; }
        public double dLength { get; set; }
    }

    private class Example
    {
        public offSets curve1 { get; set; }
        public offSets curve2 { get; set; }
        public List<offSets> lstCurves { get; set; }

        public string testString { get; set; }

        public double[] somemethod()
        {
           //code that returns an array - something lie:
           return this.lstCurves.Select(i => i.d2).ToArray();
        }
    }

    private void temp()
    {
        Example e = new Example();

        e.testString = "This is some test text";

        MessageBox.Show(e.testString);
        // This works as expected.


        e.curve1.sName = "Some Name"; 
        // error on above line: "System.NullReferenceException"
     }
私有类偏移量
{
公共字符串sName{get;set;}
公共双d0{get;set;}
公共双d1{get;set;}
公共双d2{get;set;}
公共双d3{get;set;}
公共双d4{get;set;}
公共双d5{get;set;}
公共双d6{get;set;}
公共双d7{get;set;}
公共双d8{get;set;}
公共双数据长度{get;set;}
}
私有类示例
{
公共偏移量曲线1{get;set;}
公共偏移量曲线2{get;set;}
公共列表{get;set;}
公共字符串testString{get;set;}
公共双精度方法()
{
//返回一个数组的代码-有东西在说谎:
返回这个.lstCurves.Select(i=>i.d2.ToArray();
}
}
私有void temp()
{
示例e=新示例();
e、 testString=“这是一些测试文本”;
Show(例如testString);
//这正如预期的那样有效。
e、 curve1.sName=“某个名称”;
//第行上方的错误:“System.NullReferenceException”
}

这可能会帮到你

Example e = new Example();
e.curve1 = new offSets();
e.curve1.sName = "Some Name";

这可能对你有用

Example e = new Example();
e.curve1 = new offSets();
e.curve1.sName = "Some Name";

我不知道你是否要说 e、 曲线1=新偏移()


在最后一行之前?

我想知道你是否要说 e、 曲线1=新偏移()


在最后一行之前?

声明了
curve1
属性,但为“空”(即
null

将构造函数添加到
示例
类中,然后在其中创建
偏移
对象:

private class Example
{
    public offSets curve1 { get; set; }
    public offSets curve2 { get; set; }
    public List<offSets> lstCurves { get; set; }

    public string testString { get; set; }

    public Example()
    {
        this.curve1 = new offSets();
        this.curve2 = new offSets();
        this.lstCurves = new List<offSets>();
    }

    public double[] somemethod()
    {
       //code that returns an array - something lie:
       return this.lstCurves.Select(i => i.d2).ToArray();
    }
}
私有类示例
{
公共偏移量曲线1{get;set;}
公共偏移量曲线2{get;set;}
公共列表{get;set;}
公共字符串testString{get;set;}
公共示例()
{
this.curve1=新偏移();
this.curve2=新偏移();
this.lstCurves=新列表();
}
公共双精度方法()
{
//返回一个数组的代码-有东西在说谎:
返回这个.lstCurves.Select(i=>i.d2.ToArray();
}
}

声明了
curve1
属性,但为“空”(即
null

将构造函数添加到
示例
类中,然后在其中创建
偏移
对象:

private class Example
{
    public offSets curve1 { get; set; }
    public offSets curve2 { get; set; }
    public List<offSets> lstCurves { get; set; }

    public string testString { get; set; }

    public Example()
    {
        this.curve1 = new offSets();
        this.curve2 = new offSets();
        this.lstCurves = new List<offSets>();
    }

    public double[] somemethod()
    {
       //code that returns an array - something lie:
       return this.lstCurves.Select(i => i.d2).ToArray();
    }
}
私有类示例
{
公共偏移量曲线1{get;set;}
公共偏移量曲线2{get;set;}
公共列表{get;set;}
公共字符串testString{get;set;}
公共示例()
{
this.curve1=新偏移();
this.curve2=新偏移();
this.lstCurves=新列表();
}
公共双精度方法()
{
//返回一个数组的代码-有东西在说谎:
返回这个.lstCurves.Select(i=>i.d2.ToArray();
}
}

由于未初始化curve1,因此出现错误。换句话说,您会得到一个异常,因为e.curve1为null,所以不能分配e.curve1.sName

您需要修改temp()函数:

private void temp()
{
    Example e = new Example();

    e.testString = "This is some test text";

    MessageBox.Show(e.testString);
    // This works as expected.


    e.curve1 = new offSets();
    e.curve1.sName = "Some Name";
    // now it should work
 }

由于未初始化curve1,因此出现错误。换句话说,您会得到一个异常,因为e.curve1为null,所以不能分配e.curve1.sName

您需要修改temp()函数:

private void temp()
{
    Example e = new Example();

    e.testString = "This is some test text";

    MessageBox.Show(e.testString);
    // This works as expected.


    e.curve1 = new offSets();
    e.curve1.sName = "Some Name";
    // now it should work
 }

请看,请看,这可能会起作用,但下面使用构造函数方法的答案肯定更加用户友好/消除了可能忘记初始化单个类的问题。感谢您的回答ThoughtSure,在构造函数中初始化属性是最方便的方法。我只想指出初始化的对象占用内存。如果您有大量的对象,并且并非所有实例都需要某些属性,您可以选择不初始化它们以节省空间。也许这会起作用,但是下面使用构造函数方法的答案肯定更加用户友好/消除了可能忘记初始化单个类的问题。感谢您的回答ThoughtSure,在构造函数中初始化属性是最方便的方法。我只想指出初始化的对象占用内存。如果有大量对象,并且并非所有实例都需要某些属性,则可以选择不初始化它们以节省空间。