Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

error: cast to string

Status
Not open for further replies.

mkohl

Programmer
Feb 22, 2005
82
US
First and formost I must thank everyone who attempts to solve this problem. This is just a program to test some of the stuff I am learning.

errors During compilation:
code generation for property 'TotalPrice' failed. Error was:' cast from string"" to type 'Decimal' is not valid.'

code generation for property 'Quantity' failed. Error was:' cast from string"" to type 'Integer' is not valid.'

code generation for property 'PricePerItem' failed. Error was:' cast from string"" to type 'Decimal' is not valid.'

Inteface:
Interface StoreFields
Property product() As String
Property Quantity() As Integer
Property FirstName() As String
Property LastName() As String
Property PricePerItem() As Decimal
Property TotalPrice() As Decimal
End Interface

Main Form:
Public Class MyShop
Inherits System.Windows.Forms.Form
Structure Person
Dim ID As Integer
Dim Firstname As String
Dim Lastname As String
Dim Quantity As Integer
Dim Price As Decimal
Dim TotalPrice As Decimal
Dim Product As String
End Structure

Dim Myarray(0) As Person

Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

End Sub

Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RBBanana.CheckedChanged

End Sub

Private Sub RBApple_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RBApple.CheckedChanged





End Sub

Private Sub ToolBar1_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles ToolBar1.ButtonClick
RBApple.Checked = False
RBBanana.Checked = False
RBOrange.Checked = False

Select Case e.Button.Tag
Case "apples"
RBApple.Checked = True
Case "banana"
RBBanana.Checked = True
Case "orange"
RBOrange.Checked = True

End Select

End Sub

Private Sub RBOrange_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RBOrange.CheckedChanged

End Sub


Private Sub MyShop_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub GroupBox2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox2.Enter

End Sub


Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim x As New Person
Dim Price_apples As Decimal
Dim Price_oranges As Decimal
Dim price_Bananas As Decimal

Price_apples = 1.0
Price_oranges = 2.0
price_Bananas = 3.0
x.ID = UBound(Myarray)
x.Firstname = txtFname.Text
x.Lastname = txtLname.Text
x.Quantity = CInt(cboQuantity.Text)
If RBApple.Checked = True Then
x.Price = Price_apples
x.Product = "Apple"
ElseIf RBOrange.Checked = True Then
x.Price = Price_oranges
x.Product = "Oranage"
ElseIf RBBanana.Checked = True Then
x.Price = price_Bananas
x.Product = "Banana"
End If
x.TotalPrice = x.Quantity * x.Price
Myarray(UBound(Myarray)) = x
ReDim Preserve Myarray(UBound(Myarray) + 1)

MsgBox("customer added", , "info")

txtFname.Text = ""
txtLname.Text = ""
cboQuantity.Text = ""
RBApple.Checked = False
RBOrange.Checked = False
RBBanana.Checked = False
End Sub

Private Sub btnGet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGet.Click
Dim x As Person
Dim m As String

m = InputBox("Please enter ID number")
x = Myarray(CInt(m))
MyStoreBar1.FirstName = x.Firstname
MyStoreBar1.LastName = x.Lastname
MyStoreBar1.Quantity = (x.Quantity).ToString
MyStoreBar1.product = x.Product
MyStoreBar1.PricePerItem = x.Price
MyStoreBar1.TotalPrice = FormatCurrency(x.TotalPrice, 2)
MyStoreBar1.ButtonChecked = True



End Sub
End Class

Composite Control:
Public Class MyStoreBar
Inherits System.Windows.Forms.UserControl
Implements StoreFields

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Qnty As System.Windows.Forms.TextBox
Friend WithEvents Fname As System.Windows.Forms.TextBox
Friend WithEvents Lname As System.Windows.Forms.TextBox
Friend WithEvents Price As System.Windows.Forms.TextBox
Friend WithEvents Tprice As System.Windows.Forms.TextBox
Friend WithEvents btnCheckMark As System.Windows.Forms.Button
Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
Friend WithEvents cboproduct As System.Windows.Forms.ComboBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(MyStoreBar))
Me.Qnty = New System.Windows.Forms.TextBox
Me.Fname = New System.Windows.Forms.TextBox
Me.Lname = New System.Windows.Forms.TextBox
Me.cboproduct = New System.Windows.Forms.ComboBox
Me.Price = New System.Windows.Forms.TextBox
Me.Tprice = New System.Windows.Forms.TextBox
Me.btnCheckMark = New System.Windows.Forms.Button
Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)
Me.SuspendLayout()
'
'Qnty
'
Me.Qnty.Location = New System.Drawing.Point(72, 8)
Me.Qnty.Name = "Qnty"
Me.Qnty.Size = New System.Drawing.Size(56, 20)
Me.Qnty.TabIndex = 0
Me.Qnty.Text = ""
'
'Fname
'
Me.Fname.Location = New System.Drawing.Point(136, 8)
Me.Fname.Name = "Fname"
Me.Fname.Size = New System.Drawing.Size(72, 20)
Me.Fname.TabIndex = 1
Me.Fname.Text = ""
'
'Lname
'
Me.Lname.Location = New System.Drawing.Point(216, 8)
Me.Lname.Name = "Lname"
Me.Lname.Size = New System.Drawing.Size(88, 20)
Me.Lname.TabIndex = 2
Me.Lname.Text = ""
'
'cboproduct
'
Me.cboproduct.Items.AddRange(New Object() {"Apple", "Banana", "Orange"})
Me.cboproduct.Location = New System.Drawing.Point(312, 8)
Me.cboproduct.Name = "cboproduct"
Me.cboproduct.Size = New System.Drawing.Size(121, 21)
Me.cboproduct.TabIndex = 3
'
'Price
'
Me.Price.Enabled = False
Me.Price.Location = New System.Drawing.Point(440, 8)
Me.Price.Name = "Price"
Me.Price.Size = New System.Drawing.Size(72, 20)
Me.Price.TabIndex = 4
Me.Price.Text = ""
'
'Tprice
'
Me.Tprice.Enabled = False
Me.Tprice.Location = New System.Drawing.Point(520, 8)
Me.Tprice.Name = "Tprice"
Me.Tprice.Size = New System.Drawing.Size(72, 20)
Me.Tprice.TabIndex = 5
Me.Tprice.Text = ""
'
'btnCheckMark
'
Me.btnCheckMark.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.btnCheckMark.ForeColor = System.Drawing.SystemColors.ActiveBorder
Me.btnCheckMark.Image = CType(resources.GetObject("btnCheckMark.Image"), System.Drawing.Image)
Me.btnCheckMark.Location = New System.Drawing.Point(24, 8)
Me.btnCheckMark.Name = "btnCheckMark"
Me.btnCheckMark.Size = New System.Drawing.Size(32, 23)
Me.btnCheckMark.TabIndex = 6
Me.btnCheckMark.Visible = False
'
'ImageList1
'
Me.ImageList1.ImageSize = New System.Drawing.Size(16, 16)
Me.ImageList1.ImageStream = CType(resources.GetObject("ImageList1.ImageStream"), System.Windows.Forms.ImageListStreamer)
Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
'
'MyStoreBar
'
Me.Controls.Add(Me.btnCheckMark)
Me.Controls.Add(Me.Tprice)
Me.Controls.Add(Me.Price)
Me.Controls.Add(Me.cboproduct)
Me.Controls.Add(Me.Lname)
Me.Controls.Add(Me.Fname)
Me.Controls.Add(Me.Qnty)
Me.Name = "MyStoreBar"
Me.Size = New System.Drawing.Size(616, 40)
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Fname.TextChanged

End Sub

Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tprice.TextChanged

End Sub

Private Sub MyStoreBar_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
Public Property product() As String Implements StoreFields.product

Get
Return cboproduct.Text
End Get
Set(ByVal Value As String)
cboproduct.Text = Value
End Set
End Property
Public Property FirstName() As String Implements StoreFields.FirstName
Get
Return Fname.Text
End Get
Set(ByVal Value As String)
Fname.Text = Value
End Set
End Property

Public Property LastName() As String Implements StoreFields.LastName
Get
Return Lname.Text
End Get
Set(ByVal Value As String)
Lname.Text = Value
End Set
End Property

Public Property PricePerItem() As Decimal Implements StoreFields.PricePerItem
Get
Return Price.Text
End Get
Set(ByVal Value As Decimal)
Price.Text = Value
End Set
End Property

Public Property Quantity() As Integer Implements StoreFields.Quantity
Get
Return Qnty.Text
End Get
Set(ByVal Value As Integer)
Qnty.Text = Value
End Set
End Property

Public Property TotalPrice() As Decimal Implements StoreFields.TotalPrice
Get
Return Tprice.Text
End Get
Set(ByVal Value As Decimal)
Tprice.Text = Value
End Set
End Property
Public Property ButtonChecked() As Boolean
Get
Return btnCheckMark.Visible
End Get
Set(ByVal Value As Boolean)
btnCheckMark.Visible = Value

End Set
End Property

Private Sub prod_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboproduct.SelectedIndexChanged

End Sub
End Class
 
try using

DecimalVariable = decimal.parse(StringVariable)

or

DecimalVariable = CType(StringVariable, "Decimal")

or

IntegerVariable = Val(StringVariable)

there are plenty of ways to convert from a string to a number, play arround with it abit.

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Watch out for the dot-vs-comma thing. VAL always expects a dot as decimal separator, the other functions are "culture dependent", and might expect comma on european pc's.
 
So are you telling me that I need to declare my property variables as a string then when processing convert them to a decimal or integer?
 
So are you telling me that I need to declare my property variables as a string then when processing convert them to a decimal or integer?

No, I'm telling you that you can't set a numeric variable equal to an empty string. With Option Strict off, you can set the numeric variable equal to a numeric string, but the string has to contain a valid number. It's much safer to use decimal.parse or ctype().

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top