473,408 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Recieving error message "Value of type decimal cannot be converted..."

Hello,

I am taking my first quarter of programming and am having a VERY difficult time understanding jagged arrays. At the moment I am working on a project that is using two two dimensional jagged arrays and I am stuck. Here is the block of coding that is giving me headaches:

If IsNothing(g_decChargesArray(g_intActivePatientNumb er)) Then
numVisits = 0
Dim decTemp(g_intActivePatientNumber) As Decimal
g_decChargesArray(g_intActivePatientNumber) = decTemp
Dim strTemp(0) As String
strTemp(0) = g_dtmDate.ToString("d")
g_strVisit(g_intActivePatientNumber) = strTemp
Else
numVisits = g_decChargesArray(g_intActivePatientNumber).Length
End If
ReDim Preserve g_strVisit(g_strVisit.Length)
ReDim Preserve g_decChargesArray(g_decChargesArray.Length)
decCharges = CalcCharges()
If decCharges = -1 Then
MessageBox.Show("The Following Fields Will Only Accept Positive Numeric Values:" & vbCrLf & vbCrLf & "Length of Stay (Must be entered as whole number!)" & vbCrLf &
"Medical Charges" & vbCrLf & "Surgical Charges" & vbCrLf & "Lab Fees" & vbCrLf & "Physical Rehab Fees" &
vbCrLf & vbCrLf & "Please Review Your Entries and Correct Any Errors!")
Return
Else
g_decChargesArray(g_intActivePatientNumber) = decCharges
g_strVisit(g_intActivePatientNumber)(numVisits) = g_decTotal

End If

The line in bold is the only way I can think of to pass the amount of decCharges into the array g_decChargesArray.

Help!
Mar 20 '11 #1
3 4809
Hello, Neal,

It is difficult to tell for certain because the declaration of g_decChargesArray is not shown in the code that you have posted. Based on the prefix of its name, I guess that this is a global variable declared in some module. Just based on the name, I suspect that the elements of this array are required to themselves be decimal arrays. But you are trying to assign a scalar value.

Find the declaration for g_decChargesArray to confirm the type. If it is as I suspect, then you will want to make your assignment look something like:

g_decChargesArray(g_intActivePatientNumber)(N) = decCharges

where "N" is the index of the array element intended to hold the "Charges" datum.

Cheers (& hang in there),
Randy
Mar 20 '11 #2
That worked perfectly, thanks! And yes, you are right about the public declarations. Anything that is label g_*** has been declared a Public variable on a separate module.

I should have posted this as a two part problem, though. While this allows me to pass one set of data along in those arrays, the assignment is to have multiple visits for multiple patients. Like I said, I can get this working for one patient but I'm not sure how to get this going for more than that. The basic structure of the program is this:
The main form will appear, requesting the user to enter patient names. A form pops up for them to input their name. Below is the code for that form:

Public Class frmPatientName

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Uese global variable g_strFullName to store new patient name. This
' is done in order to pass along this string value from this form
' to all others used. IsNumeric method used to verify name is properly
' enterd.

g_strFullName = String.Empty
g_strFullName = txtFullName.Text.ToString

If IsNumeric(g_strFullName) Then
MessageBox.Show("Please enter a valid name for new patient")
Else
g_blnNameValid = True
ReDim Preserve g_strNameArray(g_strNameArray.Length)
g_strNameArray(g_strNameArray.Length - 1) = g_strPatientName
Me.Close()
txtFullName.Clear()
End If

End Sub
End Class

After, they will be able to select on of the names from the drop down box. This will bring up another form with the visit info. That form has the following code:

Public Class frmPatientData

Private Sub frmPatientData_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Takes the selected name of cboPatient on the main form and display's
' the string value in the appropriate label. Uses the global variable
' g_strPatientName in order to retrieve the proper name.
ReDim Preserve g_strVisit(g_intActivePatientNumber)(0)
ReDim Preserve g_decChargesArray(g_intActivePatientNumber)(0)
lblName.Text = g_strPatientName.ToString
txtStay.Clear()
txtMeds.Clear()
txtLab.Clear()
txtSurg.Clear()
txtPhysRehab.Clear()
txtDate.Clear()
End Sub
Private Sub btnInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInput.Click
Dim dtmValue As Date
g_blnDateValid = False
If Not Date.TryParse(txtDate.Text, dtmValue) Then
MessageBox.Show("Please enter a valid short date format for visit date")
Else
g_blnDateValid = True
g_dtmDate = dtmValue.ToString("d")

End If

g_intStay = 0
g_decMeds = 0
g_decSurg = 0
g_decLab = 0
g_decPhysical = 0
g_decTotal = 0
Dim decCharges As Decimal

Dim numVisits As Integer
If IsNothing(g_decChargesArray(g_intActivePatientNumb er)) Then
numVisits = 0
Dim decTemp(g_intActivePatientNumber) As Decimal
g_decChargesArray(g_intActivePatientNumber) = decTemp
Dim strTemp(0) As String
strTemp(0) = g_dtmDate.ToString("d")
g_strVisit(g_intActivePatientNumber) = strTemp
Else
numVisits = g_decChargesArray(g_intActivePatientNumber).Length
End If
ReDim Preserve g_strVisit(g_strVisit.Length)
ReDim Preserve g_decChargesArray(g_decChargesArray.Length)
decCharges = CalcCharges()
If decCharges = -1 Then
MessageBox.Show("The Following Fields Will Only Accept Positive Numeric Values:" & vbCrLf & vbCrLf & "Length of Stay (Must be entered as whole number!)" & vbCrLf &
"Medical Charges" & vbCrLf & "Surgical Charges" & vbCrLf & "Lab Fees" & vbCrLf & "Physical Rehab Fees" &
vbCrLf & vbCrLf & "Please Review Your Entries and Correct Any Errors!")
Return
Else
g_decChargesArray(g_intActivePatientNumber)(numVis its) = decCharges
g_strVisit(g_intActivePatientNumber)(numVisits) = g_dtmDate
End If

If g_blnDateValid Then
Me.Close()
Else
MessageBox.Show("Please Enter a Visit Date Before Closing the Form!")
End If
End Sub
Private Sub mthCalendar_DateChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles mthCalendar.DateChanged
Dim dateTemp As Date
dateTemp = mthCalendar.SelectionRange.Start.Date
End Sub
' Function used to both validate and calculate all charges incurred during hospital visit

Function CalcCharges() As Decimal
Dim decTotalCharge As Decimal
Try
If Not Integer.TryParse(txtStay.Text, g_intStay) OrElse g_intStay < 0 Then
Return -1
End If
If Not Decimal.TryParse(txtMeds.Text, g_decMeds) AndAlso Not Decimal.TryParse(txtMeds.Text, g_decMeds) > 0 Then
Return -1
End If
If Not Decimal.TryParse(txtSurg.Text, g_decSurg) AndAlso Not Decimal.TryParse(txtSurg.Text, g_decSurg) > 0 Then
Return -1
End If
If Not Decimal.TryParse(txtLab.Text, g_decLab) AndAlso Not Decimal.TryParse(txtLab.Text, g_decLab) > 0 Then
Return -1
End If
If Not Decimal.TryParse(txtPhysRehab.Text, g_decPhysical) AndAlso Not Decimal.TryParse(txtPhysRehab.Text, g_decPhysical) > 0 Then
Return -1
End If
decTotalCharge = (g_intStay * 350) + g_decMeds + g_decSurg + g_decLab + g_decPhysical
g_blnDataValid = True
Catch ex As Exception
Return -1
g_blnDataValid = False
End Try
Return decTotalCharge
End Function

Once the user is ready to calculate any charges, (the input button closes that patient data form), the main form appears again. There, you select a patient name from a combo drop down box. Then another combo drop box populates the dates of their visits. The calculate button then retrieves the total charge from that visit. The code for these are as follows:

Private Sub cboPatientTotal_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboPatientTotal.SelectedIndexChanged
If cboPatientTotal.SelectedIndex < 0 Then
cboTotalVisit.SelectedIndex = -1
ElseIf cboPatientTotal.SelectedIndex >= 0 Then
g_intActivePatientNumber = cboPatientTotal.SelectedIndex
cboTotalVisit.Items.Clear()
Dim intCount As Integer

For J As Integer = 0 To g_strVisit(g_intActivePatientNumber).Length - 1
cboTotalVisit.Items.Add(g_strVisit(g_intActivePati entNumber)(J))
Next


End If
End Sub
End Class

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
' Displays hospital charges for respective patient on the selected day
' as well as total hostpital charges.
If cboPatientTotal.SelectedIndex = -1 Then
MessageBox.Show("Please select a patient name")
Return
ElseIf cboTotalVisit.SelectedIndex = -1 Then
MessageBox.Show("Please select a visit date for the patient")
Return
ElseIf cboTotalVisit.SelectedIndex >= 0 Then
g_intActivePatientNumber = cboTotalVisit.SelectedIndex
For J As Integer = 0 To g_decChargesArray(g_intActivePatientNumber).Length - 1
lblTotal.Text = g_decChargesArray(g_intActivePatientNumber)(J).ToS tring("c")
Next
End If

End Sub


I know how difficult this is just simply looking at the code on a website and with my brief description, but any help anyone can offer would be greatly appreciated.

Thanks
Mar 21 '11 #3
Hello, Neal,

You say this is your first quarter programming, which leads me to believe this is some sort of assignment that you are working on to learn about jagged arrays. If that is so, OK. But if you don't need to use jagged arrays for pedantic reasons I would not have chosen them to store data for this type of application. Without knowing more details about the requirements, I would have probably created a class structure with Patient and Visit classes to hold the required data. Also, you should generally avoid using global variables. Global variables typically lead to significant maintenance problems. They make debugging difficult and reduce flexibility and interoperability.

I haven't studied the posted code in detail, but looking at the last few lines it seems that you are looping through the g_decChargesArray and placing each successive item for the patient into the text property of a form label. The user will never see anything except the last one. I'm guessing that you actually intend to total these items and show the total. To do this change your code to something like:
Dim decTotalCharge as Decimal = 0
For J As Integer = 0 To g_decChargesArray(g_intActivePatientNumber).Length - 1
decTotalCharge = decTotalCharge + g_decChargesArray(g_intActivePatientNumber)(J)
Next J
lblTotal.Text = decTotalCharge.ToString("c")
Cheers,
Randy
Mar 21 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: ReignMan | last post by:
I'm running JDK 1.4.0_01, Xalan2.5.1 and Tomcat 4.1.27. XSL transformations work, yet when running in debug mode (Eclipse IDE), I get the following: 990S2html.xsl:3:80: Element type...
2
by: JohnD | last post by:
I am using XP Pro and when I try and run an ASP.NET page on my own server I recieve the following error messag Server cannot access application directory ..(web directory here)... The directory...
1
by: ArcadeJr | last post by:
Good morning all! I have been getting a Run-time Error message #3464 - Data Type mismatch in criteria expression. While trying to run a query. I have a database where the field Asset_Number...
1
by: Sebastian | last post by:
Hi, Why do I get this message for the code below? I am using Green Hills Multi 4.0.5. struct timespec drm_tstamp; /* time stamp */ Regards, Sebastian
0
by: WebDev2 | last post by:
I can't get AJAX.NET Pro to work. I keep getting a Compiler Error Message: CS0246: The type or namespace name 'AjaxPro' could not be found (are you missing a using directive or an assembly...
4
by: infinetinc | last post by:
I'm receiving the following error when this site is published to the production environment: Compiler Error Message: CS0246: The type or namespace name 'GridViewHelper' could not be found (are you...
3
by: Ady1 | last post by:
Hi, I'm getting this error intermitantly in a custom configuration section in my ASP.NET website. From what I can see 0.175 is a valid string to be converted to a decimal! And most of the time...
1
by: Josta7 | last post by:
Hi. I've been using Access for a couple of years now, but everything I know about it is self-taught. This makes me a little self-conscious/worried - I feel like I know how to do a lot, but I could...
24
by: carnold | last post by:
Hello, I'm a developer coming from C++ and Java ... I've going thru "Effective C#" (which highly recommend for people coming from other languages wanting to learn C#), and it states that "value...
3
by: K Viltersten | last post by:
I've been informed that a webserver sending a XML file is supposed to add "Content-Type: text/xml header". I'm not questioning that infromation but i'm unsure what was ment by it. The XML i get...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.