无法在结构内分配结构 [英] can not assigne a struct inside a struct

查看:72
本文介绍了无法在结构内分配结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace StructInStruct
{
    public partial class Form1 : Form
    {   public struct Row { public double val; public string qnt;};
        public struct FoodStruct { public Row[] row; public int nrow;};
        public static FoodStruct[] fd = new FoodStruct[100];
        public Form1()
        {
            InitializeComponent();
            pp();
        }
        private void pp()
        {   int i, j;
            for (i = 0; i < 100; i++)
            {
                fd[i] = new FoodStruct();
                fd[i].nrow = 100;
                for (j = 0; j < 100; j++)
                {
                    fd[i].row[j] = new Row();// i have error here
                    fd[i].row[j].val = j; fd[i].row[j].qnt = "Hellow";
                }//j
            }//i
        }
    }
}

推荐答案

否,您不能:必须首先分配行数组:
No, you can''t: you have to assign the row array first:
for (i = 0; i < 100; i++)
{
    fd[i] = new FoodStruct();
    fd[i].nrow = 100;
    fd[i].row = new Row[100]; //Add this line.
    for (j = 0; j < 100; j++)
    {
        fd[i].row[j] = new Row();// i have error here
        fd[i].row[j].val = j; fd[i].row[j].qnt = "Hellow";
    }//j
}//i


这篇关于无法在结构内分配结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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