C# 永远不会指定给,并且将始终具有其默认值

C# 永远不会指定给,并且将始终具有其默认值,c#,C#,我正在尝试从从从数据库创建的下拉列表中选择一个名称。我似乎无法得到分配的鬃毛 当我单击select按钮时,它不会将comboBox1中的人名传递给我下一批代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.W

我正在尝试从从从数据库创建的下拉列表中选择一个名称。我似乎无法得到分配的鬃毛

当我单击select按钮时,它不会将comboBox1中的人名传递给我下一批代码

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;
using System.Data.OleDb;
using System.Data.Sql;

namespace GlidingLogsGUI2
{
public partial class InstructorSelectionForm : Form
{
    List<Person> instructors = new List<Person>();
    Person selectedPerson;

    public InstructorSelectionForm()
    {
        InitializeComponent();
        this.TopMost = true;

        List<Person> instructors = new List<Person>();
        OleDbCommand com = new OleDbCommand("SELECT * FROM Personnel WHERE [Current?] = TRUE AND [Position] = ?", Program.DB_CONNECTION);
        com.Parameters.Add(new OleDbParameter("", PositionIDs.INSTRUCTOR));

        OleDbDataReader dr = com.ExecuteReader();

        while (dr.Read())
        {
            try
            {
                instructors.Add(new Person(dr));
            }
            catch
            {
            }
        }

        foreach (Person a in instructors)
        comboBox1.Items.Add(a.FirstName + " " + a.Surname);
    }

    private void Selectbutton_Click_1(object sender, EventArgs e)
    {
        if (selectedPerson == null)
        {
            MessageBox.Show("A person must be selected first");
            return;
        }

        new F5363(selectedPerson);
    }

    private void CancelButton_Click_1(object sender, EventArgs e)
    {
        this.Close();

    }

    private void InstructorSelectForm_Load(object sender, EventArgs e)
    {

    }


    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.Data.OleDb;
使用System.Data.Sql;
命名空间滑动日志gui2
{
公开部分班级教师选举表格:表格
{
列表讲师=新列表();
人员选择人员;
公共讲师选举表格()
{
初始化组件();
this.TopMost=true;
列表讲师=新列表();
OleDbCommand com=新的OleDbCommand(“从人员中选择*,其中[Current?]=TRUE和[Position]=?”,Program.DB_CONNECTION);
添加(新的OleDbParameter(“,positionId.讲师”);
OleDbDataReader dr=com.ExecuteReader();
while(dr.Read())
{
尝试
{
添加(新人员(dr));
}
接住
{
}
}
foreach(讲师中的人员a)
comboBox1.Items.Add(a.FirstName+“”+a.姓氏);
}
私有无效选择按钮\u单击\u 1(对象发送者,事件参数e)
{
如果(selectedPerson==null)
{
MessageBox.Show(“必须首先选择一个人”);
回来
}
新F5363(选定人员);
}
私有作废取消按钮\u单击\u 1(对象发送者,事件参数e)
{
这个。关闭();
}
私有void讲师选择表单加载(对象发送方,事件参数e)
{
}
}
}

您正在声明selectedPerson,但从未在任何地方设置它。我怀疑你想做的是删除selectedPerson的当前声明,并将click事件更改为类似的内容

private void Selectbutton_Click_1(object sender, EventArgs e)
{  
    Person selectedPerson = comboBox1.SelectedItem as Person;
    if (selectedPerson == null)
    {
...

有什么问题?标题中似乎有一个警告/错误/异常的结尾,但我猜不出来。问题是所选的人是空的?请声明。只是永远不要使用空的catch子句。我如何才能声明selectedPerson,我已经尝试了一些方法,但仍然会出现更多错误或未声明。我已经找到了我的错误。我曾两次列出一份讲师名单。列表讲师=新列表();