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

C# code structure

C# code

  • Please log in to reply
7 replies to this topic

#1 bhw13

bhw13

    New Member

  • Members
  • Pip
  • 3 posts

Posted 13 January 2013 - 07:34 PM

Hi All,

 

I am new to the Netduino and new to C# programming. I have been reading the very well written Chris Walker Getting Started book, but I would like clarification on how the following code line is constructed:

OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);

Is OutputPort a class and led an instance of the class? Are the items in the parentheses parameters? Is this code line creating the instance variable led or is some different process going on here? Maybe i haven't read far enough in my C# programming books.

Thanks very much



#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 13 January 2013 - 09:59 PM

Hi bhw13, OutputPort is a class (Microsoft.SPOT.Hardware.OutputPort) which lets you set the voltage of a pin to 3.3V or 0V (GND). Pins.ONBOARD_LED returns a Cpu.Pin value which tells OutputPort which microcontroller pin to use. "led" is the instance of OutputPort, in this example, which now controls the onboard LED. [The onboard LED is connected to a microcontroller pin--which why it's "Pin" value.] Welcome to the Netduino community, Chris

#3 bhw13

bhw13

    New Member

  • Members
  • Pip
  • 3 posts

Posted 14 January 2013 - 04:19 AM

Thanks, Chris for the quick response. I also received gracious responses from others in the chat room today who pointed me in the direction of the concept of a constructor to explain the parameters inside the parentheses of OutputPort. I have a few C# books, but the presentation and explanation of this doesn't seem straightforward. If anyone can shed some further light on this (I'm aware this is not really a C# teaching forum), I'd appreciate it- I'm trying to understand a little better the structure of the C# language applied in this case.



#4 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 14 January 2013 - 07:56 AM

Why not write some class of your own and step through the code to see what happens as parameters are passed to the constructor.

 

The constructor is a method that is called to instanciate a class, i.e. create a live object from a class definition.

 

For example you could try this:

 

[font="'courier new', courier, monospace;"]class Person[/font]

[font="'courier new', courier, monospace;"]{[/font]

[font="'courier new', courier, monospace;"] string _name;[/font]

[font="'courier new', courier, monospace;"] int _age;[/font]

 

// this is the class constructor

[font="'courier new', courier, monospace;"] public Person(string name, int age)[/font]

[font="'courier new', courier, monospace;"] {[/font]

[font="'courier new', courier, monospace;"]  // this will make the object "remember" the paramters[/font]

[font="'courier new', courier, monospace;"] _name = name;[/font]

[font="'courier new', courier, monospace;"] _age = age;[/font]

[font="'courier new', courier, monospace;"] }[/font]

 

[font="'courier new', courier, monospace;"] public void SayHello()[/font]

[font="'courier new', courier, monospace;"] {[/font]

[font="'courier new', courier, monospace;"]   System.Diagnostics.Print("Hi, my names is" + name + " and I'm " + age.ToString() + " years of age.");[/font]

[font="'courier new', courier, monospace;"]  }[/font]

[font="'courier new', courier, monospace;"]}[/font]

 

You would then create an instance of the class like so:

 

[font="'courier new', courier, monospace;"]var peter = new Person("Peter", 25);[/font]

 

The object "peter" will remember it's name and age and persist these throughout the object's lifetime (e.g. until you delete the object). Run the code and pay attention to what gets printed in the debug window when you add a call to the SayHello(.) method:

 

[font="'courier new', courier, monospace;"]peter.SayHello();[/font]

 

Hope this help you in understanding the concept of passing parameters to a class constructor.



#5 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 14 January 2013 - 12:39 PM

Hi All,

 

I am new to the Netduino and new to C# programming. I have been reading the very well written Chris Walker Getting Started book, but I would like clarification on how the following code line is constructed:

OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);

Is OutputPort a class and led an instance of the class? Are the items in the parentheses parameters? Is this code line creating the instance variable led or is some different process going on here? Maybe i haven't read far enough in my C# programming books.

Thanks very much

 

Hi bhw13 and welcome to the community.  You may want to check out something like 30 Days to Learn C# from Tuts+. They offer good courses and a good pace.  You take a section a day and each section is between 10 minutes and 30 minutes.

 

Happy Programming.



#6 dustmouse

dustmouse

    Advanced Member

  • Members
  • PipPipPip
  • 31 posts
  • LocationEdgewater, CO

Posted 15 January 2013 - 04:05 AM

Hi All,

 

I am new to the Netduino and new to C# programming. I have been reading the very well written Chris Walker Getting Started book, but I would like clarification on how the following code line is constructed:

OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);

Is OutputPort a class and led an instance of the class? Are the items in the parentheses parameters? Is this code line creating the instance variable led or is some different process going on here? Maybe i haven't read far enough in my C# programming books.

Thanks very much

 

It sounds to me like you have the concepts down.  I would suggest picking up an intro to C# book (or another language - but obviously C# makes more sense for Netduino development), unless you are mostly interested in the hardware side of things.


Check out my Netduino projects on GitHub.


#7 bhw13

bhw13

    New Member

  • Members
  • Pip
  • 3 posts

Posted 16 January 2013 - 04:12 AM

Ok, thank-you all again for your helpful suggestions. I found a short youtube video that talked about constructors.

I came up with the following piece of code. The constructor is created by public Program(). The second line of code that I'm showing here seems to have the same form as the line of code

in my original question. The 5 and 10 are obviously int's, but I imagine that you can use variables for the parameters. Sorry, again, for all this simple stuff.

 

static void Main(string[] args)

{ Program pr = new Program (5,10);

  Console.Read();  }

public Program( int s, int t)

{  Console.WtiteLine(s);

Console.WriteLine(t);  }



#8 hanzibal

hanzibal

    Advanced Member

  • Members
  • PipPipPip
  • 1287 posts
  • LocationSweden

Posted 16 January 2013 - 12:29 PM

Yes, you can pass varibles to a constuctor, e.g. like so:

 

static void Main(string[] args){   int a = 5;   int b = 10;   Program pr = new Program (a, b );   Console.Read();}






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.