문제

I work throu an EF tutorial. Now there is a Listing:

Using context As New northwindEntities
            Dim query = From prod In context.Products
                        Where prod.UnitPrice = context.Products.Max(Function(x) x.UnitPrice)
                        Select prod.ProductName, prod.UnitPrice
            For Each item In query

                'works fine
                Console.WriteLine("{0}, {1}", item.ProductName, item.UnitPrice)
                'Not working error see below:
                MsgBox(item.ProductName, " ", item.UnitPrice.ToString)

            Next
        End Using

I get the Error: Conversion from string to type 'Integer' is not valid. Doesent matter if I write item.UnitPrice.ToString or item.UnitPrice, error is still the same. But I just have no idea why? Could anyone help me here?

도움이 되었습니까?

해결책

You're using the MsgBox function wrong.

Look what parameters it accepts:

Public Function MsgBox( _
   ByVal Prompt As Object, _
   Optional ByVal Buttons As MsgBoxStyle = MsgBoxStyle.OKOnly, _
   Optional ByVal Title As Object = Nothing _
) As MsgBoxResult

When you call MsgBox(item.ProductName, " ", item.UnitPrice.ToString) you pass " " for the Buttons parameter, which should be of type MsgBoxStyle (or at least the underlying type: Integer, which VB.Net converts to MsgBoxStyle implicitly if you use Option Strict Off).


You're probably looking for something like:

MsgBox(String.Format("{0} {1}", item.ProductName, item.UnitPrice))

or

MsgBox(item.ProductName, MsgBoxStyle.OKOnly, item.UnitPrice)

다른 팁

http://msdn.microsoft.com/en-us/library/139z2azd%28v=vs.90%29.aspx second parameter of the MsgBox function takes an Enum (pretty much an integer) not a string.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top