C#使用未分配的变量错误 [英] C# use of unassigned variable error

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

问题描述

private void button1_Click(object sender, EventArgs e)
{
    Funcionario Func = new Funcionario(Func.getNome(),
                                       Func.getSexo(),
                                       Func.getDataNascimento(),
                                       Func.getBi(),
                                       Func.getTelefone(),
                                       Func.getMorada(),
                                       Func.getEmail());
}




此代码中的
,我有这个错误:使用未分配的变量Func,任何人都知道怎么放这对吗?



谢谢



in this code, im having this error : use of unassigned variable "Func", anyone knows how to put this right?

Thanks

推荐答案

在这种情况下,实例 Func 将仅在构造完成时创建/初始化,但是在通过Fun.getName()构造之前调用实例 Func 的方法,getSexo等。



我不确定你在这里想做什么。看起来你对''Funcinario''类中可用的构造函数覆盖感到困惑



应该是这样的东西



In this case the instance Func will be created/initialized only when the construction is done, but you are invoking a method of the instance Func before its construction through Fun.getName(),getSexo etc .

I am not sure what you are trying to do here. Looks like you are confused with the constructor overrides available in the ''Funcinario'' class

It should be something like this

private void button1_Click(object sender, EventArgs e)
{
    Funcionario Func = new Funcionario(); // 
    string name = Func.getName(); //data type selected only for explanation
    string sex = Func.getSexo(); // data type selected only for explanation
}


namespace Zoo
{
    public partial class FuncionarioForm : Form
    {
        List<funcionario> Lfunc;
        List<animal> LAnimal;
        
        public FuncionarioForm()
        {
            InitializeComponent();
            Lfunc = new List<funcionario>();
            LAnimal = new List<animal>();
        }

        private void AdicionarFuncionario_Load(object sender, EventArgs e)
        {

        }         

        private void Refresh()
        {
            listBox1.Items.Clear();
            foreach (Funcionario F in Lfunc)
                listBox1.Items.Add(F.ToString());
            listBox1.Items.Add("----------");
            foreach (Animal A in LAnimal)
                listBox1.Items.Add(A.ToString());
        }

        private void button1_Click(object sender, EventArgs e)
        {


            Funcionario Func = new Funcionario(Func.getNome(),
                                               Func.getSexo(),
                                               Func.getDataNascimento(),
                                               Func.getBi(),
                                               Func.getTelefone(),
                                               Func.getMorada(),
                                               Func.getEmail());

            Lfunc.Add(Func);
            Refresh(); 
        }



这是本部分中的所有代码,但我有一个带有构造函数的类Funcionario,现在我想添加Funcionario(雇主)的数据)写在文本框中的列表框和列表!!


This is all code in this part, but i have a class Funcionario with a constructor, and now i want to add the data of Funcionario(employer) that is written in the textbox´s to the listbox and the list!!


这篇关于C#使用未分配的变量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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