Essej
Essej

Reputation: 861

An object of a type convertible to 'int' is required

This is my code:

namespace Cinemaseats

 {public partial class MainForm : Form
    {
    private const int numOfSeats = 60;
    private int numOfReservedSeats = 0;
    Seat currentSeat = new Seat();
    public MainForm()
    {
        InitializeComponent();
        InitializeGUI();
    }

    private void InitializeGUI()
    {
        radioButton1.Checked = true;
        namn.Text = string.Empty;
        pris.Text = string.Empty;
    }
    private void button1_Click(object sender, EventArgs e)
    {
        **int seatNr = ReadAndValidateSeatNr();**

        if (seatNr < 0)
        {
            MessageBox.Show("Välj ett föremål från listan");
            return;
        }

        if (radioButton2.Checked)
            ReserveSeat(seatNr);
        else
            CancelSeat(seatNr);

        UpdateGUI(seatNr);
    }

    public int GetnumOfSeats()
    {
        return numOfSeats;
    }

    **public int ReadAndValidateSeatNr()
    {
     thelist.Items.Add(test); //test
     return;
    }
    string test = Convert.ToString(2);
}**
}

I have converted my string "test" to an int but still VS says that the return value has to be an int? I want to display 60 "seats in my listbox, that is to say "Seat 1","Seat 2" and so on. I wrote a test string to see if I could make it work.I'm not sure how getter and setters methods work but I have figured otu that I would need them here?

Upvotes: 0

Views: 14157

Answers (3)

Yatish
Yatish

Reputation: 145

A method can either return an int or a string (to say only one type). As other have suggested either change the return type to void if you do not want to return any thing.

If your problem is only having "Seat" string at the beginning, then you should not add the item to list box inside the method. Instead return an int from the method and add the item to your list box by doing something like this from the main block.

thelist.Items.Add("Seat " + ReadAndValidateSeatNr().ToString());

In this case your method will be retuning an int and can be used independently and you will still be able to add to your list box in the format you want it.

Upvotes: 0

Sadique
Sadique

Reputation: 22813

In your ReadAndValidateSeatNr() function you are not returning anything:

public int ReadAndValidateSeatNr()
{
     thelist.Items.Add(test); //test
     return;  //<--------- here nothing is being returned
}

My compiler gives the same error to me :P

enter image description here

Change return to void if you do not need to return anything:

public void ReadAndValidateSeatNr()
{
     thelist.Items.Add(test); //test
     //return; redundant statement - not even required in this case
}

If your requirement is something like 1 for "Seat 1", etc - go for an enum:

enum my_enum{ Seat1=1, Seat2= 2};

public int ReadAndValidateSeatNr()
{
        switch(test)
        {
             case "Seat 1":
             thelist.Items.Add(test); //test
             return (int)my_enum.Seat1;

             case "Seat 2":
             thelist.Items.Add(test); //test
             return (int)my_enum.Seat2;
        }
}

Upvotes: 6

Pascal_AC
Pascal_AC

Reputation: 541

If you don't want to return something in the ReadAndValidateSeatNr method you should change it like this:

public void ReadAndValidateSeatNr() 
{
    thelist.Items.Add(test);
}

Upvotes: 0

Related Questions