Main Site Documentation

Question about new


#1

for the following:

SPI devicePort = new SPI(new SPI.Configuration(Cpu.Pin.GPIO_Pin10,
                       true, 1, 0, false, false, 1000, SPI.SPI_module.SPI1));

why is the second “new” needed? it escapes me & no longer seems obvious


#2

Because the SPI constructor only takes a single parameter - a SPI.Configuration object. So, you have to create a “new” SPI.Configuration object that is then passed to the constructor of the “new” SPI object. Make sense?

Basically, it’s doing the exact same thing as this…

SPI.Configuration config = new SPI.Configuration(Cpu.Pin.GPIO_Pin10, true, 1, 0, false, false, 1000, SPI.SPI_module.SPI1);
SPI devicePort = new SPI(config);


#3

Thanks…now I see…sometimes you need the 2nd new & sometimes not–I guess it depends on the type of constructor.


#4

It’s not the type of constructor so much as it’s the type of object that you need. Native types (int, string, bool, etc.) do not require the use of “new” but object types do. It doesn’t matter if you’re in a constructor or just writing function code like I did in the second example.


#5

In .Net these are called value types not native types. These are objects as well.

@ Hoyt

Take a look at this article:

http://www.codeproject.com/Articles/76153/6-important-NET-concepts-Stack-heap-Value-types-re