1

I'm quite new to C# so sorry if my coding a bit dodgy! I keep getting 3 errors for my People application.
The errors are caused by trying to convert the int and bools into strings, I'm just not sure how I'm meant to change it, if you guys could give me a hand, that would be great thanks! Any tips will also be greatly appreciated! The errors that keep reoccurring are:

  • Error 1: The best overloaded method match for 'Lab5b.Person.Person(string, int, string, string, bool, string)' has some invalid arguments

  • Error 2: Argument 2: cannot convert from 'string' to 'int'

  • Error 3: Argument 5: cannot convert from 'method group' to 'bool'

My Code:

public partial class _Default : Page
    {

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Write("You have successfully added a Person!");

            Person p = new Person(TextBox1.Text, TextBox2.ToString(), TextBox3.Text, TextBox4.Text, DropDownList1.ToString, TextBox5.Text);
        }
    }

and my Person class:

class Person
{
    private string name;
    private int age;
    private string dob;
    private string telNo;
    private bool gender;
    private string address;

    public Person(string name, int age, string dob, string telNo, bool gender, string address)
    {
        Name = name;
        Age = age;
        DOB = dob;
        TelNo = telNo;
        Gender = gender;
        Address = address;
    }

    public int Age { get; set; }
    public string Name { get; set; }
    public string DOB {get; set;}
    public string TelNo {get; set;}
    public bool Gender {get; set;}
    public string Address {get; set;}

    public string PresentPerson()
    {

    }
}
mpromonet
  • 11,326
  • 43
  • 62
  • 91
  • You should make a separate question for each of your errors. At least one of your errors has been answered here: http://stackoverflow.com/questions/1019793/how-can-i-convert-string-to-int – Rick Smith Jun 03 '15 at 20:15

2 Answers2

3

You have a number of issues.

  • First, you are passing the value from TextBox2 as a string when the "Age" parameter requires an int.
  • Second, you are passing the DropDownList1 value as a method group because you did not close out the params ()
  • Third, if you HAD called .ToString() correctly, you would have been passing a string value to an argument expecting bool.
  • Fourth, referencing the control itself TextBox2 and not the value OF the textbox will give you an unexpected an incorrect result.
  • Fifth, you are not actually retrieving the selected value of the dropdown list but rather you are attempting to pass the control itself.

.

protected void Button1_Click(object sender, EventArgs e)
{
    Response.Write("You have successfully added a Person!");

    Person p = new Person(TextBox1.Text, int.Parse(TextBox2.Text.ToString()), 
        TextBox3.Text, TextBox4.Text, bool.Parse(DropDownList1.SelectedValue.ToString()), 
        TextBox5.Text);
}

For Gender, the code is making an assumption that your drop down list values are either True or False so that they can be appropriately parsed, without null entries or entries that are a different value (such as Male / Female).

Also, it's worth noting that these parses will throw an exception if they cannot be completely properly. It would be good to use TryParse and validate that your data is what you think it is.

protected void Button1_Click(object sender, EventArgs e)
{
    Response.Write("You have successfully added a Person!");

    int age;
    bool gender;

    int.TryParse(TextBox2.Text.ToString(), out age);
    bool.TryParse(DropDownList1.SelectedValue, out gender);

    Person p = new Person(TextBox1.Text, age, TextBox3.Text, TextBox4.Text, gender, 
                    TextBox5.Text);
}
David L
  • 32,885
  • 8
  • 62
  • 93
3

You are calling the constructor person with this line:

Person p = new Person(TextBox1.Text, TextBox2.ToString(), TextBox3.Text, TextBox4.Text, DropDownList1.ToString, TextBox5.Text);

Note that a TextBox.Text is of type string.

Your person constructor has a signature of:

public Person(string name, int age, string dob, string telNo, bool gender, string address)

That means the second and fifth argument need to be and int and bool respectively. If the user is entering the data in the textboxes correctly and the dropdown list has the correct values you can use the Convert class to get the right form as:

Person p = new Person(TextBox1.Text, Convert.ToInt32(TextBox2.Text), TextBox3.Text, TextBox4.Text, Convert.ToBoolean(DropDownList1.SelectedValue), TextBox5.Text);

Calling TextBox2.ToString() is trying to convert the object to a string, not necessarily the text in the textbox. Similarly for the dropdown list, where you should be using SelectedText or SelectedValue depending on how you have it set up.

Also in your class definition for Person you have the fields defined twice.

private string name;
private int age;
private string dob;
private string telNo;
private bool gender;
private string address;

And

public int Age { get; set; }
public string Name { get; set; }
public string DOB {get; set;}
public string TelNo {get; set;}
public bool Gender {get; set;}
public string Address {get; set;}

Make sure you only define your fields once. If you are trying to have private fields and public properties it is a common practice to have camelCase for the private field names and PascalCase for the properties. Also the properties should be defined to access the private fields:

private string name;

public string Name { get { return name; } set { name = value; } }
Evan Frisch
  • 1,334
  • 5
  • 22
  • 40