Results 1 to 22 of 22

Thread: Calcuylating cross wind

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2023
    Location
    Hoornaar, NL
    Posts
    24

    Question Calcuylating cross wind

    Hi Everyone,

    I'm new to VB. I used VB Excel a lot but ran into it's limitations.
    I made a tool in VB Excel which I want to convert to VB Studio. The VB language is quite the same but it also has a lot of differences.

    At this point I'd like to calculate the cross wind for aircraft.

    The math should be:
    wind_speed * sin(wind_direction - runway_heading)

    Currently I have the following code but it won't work...

    Code:
            Dim XwWindDir As Long
            Dim XwRwyHdg As Long
            Dim XwWindSpd As Long
    
    XwWindSpd * Math.Sin(XwWindDir - XwRwyHdg)

    I tried several methods which I found on the internet but still not abvle to figure out what's wrong.
    Is there anybody who can give me a push in the right direction?

    Thnx!
    Paul

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,442

    Re: Calcuylating cross wind

    You’d assign that line to a variable. If you meant to modify…

    Dim XwWindSpd As Long= something

    XwWindSpd *= Math.Sin(XwWindDir - XwRwyHdg)

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2023
    Location
    Hoornaar, NL
    Posts
    24

    Re: Calcuylating cross wind

    Quote Originally Posted by .paul. View Post
    You’d assign that line to a variable. If you meant to modify…

    Dim XwWindSpd As Long= something

    XwWindSpd *= Math.Sin(XwWindDir - XwRwyHdg)
    My bad…

    Code:
    XwResult.text = XwWindSpd * Math.Sin(XwWindDir - XwRwyHdg)
    XwResult is a label which should show the answer.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,878

    Re: Calcuylating cross wind

    Ok, so what is it doing wrong?

    There are a couple points to note. The first is that you have Option Strict OFF. That's a setting on Project | Properties. It is Off by default, but you should really turn it on. You can do that for all projects on the Options page, or for each project in Project | Properties. What Option Strict OFF does is allows implicit conversions, of which you have several. Implicit conversions are both slow and prone to certain types of errors, though not errors that should impact the code you are using.

    The second point is that you are using Long, which is most likely a holdover from your start in Excel. Long still exists in VB.NET, but you rarely have any use for it. An Integer in VBA was 16 bits, which was pretty small. A Long was 32 bits, which is usually adequate. In VB.NET, the Integer is 32 bits, so it's the same as the VBA Long, while the Long is 64-bits. Unless you are working with integer values greater than 2 billion, you can stick with Integer. There is no particular harm to using a Long, especially these days, though it may be very slightly slower, even these days. You would never see that difference, though, so you can do what you want. Just be aware that the Integer in .NET is equivalent to the VBA Long, while the .NET Long is much bigger.

    A third point is that you are putting a double value into a string. It's a double because Math.Sin will return a Double, which then gets multiplied by a Long, which results in a Double. If you just stick that into a string, the results will likely be unpleasant. Too many decimal places. What you probably want to do is a bit of formatting. I like:
    Code:
    XwResult.text = (XwWindSpd * Math.Sin(XwWindDir - XwRwyHdg)).ToString("N2")
    Which formats to two decimal places and includes a comma separator for values over 1000 (which you likely won't get for cross wind). Theoretically, though, you should go with N0, or no decimal places. There are many different formats, those are just the ones I like for floating point values.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2023
    Location
    Hoornaar, NL
    Posts
    24

    Re: Calcuylating cross wind

    Quote Originally Posted by Shaggy Hiker View Post
    A third point is that you are putting a double value into a string. It's a double because Math.Sin will return a Double, which then gets multiplied by a Long, which results in a Double. If you just stick that into a string, the results will likely be unpleasant. Too many decimal places. What you probably want to do is a bit of formatting. I like:
    Code:
    XwResult.text = (XwWindSpd * Math.Sin(XwWindDir - XwRwyHdg)).ToString("N2")

    I believe I've tried all options. I just can't get rid of all the errors like the one below.
    I've tried to solve this with CInt() and CStr() but that also doesn't help.

    Name:  Naamloos.jpg
Views: 168
Size:  13.1 KB

    Getting a bit desparate to be honest
    I thought it wouldn't be that hard but there must be some tiny thing I look over which ruins the whole code haha

  6. #6
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,081

    Re: Calcuylating cross wind

    Show the exact code you are using that is generating those errors, including all variable declarations for all variables involved.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Dec 2023
    Location
    Hoornaar, NL
    Posts
    24

    Re: Calcuylating cross wind

    Code:
    Private Sub XwCalc_Click(sender As Object, e As EventArgs) Handles XwCalc.Click
    
        Dim XwWindDir As Integer
        Dim XwVar As Integer
        Dim XwRwyHdg As Integer
        Dim XwWindSpd As Integer
        'Dim XwResult As String
    
    
        XwResult.text = XwWindSpd * Math.Sin(XwWindDir.Text - XwRwyHdg).ToString("N2")
    
    
    End Sub
    The code above results in the least errorts.
    Only XwWindDir.text gives me an error "text is not a member of Integer". Removing the .Text results in a red line below the whole instruction. I also tried to difine it differently (double, string, ...) nothing helps.

  8. #8
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,081

    Re: Calcuylating cross wind

    Quote Originally Posted by Paul_K View Post
    Code:
    Private Sub XwCalc_Click(sender As Object, e As EventArgs) Handles XwCalc.Click
    
        Dim XwWindDir As Integer
        Dim XwVar As Integer
        Dim XwRwyHdg As Integer
        Dim XwWindSpd As Integer
        'Dim XwResult As String
    
    
        XwResult.text = XwWindSpd * Math.Sin(XwWindDir.Text - XwRwyHdg).ToString("N2")
    
    
    End Sub
    The code above results in the least errorts.
    Only XwWindDir.text gives me an error "text is not a member of Integer". Removing the .Text results in a red line below the whole instruction. I also tried to difine it differently (double, string, ...) nothing helps.
    If that is the complete code for your XwCalc.Click event, then there are a myriad of issues.

    The first is, you never assign a value to any of those variables, so they will all have the default value of 0.
    Secondly, why are you trying to use XwWindDir.Text in the first place when XwWindDir is defined as an Integer?
    Thirdly, you've completely missed the outer parenthesis from Shaggy's example code, which are absolutely needed.

    It almost seems like maybe you've got TextBox/other controls on your form where there is a place for the values to be entered, and you just don't know how to pull the values into usable variables? No idea.

    Good luck getting this sorted.
    Last edited by OptionBase1; Dec 10th, 2023 at 04:53 PM.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,878

    Re: Calcuylating cross wind

    VB.NET is strongly typed. With Option Strict ON, a string is a string and a number is a number. You can't do math with strings anymore than you can meaningfully multiple "green" x "blue". With Option Strict OFF, then it might work, but only because VB will try to convert the string into a number that it can work with. If the string can be converted, then all is well and the calculation will work. It won't be as fast as it would be if you explicitly converted it, but the difference in speed is not noticeable so long as you don't do hundreds of such conversions.

    However, if the string cannot be converted, then the code will crash, but it will crash at runtime, where it is harder to diagnose and harder to fix. Also, there are worse problems than that. After all the '+' operator works just fine with strings. In VB, + concatenates strings. So, you can do something like 1 + "Blue", because 1 will be turned into "1" and the result will be "1Blue". But what happens if you add "1" + "1"? The answer will be "11", not 2. And what happens if you add "1" + 1? It could either implicitly convert the "1" to a 1 and add them, resulting in 2, or it could convert the 1 to "1" and concatenate them, in which case you get "11".

    Option Strict ON avoids all of that, but you do need to be clear on what you are doing. You do math on numbers. Only once the calculations are done do you convert the number to a string. The outer parenthesis in my example did that. The calculation was performed, and .ToString was called on the number. By leaving that out, you converted just the Sin to a string, but then you multiplied that string with a number. With Option Strict ON, that isn't allowed.
    My usual boring signature: Nothing

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Dec 2023
    Location
    Hoornaar, NL
    Posts
    24

    Re: Calcuylating cross wind

    @ OptionBase1
    @ Shaggy Hiker

    Thanks for your reply.
    I tried XwWindDir without .text and that resulted in a red line below the whole instruction. That’s why I tried it with .text although I knew it would fail.
    Maybe there is an issue in the Math.Sin() calculation with degrees or radians. Would that be possible?


    So…

    Code:
    XwResult.text = (XwWindSpd * Math.Sin(XwWindDir - XwRwyHdg)).ToString("N2")
    … should do the trick?
    I will give it a shot.

  11. #11
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,081

    Re: Calcuylating cross wind

    Quote Originally Posted by Paul_K View Post
    Maybe there is an issue in the Math.Sin() calculation with degrees or radians. Would that be possible?
    No. If you pass a number to Math.Sin and it is in degrees but the function is expecting radians, the function will simply return a value differing from what you are expecting (unless it is a case where the values are the same, such as Sin(0)).

    The compiler can't possibly know that you aren't passing the correct units, so there is no way for that to be "caught" at compile time.

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,878

    Re: Calcuylating cross wind

    Quote Originally Posted by Paul_K View Post

    I tried XwWindDir without .text and that resulted in a red line below the whole instruction. That’s why I tried it with .text although I knew it would fail.
    That's a bit disturbing. What do you think .text is? What do you think XwWindDir is?

    You shouldn't be trying these things at random. You should have an idea of what they are and how they work. The ".Text" means that you are talking about a property of the object. XwResult has a .Text property because it's an object and it has properties. It's probably a label, which derives from control, which also derives from other things. Each of the base classes adds a few properties. The label has all the properties of it's bases and itself. You can access any of the properties that are declared Public for the object, though there may be other properties that the object doesn't let you have access to.

    XwWinDir is an Integer. It can be turned into an object, but unless you turn it into an object, it is just an integer. An Integer isn't a class, it's just the value of the number. There isn't any means to add properties to a value.

    You seem to be approaching programming as if it is a black box with marvelous abilities. It's a whole lot simpler than that. I think you would be well advised to gain some foundational understanding of coding. Higher level languages can certainly make it look a bit mysterious, but it's not, and no higher level language is all that high. At the bottom level, it's just an electrical potential creating a positive voltage or not, and everything else is organization built on top of that...and not built all that far up. If you think you're at a high level, you might believe there are many layers of complexity that you don't want or need to understand, but you aren't at a very high level and there aren't many layers. An Integer is just four bytes, which means 32 of those electrical potentials organized to represent a number. You aren't far above the metal.
    My usual boring signature: Nothing

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Dec 2023
    Location
    Hoornaar, NL
    Posts
    24

    Re: Calcuylating cross wind

    I'm not a born programmer. I teached it myself with Excel VBA first. Luckily the difference in language style is not that big in VB.Net. I try to understand the basics and build my code around that.
    As English is not my native language the expression "You aren't far above the metal" results in an error in my head haha (LanguageNotFound.expression I guess) , no offense

    I really tried a lot. At least I get some numbers now (other than 0) with the code below but it still is not the correct answer.

    Code:
            Dim XwWindDir As Integer
            Dim XwRwyHdg As Integer
            Dim XwWindSpd As Integer
    
    
            XwResult.Text = (30 * Math.Sin(20)).ToString("N2")
    This results in 27.39 which is wrong en should be 10.26.
    The result of the calculation goes in the textbox (XwResult). Therefore I had to use the .text. Is that wrong? How should I do that correctly in VB? And how to solve this calculation error?
    I know I do something wrong (otherwise I would have the right answer) but is there anyone here to tell me WHAT is wrong and how it should be?
    Long, Double, Integer... I probably mix things up. the XwResult.Text (which was .Value in Excel VBA) is not suppose to only return TEXT but just the value of the calculation result.

    Anyway, thanks for your time so far.

    Any help is much appreciated.

  14. #14
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,081

    Re: Calcuylating cross wind

    The issue is exactly as I stated above in post 11. You are passing 20, which presumably means (to you) 20 degrees. The Math.Sin function is expecting a value in radians, so it is treating the passed value of 20 as "20 radians". If your working values are in degrees, then you need to convert them to Radians before passing them to Math.Sin

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,442

    Re: Calcuylating cross wind

    radians = degrees * pi/180

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Dec 2023
    Location
    Hoornaar, NL
    Posts
    24

    Re: Calcuylating cross wind

    Quote Originally Posted by OptionBase1 View Post
    No. If you pass a number to Math.Sin and it is in degrees but the function is expecting radians, the function will simply return a value differing from what you are expecting (unless it is a case where the values are the same, such as Sin(0)).

    The compiler can't possibly know that you aren't passing the correct units, so there is no way for that to be "caught" at compile time.

    Hi OptionBase1,
    I think we misunderstood... I asked if there could be a degree vs radians issue. You said no and I misunderstood the next thing you said. So yes there is a rad/deg issue... but that can be solved.

    I have tried to write a function and that works well.
    Code:
        Private Function DegToRad(angle As Double) As Double
            Return angle * (Math.PI / 180.0)
        End Function
    Now, the code WITH inserted values for the wind speed etc does work. Good news!

    Code:
            XwResult.Text = (30 * Math.Sin(DegToRad((250 + 0) - 230))).ToString("N2")
    But whenever I change one of the values for the name of the user textbox, the result is "0" again no matter what I put in the boxes.

    Code:
            XwResult.Text = (XwWindSpd * Math.Sin(DegToRad((XwWindDir + XwVar) - XwRwyHdg))).ToString("N2")
    The form for the user to put in the values is the one below.
    And it also doesn't matter if I define the textboxes as Integer of Double. Any clues?

    Attachment 189573

  17. #17
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,408

    Re: Calcuylating cross wind

    Quote Originally Posted by Paul_K View Post
    Hi OptionBase1,
    But whenever I change one of the values for the name of the user textbox, the result is "0" again no matter what I put in the boxes.
    Not entirely sure what you mean changing the value for the name of a textbox, could you clarify what you mean?

    Quote Originally Posted by Paul_K View Post
    The form for the user to put in the values is the one below.
    And it also doesn't matter if I define the textboxes as Integer of Double. Any clues?
    There is an issue with attachments on the forum that can make them unreadable - looks like this has happened here...

    Not sure what you mean by defining a textbox as an integer / double - a textbox is a textbox, an integer is an integer etc.

  18. #18
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,081

    Re: Calcuylating cross wind

    Quote Originally Posted by Paul_K View Post
    Hi OptionBase1,

    But whenever I change one of the values for the name of the user textbox, the result is "0" again no matter what I put in the boxes.

    Code:
            XwResult.Text = (XwWindSpd * Math.Sin(DegToRad((XwWindDir + XwVar) - XwRwyHdg))).ToString("N2")
    The form for the user to put in the values is the one below.
    And it also doesn't matter if I define the textboxes as Integer of Double. Any clues?
    Which of those are TextBoxes? If, for example, XwWindSpd is a Textbox, then you absolutely shouldn't be doing something like this:

    Code:
    Dim XwWindSpd As Integer
    If all of the "Xw" things in your code are TextBoxes or other user input controls, you should not be declaring a variable with the "same" name inside of your code.

    This *may* work, but it is ... less than ideal. I'll leave it to you to do proper input sanity checking, etc.

    Code:
            XwResult.Text = (CDbl(XwWindSpd.Text) * Math.Sin(DegToRad((CDbl(XwWindDir.Text) + CDbl(XwVar.Text)) - CDbl(XwRwyHdg.Text)))).ToString("N2")

  19. #19
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,442

    Re: Calcuylating cross wind

    Instead of using TextBoxes, use NumericUpDown Controls. The Value property of the NUD returns a Decimal, which is higher accuracy than a Double...

    With TextBoxes, it's difficult to ensure user input is a valid number.

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Dec 2023
    Location
    Hoornaar, NL
    Posts
    24

    Re: Calcuylating cross wind

    Quote Originally Posted by OptionBase1 View Post
    Which of those are TextBoxes? If, for example, XwWindSpd is a Textbox, then you absolutely shouldn't be doing something like this:

    Code:
    Dim XwWindSpd As Integer
    If all of the "Xw" things in your code are TextBoxes or other user input controls, you should not be declaring a variable with the "same" name inside of your code.

    This *may* work, but it is ... less than ideal. I'll leave it to you to do proper input sanity checking, etc.

    Code:
            XwResult.Text = (CDbl(XwWindSpd.Text) * Math.Sin(DegToRad((CDbl(XwWindDir.Text) + CDbl(XwVar.Text)) - CDbl(XwRwyHdg.Text)))).ToString("N2")

    Hi OptionBase1,

    That was the answer that triggered me

    Code:
          Dim XwXwind As String
            Dim HwXwind As String
    
            XwResult.Text = (CInt(XwWindSpd.Text) * Math.Sin(DegToRad((CInt(XwWindDir.Text) + CInt(XwVar.Text)) - CInt(XwRwyHdg.Text)))).ToString("N2")
            HwResult.Text = (CInt(XwWindSpd.Text) * Math.Cos(DegToRad((CInt(XwWindDir.Text) + CInt(XwVar.Text)) - CInt(XwRwyHdg.Text)))).ToString("N2")
    This works as it should! I don't know what I was thinking with defining textboxes. Thanks for pointing that out!
    Also thanks to the rest for trying to find the answers.

    Paul

  21. #21
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,442

    Re: Calcuylating cross wind

    Quote Originally Posted by Paul_K View Post
    Hi OptionBase1,

    That was the answer that triggered me

    Code:
          Dim XwXwind As String
            Dim HwXwind As String
    
            XwResult.Text = (CInt(XwWindSpd.Text) * Math.Sin(DegToRad((CInt(XwWindDir.Text) + CInt(XwVar.Text)) - CInt(XwRwyHdg.Text)))).ToString("N2")
            HwResult.Text = (CInt(XwWindSpd.Text) * Math.Cos(DegToRad((CInt(XwWindDir.Text) + CInt(XwVar.Text)) - CInt(XwRwyHdg.Text)))).ToString("N2")
    This works as it should! I don't know what I was thinking with defining textboxes. Thanks for pointing that out!
    Also thanks to the rest for trying to find the answers.

    Paul
    It works asit should until someone types ‘hello’ into XwWindDir…

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Dec 2023
    Location
    Hoornaar, NL
    Posts
    24

    Re: Calcuylating cross wind

    Nope… I took .paul.’s advice and changed the boxes to NumericUpDown fields. Problem solved. And if that didn’t exist I already had a script ready to prevent this. But thanks for the reminder

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width