2012-10-05 5 views
2

송장 유형의 새 개체를 만들려고합니다. 필요한 매개 변수를 올바른 순서로 전달합니다.object.object에 가장 많이 오버로드 된 일치 항목에 잘못된 인수가 있습니다.

그러나 잘못된 인수가 포함되어 있다고 알려줍니다. 나는 여기서 아주 단순한 것을 간과 할 수도 있지만, 누군가는 그것을 지적 할 수 있습니다.

숙제를하고 있지만 Invoice.cs 파일이 프로젝트 용으로 포함되어 있습니다.

내가 찾고있는 유일한 해결책은 내 개체가 값을 허용하지 않는 이유입니다. 이전에 객체에 문제가 없었습니다. 여기

는 내가 가지고있는 코드 :

static void Main(string[] args) 
{ 
    Invoice myInvoice = new Invoice(83, "Electric sander", 7, 57.98); 
} 

그리고 여기에 실제 Invoice.cs 파일입니다 : 당신이 double 값을 전달하는 같은 곳

// Exercise 9.3 Solution: Invoice.cs 
// Invoice class. 
public class Invoice 
{ 
    // declare variables for Invoice object 
    private int quantityValue; 
    private decimal priceValue; 

    // auto-implemented property PartNumber 
    public int PartNumber { get; set; } 

    // auto-implemented property PartDescription 
    public string PartDescription { get; set; } 

    // four-argument constructor 
    public Invoice(int part, string description, 
     int count, decimal pricePerItem) 
    { 
     PartNumber = part; 
     PartDescription = description; 
     Quantity = count; 
     Price = pricePerItem; 
    } // end constructor 

    // property for quantityValue; ensures value is positive 
    public int Quantity 
    { 
     get 
     { 
     return quantityValue; 
     } // end get 
     set 
     { 
     if (value > 0) // determine whether quantity is positive 
      quantityValue = value; // valid quantity assigned 
     } // end set 
    } // end property Quantity 

    // property for pricePerItemValue; ensures value is positive 
    public decimal Price 
    { 
     get 
     { 
     return priceValue; 
     } // end get 
     set 
     { 
     if (value >= 0M) // determine whether price is non-negative 
      priceValue = value; // valid price assigned 
     } // end set 
    } // end property Price 

    // return string containing the fields in the Invoice in a nice format 
    public override string ToString() 
    { 
     // left justify each field, and give large enough spaces so 
     // all the columns line up 
     return string.Format("{0,-5} {1,-20} {2,-5} {3,6:C}", 
     PartNumber, PartDescription, Quantity, Price); 
    } // end method ToString 
} // end class Invoice 
+1

57.98 뒤에 'M'을 넣으십시오. 그대로, 그 숫자는 내가 믿고 10 진수로 변환 할 수없는 이중 상수이지만, 57.98M은 소수 상수입니다. –

답변

4

귀하의 방법은 decimal 매개 변수를 기대하고있다 (57.98). MSDN

(설명 부분 참조),

부동 소수점 유형과 소수점 형 사이에 암시 적 변환이 없습니다. 당신이 접미사 'm'또는 'M'을 추가해야 소수를 들어

따라서, 귀하의 경우, 대신에 57.98

This SO answer 목록 접미사의 모든 종류의 57.98m를 전달합니다.

+0

아. 고맙습니다! 나는 너무 늦은 프로그래밍을하는 것을 멈춰야한다! – user1721879

관련 문제