none
Homework help RRS feed

  • Question

  •  

    PrivateSubButton1_Click(ByValsender AsSystem.Object, ByVale AsSystem.EventArgs) HandlesbtnCalc.Click

           

    DimintAdultPrice AsDecimal


           

    DimintMult AsInteger


           

    DimintAdultSales AsDecimal


           

    DimintChildPrice AsDecimal


           

    DimintMult2 AsInteger


           

    DimintChildSales AsDecimal


           

    DimintGross AsDecimal


           

    DimintHalfAdult AsDecimal


           

    DimintHalfChild AsDecimal


           

    DimintHalfGross AsDecimal


           

    'Values


            intAdultPrice = Val(_intAdultTicketTxt)

            intMult = Val(_intMultTxt)

            intAdultSales = Val(_intAdultSalesTxt)

            intChildPrice = Val(_intChildTicketTxt)

            intMult2 = Val(_intMult2Txt)

            intChildSales = Val(_intChildSalesTxt)

            intGross = Val(_intGrossTxt)

            intHalfAdult = Val(_intHalfAdultSalesTxt)

            intHalfChild = Val(_intHalfChildSalesTxt)

            intHalfGross = Val(_intHalfGrossTxt)

           

    'Calculation


            intAdultSales = intMult * intAdultPrice

            intChildSales = intMult2 * intChildPrice

            intGross = intChildSales + intAdultSales

            intHalfAdult = 2 / intAdultSales

            intHalfChild = 2 / intChildSales

            intHalfGross = 2 / intGross

    The value highlighted in bold is giving me trouble please help.. this assignment is due tomorrow :/

    Sunday, February 17, 2013 5:42 AM

All replies

  • What sort of trouble?  Is there an error message when you run the application, or is the compiler giving you an error?  What is the message?  The message usually give you a pretty good guide for fixing the problem.

    If the error is occurring at run time, what is the value of _intAdultTicketTxt when the error occurs?

    • Edited by Acamar Sunday, February 17, 2013 7:49 AM sp
    Sunday, February 17, 2013 6:20 AM
  • Homework help SPECIFICALLY is not done here.

    Renee


    "MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me

    • Proposed as answer by Renee Culver Sunday, February 17, 2013 8:46 AM
    Sunday, February 17, 2013 8:45 AM
  • Try to avoid the old Val function. Since Framework 1.1 the Conversion functions are very good for that 

    http://msdn.microsoft.com/en-us/library/s2dy91zy.aspx

    But before you can do that you have to test of those are numeric. For that has VB the extremely powerful function IsNumeric

    http://msdn.microsoft.com/en-us/library/6cd3f6w1(v=VS.90).aspx

    Be aware you have to do it yourself. Getting help in the way of code is strongly seen as not done in a forum for homework.


    Success
    Cor

    Sunday, February 17, 2013 10:47 AM
  • Homework help SPECIFICALLY is not done here.

    That is not correct.  Please refer to the code of conduct here:
    http://answers.microsoft.com/en-us/Page/faq#faqCodeConduct3

    Some participants choose not to provide answers to homework.  That is not the same as helping with homework.

    Sunday, February 17, 2013 9:33 PM
  • Homework help SPECIFICALLY is not done here.

    That is not correct.  Please refer to the code of conduct here:
    http://answers.microsoft.com/en-us/Page/faq#faqCodeConduct3

    Some participants choose not to provide answers to homework.  That is not the same as helping with homework.

    Acamar, 

    Although you are right that the message from Renee is confusing can your message be seen as a part of the code of conduct. Correcting persons about their grammar is a part of the code of conduct which should not be done. 

    Probably Renee meant. Doing somebodies homework is not done here. Like Renee, you are already long enough a contributor to these forums (and probably other forums) to know that. 

    I do now the same like you, so please let us stop to tell other long time contributors what they have done wrong, simply ignore that kind of messages. 

    If it is about newbies it can be helpful of course.


    Success
    Cor




    Monday, February 18, 2013 10:15 AM
  • As Cor told you, avoid using "Val", 

      When you need to change the type of a number from a text to a numeric type like decimal or integer, you should always use the function TryParse.

    the reason for that is it can never be guaranty that the string actually contains a text that can be recognized as a number

    the best way to use TryParse is to use it in a If Statement.

    This example of code will put in "intAdultPrice" the value in the text "_intAdultTicketTxt" if the system can recognize the value from the text, otherwise it will pop the message box

            If Not Decimal.TryParse(_intAdultTicketTxt, intAdultPrice) Then
                MsgBox("text in ""_intAdultTicketTxt"" cannot be parse as Decimal Value")
            End If

      You can also use "Integer.TryParse",   "Double.TryParse", ....etc ... 
    • Edited by Crazypennie Monday, February 18, 2013 11:44 AM
    Monday, February 18, 2013 11:43 AM
  • As Cor told you, avoid using "Val", 

      When you need to change the type of a number from a text to a numeric type like decimal or integer, you should always use the function TryParse.


    Luke,

    I see this endless times in this forum. But why that dirty System.TryParse function while VB has such an awful good IsNumeric function. Once there was nothing for C# programmers who had to do it with a kind of try catch around the parse.

    The Tryparse is completely different in use from about 99,9999% of all other .Net functions. Be aware the test on the same but in the first .Net versions was no function to test on numeric s available beside that VB IsNumeric.

    In fact it is this code in VB

        Public Function IntMYOwnTryParse(Value As String, ByRef result As Integer) As Boolean
            Try
                result = Int32.Parse(Value)
            Catch 
                Return False
            End Try
        End Function


    Like I always write it seems to be made by a JavaScripter who got an assignment to make such a kind of function. It follows those rules.


    Success
    Cor






    • Edited by Cor Ligthert Monday, February 18, 2013 1:42 PM typo
    Monday, February 18, 2013 12:14 PM
  • Because:

        IsNumeric do not guaranty a successful cast

          IsNumeric("300")  -> :)         Cbyte("300")   ->  :(

          IsNumeric("-32")  -> :)         CUint("-32")   ->  :(

          IsNumeric("999999999999")  -> :)         CInt("999999999999")   ->  :(

    Also, Tryparse uses A nice algorithm with pointers to validate and do numerical translation and then uses BlockAlloc into the stack to test if the value overflows.

    That is way better than trying to do the cast and then pickup the pieces if it fails


    • Edited by Crazypennie Monday, February 18, 2013 2:14 PM
    Monday, February 18, 2013 2:12 PM
  • The IsNumeric is working in the cases you shown. cbyte("300") is a literal not a numeric.

    Renee


    "MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me

    Monday, February 18, 2013 3:17 PM
  • The IsNumeric is working in the cases you shown. cbyte("300") is a literal not a numeric.

    Renee


    "MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me

    Exactly,.... The numeric return true and the cast fails.

    That is why IsNumeric don't guaranty that the cast from literal to numeric will work, it depend on the type the literal is to be cast to

    • Edited by Crazypennie Monday, February 18, 2013 3:36 PM
    Monday, February 18, 2013 3:26 PM
  • Yea Canada has people who come from Italia therefore is Italian spoken in Canada. That is more true than what you show currently. 

    Isnumeric and tryparse test if a string is numeric (not even a number), to what you convert it then is completely irrelevant. 

    However, if you really want to do it nice and avoid exponent values and things like that, you've to test the characters in a textbox (if you use that). For instance using this code.

    Public Class Form1
        Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
            If Not Char.IsDigit(CChar(e.KeyChar)) AndAlso Not {"."c, ","c, "-"c, ControlChars.Back}.Contains(e.KeyChar) Then
                e.Handled = True
            End If
        End Sub
    End Class

    By the way, the CByte('3000") fails because not that it is not numeric but that the value is to high. So the try in the tryparse is correct that it throws an exception. But that is not because it is not numeric.

    And before I forget it. The Isnumeric works the same as the TryParse with the exception that it has no byref argument in which is returned the tried cast. So you can say sometimes it works a pico or less second faster, but I keep it find dirty to return a value in an argument in OOP languages.

    And be aware, I wrote, I use the tryparse in C#, there is nothing better in that program language.


    Success
    Cor





    Monday, February 18, 2013 3:57 PM

  • By the way, the CByte('3000") fails because not that it is not numeric but that the value is to high. So the try in the tryparse is correct that it throws an exception. But that is not because it is not numeric.


    Success
    Cor


    Yes, I understand that Cor, it is my point here, if a user enter a text into a text box, it is not helpful to know if the value is numeric or not, 

    If in the code the value has to be cast to an Uint32, if the user has entered "-32" it is as wrong than if he has enter "TRUY%TRY" if the value represent the number of bananas in a box and has to be cast to a UInt32 ---  because the literal value cannot be cast to the expected type even if it is numeric

    Monday, February 18, 2013 4:23 PM
  • And my point is that I find it such a dirty way of using methods which should not be done in an OOP program languages. A kind of lazy job just a translation how it was done in C# in framework 1.0 and 1.1.

    Look at the code part I gave addressed to you, it is the way it was then translated to C# done in those versions. I translated it to VB because I don't want to put C# code in this forum. 


    Success
    Cor


    Monday, February 18, 2013 4:36 PM
  • The problem with handling the keypress is that it doesn't help if the user does a paste into the textbox.

    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." JohnWein

    Monday, February 18, 2013 4:47 PM
  • Actually, I think that the way it work is coming from the way C++ uses function,

       The function output thru its arguments and return Success / fail

    For this, I have to agree with you that it is far from the VB Philosophy of doing the thing

    But other than that, the underlying code  is nice and solid. 

    And unless you choose to use C++ to re-implement it, I cannot see a way to do it better ... We can just make it more into " à là VB "

    Monday, February 18, 2013 4:48 PM
  • Furthermore, IsNumeric works only on type Double numbers.  If you require an integer, it will still return True if the value is a floating point number, and you will need more code to make sure the value is an integer.  That's why I prefer using TryParse.

    Regarding the homework assignment with all the Val code, I suspect it's because the instructor is using an outdated textbook.  Most of the textbooks I've seen still use the legacy functions, since the authors did not bother upgrading their code to the TryParse method from earlier editions.


    Solitaire


    • Edited by Solitaire Monday, February 18, 2013 5:15 PM
    Monday, February 18, 2013 5:07 PM
  • Furthermore, IsNumeric works only on type Double numbers.  If you require an integer, it will still return True if the value is a floating point number, and you will need more code to make sure the value is an integer.  That's why I prefer using TryParse.

    Regarding the homework assignment with all the Val code, I suspect it's because the instructor is using an outdated textbook.  Most of the textbooks I've seen still use the legacy functions, since the authors did not bother upgrading their code to the TryParse method from earlier editions.


    Solitaire


    Where you got this nonsense. Isnumeric is about Strings. The same likewise the valuetype.Tryparse first argument

    But you are right in the fact that, many fools have written this kind of nonsense and more about Isnumeric

    But that it is so much parroted does not mean it is true.


    Success
    Cor




    Monday, February 18, 2013 6:28 PM
  • Actually, I think that the way it work is coming from the way C++ uses function,

       The function output thru its arguments and return Success / fail

    For this, I have to agree with you that it is far from the VB Philosophy of doing the thing

    But other than that, the underlying code  is nice and solid. 

    And unless you choose to use C++ to re-implement it, I cannot see a way to do it better ... We can just make it more into " à là VB "

    Luke,

    That I wrote more times in this thread, but I find consistency in code more important than all kind of old boys tricks. That makes learning (and therefore maintenance) of a program language much easier. I hope you understand I know more program languages than VB and use JavaScript to give the end of that range.

    :-)


    Success
    Cor

    Monday, February 18, 2013 6:40 PM
  • I sure  understand what you mean Cor, however, (may be sadly), it is the only tool we have that link the parsing of a literal to the type of the variable we need to cast to. 

    If we don't use this TryParse, the only options are to do the parsing code or use a try/Catch

    ---------

    Try/Catch ... it is just a plaster to hide the problem

    Doing a strong implementation of the parsing, not every one can do it, certainly not a newby

    Monday, February 18, 2013 7:20 PM
  • Luke, 

    I do it consequently


    If IsNumeric(TheGivenString) then
        TheResultDecimalValue = CDec(TheGivenString)
    End if

    Be aware it has the same quantity of typed (not automated typed) rows.

    And in C# I use the TryParse, there is nothing better. 


    Success
    Cor

    Monday, February 18, 2013 7:25 PM
  • I like the TryParse methods.  Two birds, one stone.

            If Not Decimal.TryParse(TheGivenString, TheResultDecimalValue) Then
                'error
               
            End If

    FYI - The code above is three times as fast as this

                If IsNumeric(TheGivenString) Then
                    TheResultDecimalValue = CDec(TheGivenString)
                End If


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." JohnWein



    • Edited by dbasnett Monday, February 18, 2013 7:44 PM
    Monday, February 18, 2013 7:39 PM

  • FYI - The code above is three times as fast as this

                If IsNumeric(TheGivenString) Then
                    TheResultDecimalValue = CDec(TheGivenString)
                End If



    Two times, because it is in fact the same code and in the TryParse is the CDec only one times done, but that I wrote already in this thread. Be aware I've seldom seen programs where users need to reply in one pico second.

    Also I never have had programs where that pico second did make any difference as it was about batch data processing or Internet related software.

    And I wrote already, I don't like old boys code. which saves 1 pico second but is not according to the overall way a program is made.. 

    Also be aware that they both use the Try and Catch internally so there is in both a wait moment if the catch is the first time thrown.


    Success
    Cor


    Monday, February 18, 2013 7:55 PM
  • TryParse do not use TryCatch internaly

    Monday, February 18, 2013 7:59 PM
  • It was three, I timed it.  Use whatever works.


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." JohnWein

    Monday, February 18, 2013 8:08 PM
  • I'm happy to see that you are concerned about execution speed! Saving 1 pico second is not too important unless the code is called a million or so times. It's not just 'good ole boy code' that's concerned with execution speeds...it should be ALL code. There are WAY too many amateurs writing code and who don't know/care about execution speed, after all Microsoft must make a profit, right?

    Renee


    "MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me

    Monday, February 18, 2013 8:15 PM
  • I will restate what I posted earlier, when I said that IsNumeric only works with type Double numbers, what I meant to say was:   

    IsNumeric will return True if it detects either an integer or a floating point number.  If you require only an integer, then you need to use more code.  TryParse can specify the exact type.   Try this:

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim inum As Integer, snum As String 'Ask user to enter an Integer. Assume he enters a Double instead. snum = "25.67" If IsNumeric(snum) Then MessageBox.Show(snum & " is an integer", "IsNumeric") Else MessageBox.Show(snum & " is not an integer", "IsNumeric") End If If Integer.TryParse(snum, inum) Then MessageBox.Show(snum & " is an integer", "TryParse") Else MessageBox.Show(snum & " is not an integer", "TryParse") End If End Sub



    Solitaire


    • Edited by Solitaire Monday, February 18, 2013 9:03 PM
    Monday, February 18, 2013 9:01 PM
  • PrivateSubbtnCalc_Click(ByValsender AsSystem.Object, ByVale AsSystem.EventArgs) HandlesbtnCalc.Click

           

    'Variable declaration


           

    DimintPackav AsInteger


           

    DimintPackbv AsInteger


           

    DimintPackcv AsInteger


           

    DimdblPriceav AsDouble= 99

           

    DimdblPricebv AsDouble= 199

           

    DimdblPricecv AsDouble= 299

           

    DimdblPackafullv AsDouble


           

    DimdblPackbfullv AsDouble


           

    DimdblPackcfullv AsDouble


           

    'Value identity


            intPackav =

    CInt(intPacka.Text)

            intPackbv =

    CInt(intPackb.Text)

            intPackcv =

    CInt(intPackb.Text)

            lbresult.Text =

    " "


           

    'Calculations


           

    IfIsNumeric(intPackav) AndintPackav > 0 Then


               

    SelectCase(intPackav)

                   

    CaseIs10 to 19

                        dblPackafullv = (dblPriceav - (dblPriceav * 0.2)) * intPackav

                   

    CaseIs20 to 49

                        dblPackafullv = (dblPriceav - (dblPriceav * 0.3)) * intPackav

                   

    CaseIs50 to 99

                        dblPackafullv = (dblPriceav - (dblPriceav * 0.4)) * intPackav

                   

    CaseIs> 100

                        dblPackafullv = (dblPriceav - (dblPriceav * 0.5)) * intPackav

               

    EndSelect


               

    IfIsNumeric(intPackbv) AndintPackbv > 0 Then


                   

    SelectCase


                       

    CaseIs10 to 19

                            dblPackbfullv = (dblPricebv - (dblPricebv * 0.2)) * intPackbv

                       

    CaseIs20 to 49

                            dblPackbfullv = (dblPricebv - (dblPricebv * 0.3)) * intPackbv

                       

    CaseIs50 to 99

                            dblPackbfullv = (dblPricebv - (dblPricebv * 0.4)) * intPackbv

                       

    CaseIs> 100

                            dblPackbfullv = (dblPricebv - (dblPricebv * 0.5)) * intPackbv

                   

    EndSelect


                   

    IfIsNumeric(intPackcv) AndintPackcv > 0 Then


                       

    SelectCase

                           

    CaseIs10 to 19

                                dblPackcfullv = (dblPricecv - (dblPricecv * 0.2)) * intPackcv

                           

    CaseIs20 to 49

                                dblPackcfullv = (dblPricecv - (dblPricecv * 0.3)) * intPackcv

                           

    CaseIs50 to 99

                                dblPackcfullv = (dblPricecv - (dblPricecv * 0.4)) * intPackcv

                           

    CaseIs> 100

                                dblPackcfullv = (dblPricecv - (dblPricecv * 0.5)) * intPackcv

                       

    EndSelect


                        lbresult.Text =

    "Package A : "& FormatCurrency(dblPackafullv) & vbCrLf &

                       

    "Package B : "& FormatCurrency(dblPackbfullv) & vbCrLf & "Package C : "&

                        FormatCurrency(dblPackcfullv) & vbCrLf &

    "Total : "&

                        FormatCurrency(dblPackafullv + dblPackbfullv + dblPackcfullv)

                   

    EndIf


               

    EndIf


           

    EndIf


    I Keep getting expected errors now :/ I am trying my hardest to learn.. I work full time and I do this on the side. Please help

       

    EndSub

    Tuesday, February 19, 2013 4:55 AM
  • BTW I really do appreciate the pointers this one took me about 2 hours to layout and jot down, I felt like it looked right... but I think I may be missing something I keep getting "relational operator expected" and "expression expected"
    Tuesday, February 19, 2013 4:59 AM
  • Your code is very difficult to read.

    Please post the exact error message that you are getting and the line at which the error occurs.  The error message usually provides a good indication of what is needed to fix the problem.  You should also post the values of relevant variables.

    • Edited by Acamar Tuesday, February 19, 2013 11:50 AM sp
    Tuesday, February 19, 2013 11:48 AM
  • " Correcting persons about their grammar is a part of the code of conduct which should not be done. "

    Cor I've been in this business very professionally for forty years. It is specified that in this forum we speak English. Code is indefinite and ALWAYS singular. I shall continue correcting because it is not English and it is most disruptive reading it. Slowly we are witnessing social change and it is in a destructive direction as far as I am concerned.

    I shall continue correcting but I doubt it will do any good.

    Renee


    "MODERN PROGRAMMING is deficient in elementary ways BECAUSE of problems INTRODUCED by MODERN PROGRAMMING." Me

    Wednesday, February 20, 2013 6:41 AM
  • Hello Decrepit Blade,

    I feel that I should take a moment to explain how you properly post code into these forums.

    When either creating a new thread or responding to a thread, when you want to insert a block of code, please look for the following button, do not worry about coloring your code as the forum software will automatically format it for which language you select when posting code.

    Please go back and edit your posts that contain code, using the forum standard. Thanks


    “If you want something you've never had, you need to do something you've never done.”

    Don't forget to mark helpful posts and answers ! Answer an interesting question? Write a new article about it! My Articles
    *This post does not reflect the opinion of Microsoft, or its employees.

    Wednesday, February 20, 2013 7:44 AM
    Moderator