Results 1 to 4 of 4

Thread: [RESOLVED] Integer cannot be converted to UShort

  1. #1

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Resolved [RESOLVED] Integer cannot be converted to UShort

    I have this piece of code in C#:
    vb.net Code:
    1. return ((UInt16)(((0xFF00 & input) >> 8) | ((0x00FF & input) << 8)));

    Here is the VB code:
    vb.net Code:
    1. Return DirectCast(((&HFF00 And input) >> 8) Or ((&H00FF And input) << 8), UInt16)

    But the VB code is throwing the following error:
    Value of type 'Integer' cannot be converted to 'UShort'.
    Any ideas why I am getting it ? I was stumped because the C# code is not throwing that error!

    Thanks in advance

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  2. #2
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Integer cannot be converted to UShort

    Try this:

    Code:
    Return Convert.ToUInt16(((&HFF00 And input) >> 8) Or ((&HFF And input) << 8))
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Integer cannot be converted to UShort

    Use CType instead of DirectCast and all your problems will disappear!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Integer cannot be converted to UShort

    Thanks

    Both solutions seems to be working
    Last edited by akhileshbc; Jan 15th, 2013 at 12:11 PM. Reason: dunfiddin's solution was also tested

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

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