1

I'm receving an Index out bounds error on the FullName property below. I have my persondetails class and data class where I am using a SqlDataReader trying to call this property. The firstname and lastname values are returned using a stored procedure and then I wanted to create a property to concatenate these 2 and just be able to call FullName in my stored Procedure.

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;
}

data class

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

Stored Procedure simply returns lastname and firstname from my table which has 2 seperate fields. I do not want to do the concatenation here I would like to do it in my properties.

EDITED*** Working Code

Tim
  • 1,209
  • 4
  • 21
  • 33

3 Answers3

3

Since the like string interpolation feature was added in C# 6.0, we can concatenate string as below

public string FullName => $"{firstName} {lastName}";
Ian Griffiths
  • 14,302
  • 2
  • 64
  • 88
  • While this works, it's questionable whether it's an improvement on use of `+`. String interpolation is great when you have relatively complex strings, but it use `string.Format` to do the work, which is typically less efficient than `string.Concat` which is what `+` compiles to. – Ian Griffiths Sep 08 '17 at 14:56
1

You don't have a column returned from your stored procedure named "FullName" is the problem. That's why you get the error.

If your stored procedure returns FirstName and LastName you need to store those in their appropriate properties.

I would expect you to have a method that populates your class from the database... and in it something like this...

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

That would populate your FirstName and LastName in your class.... then your FullName property would work since it just concatenates FirstName and LastName.

Kevin
  • 4,586
  • 23
  • 35
  • I edited my code...not sure if i was suppose to change my constructor that way also. But seems to return a property with fullName but reference that in a dropdownlist is not working – Tim Jul 16 '13 at 11:57
  • I got it I was definitely over thinking this. But my problem is the way I was trying to check if it was null in the wrong place which was causing my fullname property to only return lastname. Thanks for the help – Tim Jul 16 '13 at 12:11
1

You are not actually setting the firstName and lastName fields in the class constructor, you are duplicating code in the constructor and the FullName property. What you need to do is:

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

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

This will ensure that the FullName property value is calculated correctly.

Peter Monks
  • 4,219
  • 2
  • 22
  • 38
  • 1
    Also you can use expression body: public string FullName => FirstName + " " + LastName; – Elnaz Sep 24 '16 at 06:36