none
BC42105 error RRS feed

  • Question

  • My code:

    Public Class frmSimpleCalculator

        Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        End Sub
        Dim operand1 As Decimal
        Dim operator1 As String
        Dim operand2 As Decimal
        Dim result? As Decimal
        Private Function funcCalculationResult(result? As Decimal)

            If operator1 = "+" Then
                result = operand1 + operand2
            ElseIf operator1 = "-" Then
                result = operand1 - operand2
            ElseIf operator1 = "*" Then
                result = operand1 * operand2
            ElseIf operator1 = "/" Then
                result = operand1 / operand2
            Else
            End If

            Me.funcCalculationResult(result)

            txtResult.Text = FormatNumber(result, 4)
            txtOperand1.Select()

        End Function

        Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
            Me.Close()
        End Sub
    End Class

     It won't run and I have to EXIT to stop it. This beside the BC42105 error at End Function.  Please Help. The result has to be a number with up to 4 decimal places.

    Saturday, October 6, 2018 8:25 PM

Answers

  • Hi

    The reason for the error seems to be that not all the 'paths' in your function return a result.

    Here is some code that may help.

    NOTES: ensure you put the 2 Option lines at the top of your code. Those may have shown you the error(s).

    This example uses 2 Operand textboxes plus an Operator textbox as well as a result textbox = plus, a GO button.

    Image

    Form1 Code

    ' Form1 with layout as per
    ' Image (control Names can
    ' be determined by inspection)
    
    ' *******  ENSURE YOU HAVE THESE 2 LINES
    Option Strict On
    Option Explicit On
    
    
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        AcceptButton = Button1
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim result As Decimal = funcCalculationResult(txtOperator1.Text, GetDecimal(txtOperand1.Text), GetDecimal(txtOperand2.Text))
    
        txtResult.Text = FormatNumber(result, 4)
        txtOperator1.Select()
      End Sub
    
      ' Function to Get a Decimal from
      ' a String (needed to do Math)
      Function GetDecimal(s As String) As Decimal
        Dim v As Decimal = 0D
        If Decimal.TryParse(s, v) Then Return v
        Return 0D
      End Function
    
    
      ' Your function, altered a little to 
      ' make it work.
      Private Function funcCalculationResult(operator1 As String, operand1 As Decimal, operand2 As Decimal) As Decimal
        Dim result As Decimal
        Select Case operator1
          Case "+"
            result = operand1 + operand2
          Case "-"
            result = operand1 - operand2
          Case "*"
            result = operand1 * operand2
          Case "/"
            result = operand1 / operand2
        End Select
        Return result
      End Function
    End Class
    BTW: if this is school work, then LEARN what the code does or I will make sure your school learns that you are a cheat!


    Regards Les, Livingston, Scotland


    • Edited by leshay Saturday, October 6, 2018 9:41 PM
    • Marked as answer by Prodson101 Monday, October 8, 2018 4:32 PM
    Saturday, October 6, 2018 9:39 PM
  • Your help is appreciated. I am a 53 year old disabled veteran from the USAF and am trying to teach myself code. The code you provided gave me more errors than I originally had. Maybe I screwed it up somehow. I am getting real frustrated and am about to quit. The IDE said that I had things not declared and statements not concluded. It will not build.

    Hi

    Well, I do not need the SchoolWork warning anymore  :)

    I have you beat, 72 years here, retired and trying to learn coding also.

    Do NOT give up. It is always the same at the start where you have a steep learning curve, but it does get more easily understood pretty quickly after you get started.

    You are in the right place, this forum will give you all the support you need, and rremember, no question is silly.

    One other thing. The answer(s) you will get to questions will depend strongly on the question content. You need to supply as much detail as possible, with code (in code blocks). In particular, 'it doesn't work' never works - any error/exception text should also be included where relevant.

    *

    Now back to your post. The likely reason for a sudden increase of errors/exceptions may be due to the top two Option lines. These cause many things that *could* cause issues to be reported whereas if those Options are not included (the default is for them tobe 'Off'), a lot of the errors/warnings are suppressed.

    Believe it or not, those error reports are a good thing - if you always use those Options, it forces you to be more specific in you code and ultimately leads to better code.

    *

    The code as listed does compile and work correctly here (I will post exactly what I have tested with below). I had to rewrite it to encompass your selected project name etc. I also added labels to show the control names being used in the code. This code DOES WORK OK. It is almost the same as previously posted and the only changes made no difference to whether or not it would compile and/or work OK. (both versions would compile and work)

    *

    Keep at it, and ask here any time if you have further issues.

    Image

    Form Code

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        AcceptButton = btnCalculate
      End Sub
      Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    
        Dim result As Decimal = funcCalculationResult(txtOperator1.Text, GetDecimal(txtOperand1.Text), GetDecimal(txtOperand2.Text))
    
        txtResult.Text = FormatNumber(result, 4)
        txtOperand1.Select()
      End Sub
      Function GetDecimal(s As String) As Decimal
        Dim v As Decimal = 0D
        If Decimal.TryParse(s, v) Then Return v
        Return 0D
      End Function
      Private Function funcCalculationResult(operator1 As String, operand1 As Decimal, operand2 As Decimal) As Decimal
        Dim result As Decimal
        Select Case operator1
          Case "+"
            result = operand1 + operand2
          Case "-"
            result = operand1 - operand2
          Case "*"
            result = operand1 * operand2
          Case "/"
            result = operand1 / operand2
        End Select
        Return result
      End Function
      Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Close()
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, October 7, 2018 11:34 PM
    • Marked as answer by Prodson101 Monday, October 8, 2018 4:31 PM
    Sunday, October 7, 2018 11:32 PM
  • Hi

    You have a line 'Private txtOperator1 As String' at the beginning of your code -what is that for? It is likely causing the conflict you see. Try removing that line and see if a build is successful.

    *

    If we are to continue with this thread, we need to be 'on the same page' so to speak. If we are to have the same code under discussion, then you need to copy the code posted and replace ALL of the code in your Form1 code with the copied version (overwrite everything unless stated otherwise)

    *

    BTW: you can post code in a code block using the BLUE marked button on the ToolBar at the top of the reply editor pane. (the Red marked for posting images but you may not be able as yet to do that)



    Regards Les, Livingston, Scotland





    • Marked as answer by Prodson101 Monday, October 8, 2018 4:31 PM
    • Edited by leshay Thursday, October 25, 2018 5:54 PM
    Monday, October 8, 2018 1:39 PM
  • Your help is appreciated. I am a 53 year old disabled veteran from the USAF and am trying to teach myself code. The code you provided gave me more errors than I originally had. Maybe I screwed it up somehow. I am getting real frustrated and am about to quit. The IDE said that I had things not declared and statements not concluded. It will not build.

    Option Strict On
    Option Explicit On

    Public Class frmSimpleCalculator
        Private Sub frmSimpleCalculator_Load(sender As Object, e As EventArgs) Handles MyBase.Load AcceptButton  = btnCalculate
            End Sub


        Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click

            Dim result As Decimal = funcCalculationResult(txtOperator1.Text, GetDecimal(txtOperand1.Text), GetDecimal(txtOperand2.Text))

            txtResult.Text = FormatNumber(result, 4)
            txtOperand1.Select()
        End Sub

        Function GetDecimal(s As String) As Decimal
            Dim v As Decimal = 0D
            If Decimal.TryParse(s, v) Then Return v
            Return 0D
        End Function

        Private Function funcCalculationResult(operator1 As String, operand1 As Decimal, operand2 As Decimal) As Decimal
            Dim result As Decimal
            Select Case operator1
                Case "+"
                    result = operand1 + operand2
                Case "-"
                    result = operand1 - operand2
                Case "*"
                    result = operand1 * operand2
                Case "/"
                    result = operand1 / operand2
            End Select
            Return result
        End Function

        Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
            Me.Close()
        End Sub
    End Class

    I would suggest looking at a good working sample as found in the link below.

    https://code.msdn.microsoft.com/windowsdesktop/VBNet-Calculator-08065b6b

    How to learn from this 

    • Compile the project, run it, try using it.
    • Examine the source code.
    • Set breakpoints and step through the code.
    • If something does not make sense to you, come back here and ask.

    Code is broken down in the form an a class.

    Interface


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Prodson101 Monday, October 8, 2018 4:33 PM
    Monday, October 8, 2018 1:49 PM
    Moderator

All replies

  • Hi

    The reason for the error seems to be that not all the 'paths' in your function return a result.

    Here is some code that may help.

    NOTES: ensure you put the 2 Option lines at the top of your code. Those may have shown you the error(s).

    This example uses 2 Operand textboxes plus an Operator textbox as well as a result textbox = plus, a GO button.

    Image

    Form1 Code

    ' Form1 with layout as per
    ' Image (control Names can
    ' be determined by inspection)
    
    ' *******  ENSURE YOU HAVE THESE 2 LINES
    Option Strict On
    Option Explicit On
    
    
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        AcceptButton = Button1
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim result As Decimal = funcCalculationResult(txtOperator1.Text, GetDecimal(txtOperand1.Text), GetDecimal(txtOperand2.Text))
    
        txtResult.Text = FormatNumber(result, 4)
        txtOperator1.Select()
      End Sub
    
      ' Function to Get a Decimal from
      ' a String (needed to do Math)
      Function GetDecimal(s As String) As Decimal
        Dim v As Decimal = 0D
        If Decimal.TryParse(s, v) Then Return v
        Return 0D
      End Function
    
    
      ' Your function, altered a little to 
      ' make it work.
      Private Function funcCalculationResult(operator1 As String, operand1 As Decimal, operand2 As Decimal) As Decimal
        Dim result As Decimal
        Select Case operator1
          Case "+"
            result = operand1 + operand2
          Case "-"
            result = operand1 - operand2
          Case "*"
            result = operand1 * operand2
          Case "/"
            result = operand1 / operand2
        End Select
        Return result
      End Function
    End Class
    BTW: if this is school work, then LEARN what the code does or I will make sure your school learns that you are a cheat!


    Regards Les, Livingston, Scotland


    • Edited by leshay Saturday, October 6, 2018 9:41 PM
    • Marked as answer by Prodson101 Monday, October 8, 2018 4:32 PM
    Saturday, October 6, 2018 9:39 PM
  • Your help is appreciated. I am a 53 year old disabled veteran from the USAF and am trying to teach myself code. The code you provided gave me more errors than I originally had. Maybe I screwed it up somehow. I am getting real frustrated and am about to quit. The IDE said that I had things not declared and statements not concluded. It will not build.

    Option Strict On
    Option Explicit On

    Public Class frmSimpleCalculator
        Private Sub frmSimpleCalculator_Load(sender As Object, e As EventArgs) Handles MyBase.Load AcceptButton  = btnCalculate
            End Sub


        Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click

            Dim result As Decimal = funcCalculationResult(txtOperator1.Text, GetDecimal(txtOperand1.Text), GetDecimal(txtOperand2.Text))

            txtResult.Text = FormatNumber(result, 4)
            txtOperand1.Select()
        End Sub

        Function GetDecimal(s As String) As Decimal
            Dim v As Decimal = 0D
            If Decimal.TryParse(s, v) Then Return v
            Return 0D
        End Function

        Private Function funcCalculationResult(operator1 As String, operand1 As Decimal, operand2 As Decimal) As Decimal
            Dim result As Decimal
            Select Case operator1
                Case "+"
                    result = operand1 + operand2
                Case "-"
                    result = operand1 - operand2
                Case "*"
                    result = operand1 * operand2
                Case "/"
                    result = operand1 / operand2
            End Select
            Return result
        End Function

        Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
            Me.Close()
        End Sub
    End Class

    Sunday, October 7, 2018 10:19 PM
  • Your help is appreciated. I am a 53 year old disabled veteran from the USAF and am trying to teach myself code. The code you provided gave me more errors than I originally had. Maybe I screwed it up somehow. I am getting real frustrated and am about to quit. The IDE said that I had things not declared and statements not concluded. It will not build.

    Hi

    Well, I do not need the SchoolWork warning anymore  :)

    I have you beat, 72 years here, retired and trying to learn coding also.

    Do NOT give up. It is always the same at the start where you have a steep learning curve, but it does get more easily understood pretty quickly after you get started.

    You are in the right place, this forum will give you all the support you need, and rremember, no question is silly.

    One other thing. The answer(s) you will get to questions will depend strongly on the question content. You need to supply as much detail as possible, with code (in code blocks). In particular, 'it doesn't work' never works - any error/exception text should also be included where relevant.

    *

    Now back to your post. The likely reason for a sudden increase of errors/exceptions may be due to the top two Option lines. These cause many things that *could* cause issues to be reported whereas if those Options are not included (the default is for them tobe 'Off'), a lot of the errors/warnings are suppressed.

    Believe it or not, those error reports are a good thing - if you always use those Options, it forces you to be more specific in you code and ultimately leads to better code.

    *

    The code as listed does compile and work correctly here (I will post exactly what I have tested with below). I had to rewrite it to encompass your selected project name etc. I also added labels to show the control names being used in the code. This code DOES WORK OK. It is almost the same as previously posted and the only changes made no difference to whether or not it would compile and/or work OK. (both versions would compile and work)

    *

    Keep at it, and ask here any time if you have further issues.

    Image

    Form Code

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        AcceptButton = btnCalculate
      End Sub
      Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    
        Dim result As Decimal = funcCalculationResult(txtOperator1.Text, GetDecimal(txtOperand1.Text), GetDecimal(txtOperand2.Text))
    
        txtResult.Text = FormatNumber(result, 4)
        txtOperand1.Select()
      End Sub
      Function GetDecimal(s As String) As Decimal
        Dim v As Decimal = 0D
        If Decimal.TryParse(s, v) Then Return v
        Return 0D
      End Function
      Private Function funcCalculationResult(operator1 As String, operand1 As Decimal, operand2 As Decimal) As Decimal
        Dim result As Decimal
        Select Case operator1
          Case "+"
            result = operand1 + operand2
          Case "-"
            result = operand1 - operand2
          Case "*"
            result = operand1 * operand2
          Case "/"
            result = operand1 / operand2
        End Select
        Return result
      End Function
      Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Close()
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, October 7, 2018 11:34 PM
    • Marked as answer by Prodson101 Monday, October 8, 2018 4:31 PM
    Sunday, October 7, 2018 11:32 PM
  • I am using the Visual Studio 2015, maybe that is why I am having trouble.

    On
    Option Explicit On

    Public Class frmSimpleCalculator
        Private txtOperator1 As String

        Private Sub frmSimpleCalculator_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            AcceptButton = btnCalculate
        End Sub


        Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click

            Dim result As Decimal = funcCalculationResult(txtOperator1.Text, GetDecimal(txtOperand1.Text), GetDecimal(txtOperand2.Text))

            txtResult.Text = FormatNumber(result, 4)
            txtOperand1.Select()
        End Sub

        Function GetDecimal(s As String) As Decimal
            Dim v As Decimal = 0D
            If Decimal.TryParse(s, v) Then Return v
            Return 0D
        End Function

        Private Function funcCalculationResult(operator1 As String, operand1 As Decimal, operand2 As Decimal) As Decimal
            Dim result As Decimal
            Select Case operator1
                Case "+"
                    result = operand1 + operand2
                Case "-"
                    result = operand1 - operand2
                Case "*"
                    result = operand1 * operand2
                Case "/"
                    result = operand1 / operand2
            End Select
            Return result
        End Function

        Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
            Me.Close()
        End Sub
    End Class

    In the first Dim line, as I have highlighted, I get a red underline that says Text is not a member of String. So It does not build. When I did get it to actually come up with the form, it comes up in the textbox for operand2 not operand1. I checked all the form properties for all the textboxes and am stuck there too. Thanks for your help.

    Monday, October 8, 2018 1:12 PM
  • Hi

    You have a line 'Private txtOperator1 As String' at the beginning of your code -what is that for? It is likely causing the conflict you see. Try removing that line and see if a build is successful.

    *

    If we are to continue with this thread, we need to be 'on the same page' so to speak. If we are to have the same code under discussion, then you need to copy the code posted and replace ALL of the code in your Form1 code with the copied version (overwrite everything unless stated otherwise)

    *

    BTW: you can post code in a code block using the BLUE marked button on the ToolBar at the top of the reply editor pane. (the Red marked for posting images but you may not be able as yet to do that)



    Regards Les, Livingston, Scotland





    • Marked as answer by Prodson101 Monday, October 8, 2018 4:31 PM
    • Edited by leshay Thursday, October 25, 2018 5:54 PM
    Monday, October 8, 2018 1:39 PM
  • Your help is appreciated. I am a 53 year old disabled veteran from the USAF and am trying to teach myself code. The code you provided gave me more errors than I originally had. Maybe I screwed it up somehow. I am getting real frustrated and am about to quit. The IDE said that I had things not declared and statements not concluded. It will not build.

    Option Strict On
    Option Explicit On

    Public Class frmSimpleCalculator
        Private Sub frmSimpleCalculator_Load(sender As Object, e As EventArgs) Handles MyBase.Load AcceptButton  = btnCalculate
            End Sub


        Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click

            Dim result As Decimal = funcCalculationResult(txtOperator1.Text, GetDecimal(txtOperand1.Text), GetDecimal(txtOperand2.Text))

            txtResult.Text = FormatNumber(result, 4)
            txtOperand1.Select()
        End Sub

        Function GetDecimal(s As String) As Decimal
            Dim v As Decimal = 0D
            If Decimal.TryParse(s, v) Then Return v
            Return 0D
        End Function

        Private Function funcCalculationResult(operator1 As String, operand1 As Decimal, operand2 As Decimal) As Decimal
            Dim result As Decimal
            Select Case operator1
                Case "+"
                    result = operand1 + operand2
                Case "-"
                    result = operand1 - operand2
                Case "*"
                    result = operand1 * operand2
                Case "/"
                    result = operand1 / operand2
            End Select
            Return result
        End Function

        Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
            Me.Close()
        End Sub
    End Class

    I would suggest looking at a good working sample as found in the link below.

    https://code.msdn.microsoft.com/windowsdesktop/VBNet-Calculator-08065b6b

    How to learn from this 

    • Compile the project, run it, try using it.
    • Examine the source code.
    • Set breakpoints and step through the code.
    • If something does not make sense to you, come back here and ask.

    Code is broken down in the form an a class.

    Interface


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Prodson101 Monday, October 8, 2018 4:33 PM
    Monday, October 8, 2018 1:49 PM
    Moderator
  • Thanks.....I had to change the Operator1.Text to Operator.Text. It worked well after that. When the form comes up after the build, the focus is on the third textbox. The textbox for the second operand. I don't understand why. The properties of the textbox3s seem correct. All the TAB INDEX are set to 1.

    Monday, October 8, 2018 4:12 PM
  • Hi

    The TAB INDEX is the order that the TAB KEY will follow through the various controls on the form. For example (using only 3 textboxes):i f you set the leftmost textbox TAB INDEX = 1 and rightmost textbox TAB INDEX = 2 and center textbox TAB INDEX = 3, then when running, using the TAB KEY will cycle through those controls - leftmost, rightmost, center, leftmost, rightmost, center. Those settings should have no impact on your project unless you use the TAB KEY during use.

    *

    To set the focus to a particular control on the Form when first run, you can doso like this (in the Load event Handler): txtOperator1.Select (the center textbox of top 3)

    *

    You say 'I had to change the Operator1.Text to Operator.Text' which is tantamount to removing the line altogether since the new variable txtOperator is not used anywhere else in your code. It was/is a spurious random uneccessary line and should be expunged from this universe :)


    Regards Les, Livingston, Scotland

    Monday, October 8, 2018 4:39 PM
  • I have a new problem. This code is killing me. The book I am using doesn't discuss the things I am specifically trying to do. I have gotten my program to work but I am trying to modify it. It is an easy form that gets arrival date and departure date. It calculates number of nights, total price (based on $160 per night so far), and  average price per night. I am trying to add a load event handler that gets the current date and three days after the current date and uses these as default values for arrival and departure. Then I want to charge $180 for Friday and Saturday nights and keep $160 for other nights. I think a While loop is the way to go. I started the process with the Dim statements and am lost with the loop. I can't figure the way to identify the days for all the nights and then charge the different amounts.
    Public Class Form1
    
        Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    
            Dim arrivalDate As Date = CDate(txtArrivalDate.Text)
            Dim departureDate As Date = CDate(txtDepartureDate.Text)
            Dim timeOfTrip As TimeSpan = departureDate.Subtract(arrivalDate)
            Dim numberOfNights As String = (txtNights.Text)
            Dim totalPrice As String = (txtTotalPrice.Text)
            Dim avgPricePerNight As String = (txtAvgPrice.Text)
    
            numberOfNights = CType((timeOfTrip.Days), String)
            totalPrice = CType(CDec(CDbl(numberOfNights) * 160.0), String)
            avgPricePerNight = CType(CDec(totalPrice) / CDec(numberOfNights), String)
    
            txtNights.Text = numberOfNights
            txtTotalPrice.Text = totalPrice
            txtAvgPrice.Text = avgPricePerNight
    
        End Sub
    
        Public Function IsValidData() As Boolean
    
        End Function
    
        Public Function IsPresent(textbox As TextBox, name As String) _
                As Boolean
            If textbox.Text = "" Then
                MessageBox.Show(name & " is a required field.", "Entry Error")
                textbox.Select()
                Return False
            Else
                Return True
            End If
        End Function
    
        Public Function IsDateTime(textbox As TextBox, name As String) _
                As Boolean
    
        End Function
    
        Public Function IsWithinRange(textbox As TextBox, name As String,
                ByVal min As Date, ByVal max As Date) As Boolean
    
        End Function
    
        Private Sub btnExit_Click(sender As Object,
            e As EventArgs) Handles btnExit.Click
            Me.Close()
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim currentDate As Date = Date.Now
            Dim arrivalDate As Date
            Dim departureDate As Date = CDate(txtDepartureDate.Text)
            Dim timeOfTrip As TimeSpan = departureDate.Subtract(arrivalDate)
            Dim numberOfNights As String = (txtNights.Text)
            Dim totalPrice As String = (txtTotalPrice.Text)
            Dim avgPricePerNight As String = (txtAvgPrice.Text)
    
            Do
                numberOfNights = Weekday(DateInterval.Day)
            Loop
    
        End Sub
    End Class
    Any help would be appreciated
    Thursday, October 25, 2018 5:45 PM
  • Hi

    This thread has already been answered, and is about a different matter. You should start a new thread for this question - you can have as many threads as you need.

    To get you started, here is some code that may offer an insight. When dealing with Dates/Times, you really do need to use Date types (or more specifically, DateTime types). Using these make all sorts of date/time calculations very simple. If you choose to just use TextBoxes for (say) day/month/year/hour/min/sec   etc then it would rapidly become extremely tricky to keep track of - and. you would probably very quickly, be asking for all sorts of help.

    However, have a look at this and see how simplr it is:

    (Note: Lable3 is the one that gets the result)

    Image

    Code

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DateTimePicker1.Value = Now.Date
        DateTimePicker2.Value = Now.AddDays(3)
      End Sub
      Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker2.ValueChanged, DateTimePicker1.ValueChanged
        Dim v As Integer = DateTimePicker2.Value.Subtract(DateTimePicker1.Value).Days
        Label3.Text = v.ToString
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, October 25, 2018 6:35 PM
    Thursday, October 25, 2018 6:34 PM
  • That code doesn't make much sense to me. What is Picker1 and Picker2? Are they arrival date and departure date? Label3 is number of nights? It also doesn't help me with determining days of the week and adding cost per night, 180 or 160 depending upon what day of week. I appreciate the help but I am just as lost as I was,. The code has numerous mistakes when I run it. It says that the DateTimePicker1 is not declared, DateTimePicker2 is not declared. Please help.
    Thursday, October 25, 2018 7:54 PM
  • Hi

    I didn't explain it very well. DateTimePicker1 and DateTimePicker2 are DateTimePicker controls found in the Toolbox and dragged onto the Form (as any of the conrtols are - Labels, TextBoxes etc.)

    Without those, the build fails with exceptions that you are getting.

    I didn't continue with the rest of your question as it needs to be in a new thread. Please start one.


    Regards Les, Livingston, Scotland

    Thursday, October 25, 2018 8:16 PM