There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
 
Software Development
Tag Cloud
audio bios blue screen boot bsod card computer connection crash dell drivers error excel firefox freeze freezing google hard drive hardware hijackthis install internet laptop linux malware network no sound outlook problem reboot redirect router screen server slow sound speakers spyware startup trojan usb video virus vista vundo windows windows 7 windows vista windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Software Development >
Moduals

Tip: Click here to scan for System Errors and Optimize PC performance
[ Sponsored Link ]

Closed Thread
 
Thread Tools
eddionne's Avatar
Junior Member with 5 posts.
 
Join Date: Feb 2005
Location: Tavares, Fl - Methuen, Ma - Naples,
Experience: Intermediate
16-Mar-2005, 10:21 PM #1
Moduals
I am new to and confused by VB.Net!

I am trying to learn to use VB.Net and the book that I am using requires writing a program to calculate a discount amount on an order, to display the discount and deducted it from the order amount to show the total charge.

I have written the program and it works fine, I am now trying to create modules for the program. I have written the following code:

This appears in the class area:
"Call mInvoiceTotal(txtOrderTotal.Text, lblDiscountAmount.Text, lblInvoiceTotal.Text)"

I placed this code in a modual:
"Public Function mInvoiceTotal(ByVal OrderTotal As Decimal, ByVal DiscountAmount As Decimal, ByVal InvoiceTotal As Decimal) As Decimal

Select Case OrderTotal
Case Is >= 500
DiscountAmount = 0.2

Case Is >= 250
DiscountAmount = 0.15

Case Is >= 100
DiscountAmount = 0.1

Case Is < 100
DiscountAmount = 0

End Select

End Function"

When I run debug stepping through this function I see all the correct answers, but when I get to the end of the function and return to the calling program all the values go away and I get no results. What is wrong with my code?

Any help would be welcome.

Thank you

eddionne
eddionne's Avatar
Junior Member with 5 posts.
 
Join Date: Feb 2005
Location: Tavares, Fl - Methuen, Ma - Naples,
Experience: Intermediate
13-Nov-2005, 10:11 PM #2
confused
I do not have a reply to your question but I am curious; is there another eddionne out there?

This eddionne did not post this question nor the one about duel boot.

If another ed dionne exists email me at eddionne@hotmail.com
OBP's Avatar
OBP OBP is offline
Computer Specs
Distinguished Member with 9,238 posts.
 
Join Date: Mar 2005
Location: UK
Experience: An old Basic Programmer
14-Nov-2005, 05:41 AM #3
To the poster, I do not know VB.net but I do know VBA and I think your VB's "logic" is not correct. If a value is greater than 500 then it is also greater than 100 and 250 therefore when it meets the requirement of >= 500 or 250 you need to exit the select.
Have you dimensioned the variables as "public"?
__________________
OBP
I do not give up easily
cristobal03's Avatar
Distinguished Member with 2,994 posts.
 
Join Date: Aug 2005
Experience: Advanced
15-Nov-2005, 01:21 PM #4
To OP:

Your function mInvoiceTotal has no return value set. The argument DiscountAmount is being passed ByVal not ByRef, so any change to DiscountAmount remains local to the function and is not reflected in the calling procedure.

I think you've confused a couple of things. If you want the function itself to have a return value of type Decimal, instead of your

Code:
DiscountAmount = 0.2
DiscountAmount = 0.15
DiscountAmount = 0.1
DiscountAmount = 0
you'd want to use something like this:

Code:
mInvoiceTotal = 0.2
mInvoiceTotal = 0.15
mInvoiceTotal = 0.1
mInvoiceTotal = 0
A function's return value is the value assigned to the function's name within the procedure. With the above changes, you'd call mInvoiceTotal using the following model:

Code:
Dim decSomeOrder       As Decimal
Dim decSomeDiscount    As Decimal
Dim decSomeTotal       As Decimal
Dim decTheReturnValue  As Decimal

decSomeOrder = 312
decSomeDiscount = 0
decSomeTotal = 0.03

decTheReturnValue = mInvoiceTotal(decSomeOrder, _
                                  decSomeDiscount, _
                                  decSomeTotal)

' decSomeOrder contains 312 still
' decSomeDiscount contains 0 still
' decSomeTotal contains 0.03 still
' decTheReturnValue contains 0.15 now
On the other hand, if you want to affect the variables in the calling procedure directly, you should change your Function declaration like so:

Code:
Public Function mInvoiceTotal(ByRef OrderTotal As Decimal, _
                              ByRef DiscountAmount As Decimal, _
                              ByVal InvoiceTotal As Decimal)

  ' Select Case statements generally list cases in ascending
  ' order; also, Select Case is generally used for exclusive
  ' conditions.  As OBP pointed out, your cases are
  ' non-exclusive.  You need to disambiguate this contitional
  ' control block somehow.  The easiest way, as OBP noted,
  ' is to include an Exit Select statement in each Case.  If
  ' you don't want to use Exit Select, put your cases in
  ' ascending order.

  Select Case OrderTotal
    Case Is >= 500
      DiscountAmount = 0.2
      Exit Select

    Case Is >= 250
      DiscountAmount = 0.15
      Exit Select

    Case Is >= 100
      DiscountAmount = 0.1
      Exit Select

    Case Is < 100
      DiscountAmount = 0

  End Select
End Function
And you'd use the function like so:

Code:
Dim decSomeOrder      As Decimal
Dim decSomeDiscount   As Decimal
Dim decSomeTotal      As Decimal

decSomeOrder = 312
decSomeDiscount = 0.64859
decSomeTotal = 0.03

mInvoiceTotal decSomeOrder, decSomeDiscount, decSomeTotal

' decSomeOrder contains 312 still
' decSomeDiscount contains 0.15 now
' decSomeTotal contains 0.03 still
Is the difference clear? Passing an argument ByRef means the called procedure doesn't necessarily have to return a value because it affects the values in the calling procedure directly. On the other hand, if you want the called procedure to interact with the calling procedure and want to pass arguments ByVal, the called procedure needs a return value.

Here's a simple example. If you wrote a book report, and you wanted me to edit it, you could do two things. You could give me a photocopied version of the report (think: ByVal), or you could send me a link to the file itself (think: ByRef). If I had a photocopy, I'd mark my changes on it and hand the photocopy back to you for you to do something with. If I had a link, I could open the file directly--while it was still on your computer, even--and change it directly. If I had a photocopy and didn't give it back to you, I haven't given you a return value. Reciprocally, if you haven't given me a link to the report, I have no reference to the report. So you need either a value and a return, or a defined reference.

I personally don't use ByRef unless I have to; passing references is arguably more efficient but it's a bad idea for beginning coders; references are considerably harder to debug than values.

You probably also want to consider how you're using data types. The Decimal data type is the largest floating-point precision data type; for your application, it looks like you could get by using Singles instead of Decimals.

If none of this makes sense, post back and I'll try to explain a bit more.

chris.

[edit]
Added a couple of things I left out.
[/edit]

[edit 2]
Added OBP's logic about Select Case to my code example.
[/edit 2]

[edit 3]
Just realized OP posted 8 months ago. If anyone else is curious, though, post back and I'll explain a bit more.
[/edit 3]

Last edited by cristobal03 : 15-Nov-2005 03:59 PM.
Closed Thread Bookmark and Share

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Smart Search

Find your solution!



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.

Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -5. The time now is 05:30 PM.
Copyright © 1996 - 2009 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.
Powered by Cermak Technologies, Inc.