Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.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#_Asp.net_Automatic Properties - Fatal编程技术网

C# 组合特性

C# 组合特性,c#,asp.net,automatic-properties,C#,Asp.net,Automatic Properties,我在下面的FullName属性上收到一个Index out-bounds错误。我有我的persondetails类和data类,其中我正在使用SqlDataReader尝试调用此属性。使用存储过程返回firstname和lastname值,然后我想创建一个属性来连接这两个值,并能够在存储过程中调用FullName persondetailsclass private string firstName; private string lastName; private string fullNam

我在下面的
FullName属性
上收到一个
Index out-bounds错误。我有我的
persondetails
类和
data
类,其中我正在使用
SqlDataReader
尝试调用此属性。使用存储过程返回
firstname
lastname
值,然后我想创建一个属性来连接这两个值,并能够在存储过程中调用
FullName

persondetails
class

private string firstName;
private string lastName;
private string fullName;

public string FirstName
{
    get { return firstName;}
    set { set firstName = value;}
}

public string LastName
    get { return lastName; }
    set { set lastName = value; }

public string FullName
    get { return lastName + "," + firstName;
}

public persondetails(string lastName, string firstName)
{
    this.lastName = lastName;
    this.firstName = firstName;
}
public List<Persondetails> getFullName()
{
    // Do my sql connection stuff
    List<Persondetails> persondetails = new List<Persondetails>();
    Persondetails person;

    try
    {
        // open connection
        // specify "stored Procedure" (which returns firstname and lastname)

        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                person = new Persondetails((
                reader.GetString(reader.GetOrdinal("LASTNAME")));
                reader.GetString(reader.GetOrdinal("FIRSTNAME")));
                persondetails.Add(person);
            }
            reader.Close();
            return persondetails;
        }
        // the rest of my method
数据

private string firstName;
private string lastName;
private string fullName;

public string FirstName
{
    get { return firstName;}
    set { set firstName = value;}
}

public string LastName
    get { return lastName; }
    set { set lastName = value; }

public string FullName
    get { return lastName + "," + firstName;
}

public persondetails(string lastName, string firstName)
{
    this.lastName = lastName;
    this.firstName = firstName;
}
public List<Persondetails> getFullName()
{
    // Do my sql connection stuff
    List<Persondetails> persondetails = new List<Persondetails>();
    Persondetails person;

    try
    {
        // open connection
        // specify "stored Procedure" (which returns firstname and lastname)

        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                person = new Persondetails((
                reader.GetString(reader.GetOrdinal("LASTNAME")));
                reader.GetString(reader.GetOrdinal("FIRSTNAME")));
                persondetails.Add(person);
            }
            reader.Close();
            return persondetails;
        }
        // the rest of my method
public List getFullName()
{
//做我的sql连接的事情
List persondetails=新列表();
人格化的人;
尝试
{
//开放连接
//指定“存储过程”(返回firstname和lastname)
使用(SqlDataReader=cmd.ExecuteReader())
{
while(reader.Read())
{
人员=新人员((
reader.GetString(reader.GetOrdinal(“LASTNAME”));
reader.GetString(reader.GetOrdinal(“FIRSTNAME”));
persondetails.添加(person);
}
reader.Close();
返回个人信息;
}
//我剩下的方法
存储过程只是从我的表中返回
lastname
firstname
,该表有两个单独的字段。我不想在这里进行连接,我想在我的属性中进行连接

编辑***
工作代码

问题在于存储过程没有返回名为“FullName”的列。这就是为什么会出现错误

如果存储过程返回FirstName和LastName,则需要将它们存储在相应的属性中

我希望您有一个从数据库填充类的方法…在它里面类似这样的东西

FirstName = reader.GetString(reader.GetOrdinal("FirstName")));  
LastName = reader.GetString(reader.GetOrdinal("LastName")));  

这将在类中填充您的FirstName和LastName…然后您的FullName属性将起作用,因为它只是连接FirstName和LastName。

问题在于您的存储过程没有返回名为“FullName”的列。这就是出现错误的原因

如果存储过程返回FirstName和LastName,则需要将它们存储在相应的属性中

我希望您有一个从数据库填充类的方法…在它里面类似这样的东西

FirstName = reader.GetString(reader.GetOrdinal("FirstName")));  
LastName = reader.GetString(reader.GetOrdinal("LastName")));  

这将在类中填充您的FirstName和LastName…然后您的FullName属性将起作用,因为它只是连接FirstName和LastName。

您实际上没有在类构造函数中设置
FirstName
LastName
字段,而是在构造函数和
FullName中复制代码ode>属性。您需要做的是:

public string FullName
    get { return this.lastName + "," + this.firstName;
}

public persondetails(string lastName, string firstName)
{
    this.firstName = firstName;
    this.lastName = lastName;
}

这将确保正确计算
FullName
属性值。

您不是在类构造函数中实际设置
firstName
lastName
字段,而是在构造函数和
FullName
属性中复制代码。您需要做的是:

public string FullName
    get { return this.lastName + "," + this.firstName;
}

public persondetails(string lastName, string firstName)
{
    this.firstName = firstName;
    this.lastName = lastName;
}

这将确保正确计算
FullName
属性值。

由于在C#6.0中添加了like
string
插值功能,我们可以如下连接
string

public string FullName => $"{firstName} {lastName}";

由于在C#6.0中添加了like
string
插值功能,我们可以如下连接
string

public string FullName => $"{firstName} {lastName}";

你说的是哪个返回firstname和lastname…为什么你希望结果中出现
FullName
?你从哪里得到异常?你说的是哪个返回firstname和lastname…为什么你希望结果中出现
FullName
?你从哪里得到异常?我编辑了我的代码…不确定我是否应该更改我的构造函数也是这样。但是似乎返回了一个带有fullName的属性,但dropdownlist中的引用不起作用。我明白了,我肯定是想得太多了。但我的问题是,我试图检查它是否在错误的位置为null,这导致我的fullName属性只返回lastname。谢谢你的帮助help@Tim擅长lent!很高兴我能帮上忙。我编辑了我的代码…不确定我是否也应该这样更改我的构造函数。但似乎返回了一个全名的属性,但在dropdownlist中引用了一个不起作用的属性。我得到了它。我肯定是想太多了。但我的问题是,我试图检查它是否在错误的位置为null,这是原因ng my fullname属性以仅返回lastname。感谢您的帮助help@Tim太好了!很高兴我能提供帮助。您还可以使用表达式体:public string FullName=>FirstName+“”+LastName;也可以使用表达式体:public string FullName=>FirstName+“”+LastName;虽然这样做有效,但它是否是对
+
使用的改进还是个疑问。当您有相对复杂的字符串时,字符串插值是很好的,但是它使用
String.Format
来做这项工作,这通常比
String.Concat
的效率要低ks,这是否是对
+
使用的改进值得怀疑。当字符串比较复杂时,字符串插值是很好的,但是它使用
字符串.Format
来完成这项工作,这通常比
字符串.Concat
效率低,而
+
就是编译的。