Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Difference in using VB vs C#


Best Answer Gutworks, 29 December 2012 - 04:49 AM

Chuck I just tried your sample and I got the same error. I recall a couple of firmware editions back Chris added support for doubles as arguments in addition to the bytes. When I use doubles as a value the code works fine. So that got me thinking...

 

I tried a couple of different ways and there seems to be a major difference in how Visual Basic and C# deal with Doubles and Bytes, or at the very least in the way it interprets the arguments for the SetColor() constructors.

 

SetColour() has two constructors:

 

SetColor(Double, Double, Double) SetColor(Byte, Byte, Byte)

Visual Basic seems to default to the the Double constructor unless you explicitly cast the values as bytes. Conversely, C# will default to a Byte unless you explicitly tell it to use Doubles. This can be done by easily where 0.0 and 0f both represent a Double and work in this case.

 

So for example:

led.SetColor(1,0,1);

In VB, this will default to the Double constructor, however in C# it will assume that you are passing three bytes and will use the Byte constructor. 

 

In VB if you want to use bytes you can use (though I'm sure there are more elegant ways):

 

Led.SetColor(CType(100, Byte), CType(0, Byte), CType(100, Byte))

 

It is a rather peculiar behaviour between the two languages, and I'm not sure why they would act differently. How even more perplexing is that in VB Intellisense will actually show the Byte constructor as you type out SetColor(100, 0, 100), but after finishing the line of code you can hover over the method and see that it has defaulted back to the Double one. 

 

In any case, I hope this helps somewhat. And perhaps someone can provide us some theory as why this is happening and if it's a bug warranting a fix within the .Net Mirco Framework. 

 

Cheers,

Steve

 

* EDIT: Removed the Canadian flavour from my colours. :)

Go to the full post


  • Please log in to reply
11 replies to this topic

#1 carb

carb

    Advanced Member

  • Members
  • PipPipPip
  • 352 posts
  • LocationCrystal River, Florida

Posted 25 November 2012 - 07:31 PM

I was writting a simple program in Visual Basic using a Netduino Go, RgbLed module and a Button module. I am using 4.2.1 firmware and SDK.
Imports Microsoft.SPOT
Imports Microsoft.SPOT.Hardware
Imports SecretLabs.NETMF.Hardware
Imports SecretLabs.NETMF.Hardware.Netduino


Module Module1

    Sub Main()
    Dim Led As NetduinoGo.RgbLed = New NetduinoGo.RgbLed
    Dim PB As NetduinoGo.Button = New NetduinoGo.Button
    While (True)
      If PB.IsPressed Then
        Led.SetColor(100, 0, 100)
      Else
        Led.SetColor(0, 0, 0)
      End If
      Thread.Sleep(50)
    End While
  End Sub

End Module
But the program throws an exception "An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in NetduinoGo.RgbLed.dll"

The same program written in C# works as expected.
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.Threading;

static class Module1
{
  public static void Main()
  {
    NetduinoGo.RgbLed Led = new NetduinoGo.RgbLed();
    NetduinoGo.Button PB = new NetduinoGo.Button();
    while ((true))
    {
      if (PB.IsPressed)
      {
        Led.SetColor(100, 0, 100);
      }
      else
      {
        Led.SetColor(0, 0, 0);
      }
      Thread.Sleep(50);
    }
  }
}
Am I doing something wrong or is this a bug?

Chuck

Attached Files



#2 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 25 November 2012 - 10:08 PM

try this. I made a few changes. Notice the "Imports SecretLabs.NETMF.Hardware.NetduinoGo". You are using the wrong dll. Second, add () to the end of RgbLed and Button. Most likely its the Import causing the issue.

Imports Microsoft.SPOT
Imports Microsoft.SPOT.Hardware
Imports SecretLabs.NETMF.Hardware
Imports SecretLabs.NETMF.Hardware.NetduinoGo
Imports System.Threading

Module Module1

	Sub New()
	End Sub

	Sub Main()
    	Dim Led As New NetduinoGo.RgbLed()
    	Dim PB As New NetduinoGo.Button()
    	While (True)
        	If PB.IsPressed Then
            	Led.SetColor(100, 0, 100)
        	Else
            	Led.SetColor(0, 0, 0)
        	End If
        	Thread.Sleep(50)
    	End While
	End Sub

End Module


#3 carb

carb

    Advanced Member

  • Members
  • PipPipPip
  • 352 posts
  • LocationCrystal River, Florida

Posted 25 November 2012 - 11:07 PM

try this. I made a few changes. Notice the "Imports SecretLabs.NETMF.Hardware.NetduinoGo". You are using the wrong dll. Second, add () to the end of RgbLed and Button. Most likely its the Import causing the issue.

Thanks Dave,

I will try that.

I thought under 4.2.1 you should use only the SecretLabs.NETMF.Hardware.Netduino, least that is what I thought I read in one of the threads.

The code converter removed the ()s and it did not show any errors.

Chuck

#4 carb

carb

    Advanced Member

  • Members
  • PipPipPip
  • 352 posts
  • LocationCrystal River, Florida

Posted 25 November 2012 - 11:27 PM

Dave,

No joy,
  • I replaced the code, same results.
  • Changed from "Imports SecretLabs.NETMF.Hardware.Netduino" to "Imports SecretLabs.NETMF.Hardware.NetduinoGo", same results.
  • Deleted C:\Program Files (x86)\Secret Labs\Netduino SDK\Assemblies\v4.2 directory and reinstalled 4.2.1, same results.
Visual Basic just does not want to play. :(

Chuck

Attached Files



#5 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 26 November 2012 - 02:39 AM

There may be VB issues. As a former VB4,5,6 programer, then VB.NET programmer since 1999-2000-ish, I took the opportunity to learn C# while working with the Netduino. It gave me a good excuse to crash course on it. You may want to do the same. It looks daunting in the beginning (especially for me who never really worked in C languages) but I learned it pretty quickly. You can also download SharpDevelop or even use many of the conversion sites on the web to write code in VB.NET then convert to C# when you hit a wall. I find myself working in C# a lot more than VB now except for my VB centric clients who have a difficult time letting go of legacy technologies.

#6 carb

carb

    Advanced Member

  • Members
  • PipPipPip
  • 352 posts
  • LocationCrystal River, Florida

Posted 26 November 2012 - 02:47 AM

I find myself working in C# a lot more than VB now except for my VB centric clients who have a difficult time letting go of legacy technologies.

Thanks for looking at it.

I have been trying to write in both C# and VB, I originaly started with Turbo Pascal and later Delphi.

I use several converters to help me when I get stuck. The latest was from an attempt to upgrade an earlier program that used the shieldbase, LCD Display, 2 pots, 1 RgbLed, a TMP-36 temperature sensor and drove 4 relays. It was just something I put together when the Neduino Go came out. I wrote it in C# then converted to VB to help some other members. I just hate using the darn brackets.

Chuck

#7 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 05 December 2012 - 11:02 PM

Hi Chuck, Could you please send me a ZIP of your entire solution directory for the above repro? I can then deploy it to a Netduino Go here and see what Visual Basic is grumpy about. :) We definitely want to make sure VB works well on these boards. Chris

#8 carb

carb

    Advanced Member

  • Members
  • PipPipPip
  • 352 posts
  • LocationCrystal River, Florida

Posted 05 December 2012 - 11:19 PM

Chris, Here is the zip file. Hopefully it is not to much, but I zip the project directory. Thanks, Chuck

Attached Files



#9 carb

carb

    Advanced Member

  • Members
  • PipPipPip
  • 352 posts
  • LocationCrystal River, Florida

Posted 29 December 2012 - 01:26 AM

Chris,

 

Any information on the problems while using Visual Basic? I have tried everything short of a (big) hammer.

 

Tried again tonight to SetColor of a RGBLed using a potentiometer. I get an exception for out of range on the Led. Value from the Pot is 0 to 255.

 

If I convert the code to C# it runs as expected.

 

Chuck



#10 Gutworks

Gutworks

    Advanced Member

  • Members
  • PipPipPip
  • 363 posts
  • LocationOttawa, Ontario

Posted 29 December 2012 - 04:49 AM   Best Answer

Chuck I just tried your sample and I got the same error. I recall a couple of firmware editions back Chris added support for doubles as arguments in addition to the bytes. When I use doubles as a value the code works fine. So that got me thinking...

 

I tried a couple of different ways and there seems to be a major difference in how Visual Basic and C# deal with Doubles and Bytes, or at the very least in the way it interprets the arguments for the SetColor() constructors.

 

SetColour() has two constructors:

 

SetColor(Double, Double, Double) SetColor(Byte, Byte, Byte)

Visual Basic seems to default to the the Double constructor unless you explicitly cast the values as bytes. Conversely, C# will default to a Byte unless you explicitly tell it to use Doubles. This can be done by easily where 0.0 and 0f both represent a Double and work in this case.

 

So for example:

led.SetColor(1,0,1);

In VB, this will default to the Double constructor, however in C# it will assume that you are passing three bytes and will use the Byte constructor. 

 

In VB if you want to use bytes you can use (though I'm sure there are more elegant ways):

 

Led.SetColor(CType(100, Byte), CType(0, Byte), CType(100, Byte))

 

It is a rather peculiar behaviour between the two languages, and I'm not sure why they would act differently. How even more perplexing is that in VB Intellisense will actually show the Byte constructor as you type out SetColor(100, 0, 100), but after finishing the line of code you can hover over the method and see that it has defaulted back to the Double one. 

 

In any case, I hope this helps somewhat. And perhaps someone can provide us some theory as why this is happening and if it's a bug warranting a fix within the .Net Mirco Framework. 

 

Cheers,

Steve

 

* EDIT: Removed the Canadian flavour from my colours. :)


Edited by Gutworks, 29 December 2012 - 04:57 PM.


#11 carb

carb

    Advanced Member

  • Members
  • PipPipPip
  • 352 posts
  • LocationCrystal River, Florida

Posted 29 December 2012 - 06:03 AM

Thanks Steve,

 

Still trying to move towards C#, but I wanted to figure out why programs that had been working, whould now throw exceptions, some of which are a little cryptic in nature. In any case maybe this will help others that are still hanging onto Visual Basic to avoid case sensitive variables and those darn

{   brackets;}

 

Chuck



#12 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 29 December 2012 - 08:43 PM

Chuck,

 

Give in to the brackets, resistance is futile, you will be assimilated.

 

You will become one with the Collective....

 

Regards,

The C# Collective


To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.