2014-01-14 4 views
1

Array listc#에 작성했으며 사용자가 양식의 데이터 입력을 통해 목록에 추가 할 수 있기를 바랍니다. 다음 코드를 입력했지만 오류가 발생했습니다. "Input string was not in the correct format." 사용자 입력을 가능하게하기 위해이를 수정하는 방법을 모르겠습니다.올바른 입력 문자열이 배열 목록의 올바른 형식이 아닙니다.

Arraylist hotelRooms = new ArrayList 

Public void AddCurrentItem() 
{ 
    HotelRoom hotelRooms = new HotelRoom 
Int.Parse(textboxRoomNumber.Text),comboBoxRoomType. Text, int.Parse (comboBoxFloorNumber.Text), comboBoxSeaView. Text, decimal.Parse (textBoxRoomRate. Text), comboBoxBooked. Text); 
hotelRooms. Add(hotelRooms) 

} 
+3

를? 누락 된 parantheses aftr 새로운 호텔 룸 단지 오타가 있습니까? – scheien

+1

int로 구문 분석하는 텍스트 상자에 숫자가 포함되어 있는지 확인하십시오. –

+0

[try-catch] (http://msdn.microsoft.com/ru-ru/library/0yd65esw.aspx), Luke :-) – Grundy

답변

0

int.Parse (string)는 문자열이 비어있는 경우 ArgumentFormatException을 발생시킵니다.

이 예제는 입력 문자열이 올바른 형식이 아님을 나타내는 예외를 throw합니다.

string myString = ""; 
int i = int.Parse(myString); 

그래서 호텔 룸 생성자에서 사용하는 입력을 확인해야합니다. HotelRoom 생성자를 치기 전에 입력을 변수로 확장 할 수 있습니다.

뭔가 같은 :

int roomNumber = 0; 
int.TryParse(textboxRoomNumber.Text, out roomNumber); 

string roomType = comboBoxRoomType.Text; 

그런 다음 빈방 개체가 일부 확인 할 인스턴스화하기 전에 : (brewity 생략 일부 코드)처럼 빈방 클래스보고 무엇을

if (roomNumber > 0 && !string.IsNullOrWhitespace(roomType)) 
{ 
    HotelRoom h = new HotelRoom(roomNumber, roomType); 
    hotelRooms.Add(h); 
} 
else 
{ 
    // return the variables to the ui, so that the user can do another 
    // selection if something is missing or wrong. 
} 
관련 문제