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

Method parameter "this byte"


  • Please log in to reply
6 replies to this topic

#1 Jesper

Jesper

    New Member

  • Members
  • Pip
  • 3 posts

Posted 20 February 2012 - 09:39 AM

Hi
This is my first post so hope i'm doing it right ;).

My experiences with Netduino and C#, is only basic knowledge.

My problem is that i can't get my Method to compile with the Micro Framework,
the problem is that i can't use "this byte". Is there a workaround for this solution.

public static class Class1
    {
        public static bool GetBit(this byte b, int bitNumber)
        {
            return (b & (1 << bitNumber)) != 0;
        }
    }

I use the code like this
if (signExp.GetBit(7)) sign = 1;


#2 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 20 February 2012 - 12:41 PM

Hi Jesper and welcome to the forums! this byte, what are you trying to do? The keyword 'this' doesn't belong there. Doesn't it work without?
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#3 Magpie

Magpie

    Advanced Member

  • Members
  • PipPipPip
  • 279 posts
  • LocationAustralia (south island)

Posted 20 February 2012 - 01:02 PM

Hi Jesper

Your code is trying to use what are called extension methods.

You can read about them here.
extension methods

you can probably just drop the 'this' and change your call.
STEFF Shield High Powered Led Driver shield.

#4 Geancarlo

Geancarlo

    Member

  • Members
  • PipPip
  • 24 posts

Posted 20 February 2012 - 01:19 PM

You can try this out http://mikedodaro.ne...icro-framework/

#5 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 20 February 2012 - 02:08 PM

My problem is that i can't get my Method to compile with the Micro Framework,

If you compile your application for .NET Micro Framework version 4.1, you'd need to add ExtensionAttribute as suggested by Geancarlo, in .NET MF version 4.2 the attribute is already included in core libraries and no changes are necessary.

#6 Jesper

Jesper

    New Member

  • Members
  • Pip
  • 3 posts

Posted 20 February 2012 - 02:21 PM

Hi Jesper and welcome to the forums!

this byte, what are you trying to do? The keyword 'this' doesn't belong there. Doesn't it work without?


Thanks Stefan..

I'm trying to reuse some code i have written in a PC application, and now the its the Netduino's time to do job.

I won't to make my call like this
if (testData.GetBitV1(0)) Console.WriteLine("BitV1: 0 is true");
With one parameter and not tow.

But with the Netduino program it seams that i can't write my extension method like this. To get it working i need tow parameter the source and the input value.


Console application to test.

using System;
using Win32TestFunc.Win32TestFunc;

namespace Win32TestFunc
{
    class Program
    { 
        static void Main()
        {
            const byte testData = 1;

            if (testData.GetBitV1(0)) Console.WriteLine("BitV1: 0 is true");                      // This solution i "need" but dose not work in the Micro framework
            if (BitHelper.GetBitV2(testData, 0)) Console.WriteLine("BitV2: 0 is true");   // This solution is working in the Micro framework
            Console.ReadLine();
        }
    }
}

The class

namespace Win32TestFunc
{
    namespace Win32TestFunc
    {
        public static class BitHelper
        {
            public static bool GetBitV1(this byte b, int bitNumber)
            {
                return (b & (1 << bitNumber)) != 0;
            }

            public static bool GetBitV2(byte b, int bitNumber)
            {
                return (b & (1 << bitNumber)) != 0;
            }
        }

    }

}


#7 Jesper

Jesper

    New Member

  • Members
  • Pip
  • 3 posts

Posted 20 February 2012 - 02:23 PM

You can try this out http://mikedodaro.ne...icro-framework/


Thanks i will take a look :o)
Jesper




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.