C# help
#1
Posted 06 March 2011 - 08:12 AM
#2
Posted 06 March 2011 - 10:31 AM
#3
Posted 06 March 2011 - 03:46 PM
#4
Posted 06 March 2011 - 04:04 PM
Total BOCS Traveled Distance: 9708 miles | States Visited: 5
Track the Box
#5
Posted 06 March 2011 - 06:24 PM
#6
Posted 06 March 2011 - 07:23 PM
"C# may be more like java syntax but it's nearly identical to VB.NET"
Vs.
"Ruff, I really think that C# is much more like Java, instead of VB."
since it proofs my feeling.
Sorry for this confusing question, I came back from a 13 hour netduino session and was too tired and wasn't able to express myself properly.
I think I know well about OOP patterns, so these link from microsoft is a little needles for me.
so first: I understand that generics may be too much overhead for micro.net. So am I able to use any kind of Collection?
to the other problem with my static class: Its not a static class, it was only declared static in program.class. It should encapsulate the patterns for a 7-digit-led. Here is the code:
public class Program { static Config config = new Config(Pins.GPIO_PIN_D10, Pins.GPIO_PIN_D8, Pins.GPIO_PIN_D11, Pins.GPIO_PIN_D6, Pins.GPIO_PIN_D7, Pins.GPIO_PIN_D1, Pins.GPIO_PIN_D13, Pins.GPIO_PIN_D2); public static void Main() { config.MaxNumber = 9; InterruptPort button = new InterruptPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth) button.OnInterrupt +=new NativeEventHandler(button_OnInterrupt); Thread.Sleep(Timeout.Infinite); } static void button_OnInterrupt(uint data1, uint data2, DateTime time) { if(data2 == 0) { config.turnNextNumberOn(); } } } public class Config { public OutputPort segmentA; public OutputPort segmentB; public OutputPort segmentC; public OutputPort segmentD; public OutputPort segmentE; public OutputPort segmentF; public OutputPort segmentG; public OutputPort segmentPoint; public int MaxNumber { get { return MaxNumber; } set { MaxNumber = value > 9 ? 9 : value; } } private int ledNumber; private bool state; public Config(Cpu.Pin a, Cpu.Pin b, Cpu.Pin c, Cpu.Pin d, Cpu.Pin e, Cpu.Pin f, Cpu.Pin g, Cpu.Pin point) { segmentA = new OutputPort(a, false); segmentB = new OutputPort(b, false); segmentC = new OutputPort(c, false); segmentD = new OutputPort(d, false); segmentE = new OutputPort(e, false); segmentF = new OutputPort(f, false); segmentG = new OutputPort(g, false); segmentPoint = new OutputPort(point, false); state = false; ledNumber = -1; MaxNumber = 9; } public void turnNextNumberOn() { int next = ledNumber + 1 <= MaxNumber ? ledNumber +1 : 0; turnLedNumber(next, true); } public void turnLedNumber(int number, bool state) { if (this.state && state == true) { turnLedNumber(ledNumber, false); } switch (number) { case 0: turnNumberZero(state); break; //....... } this.state = state; this.ledNumber = number; } private void turnNumberOne(bool state) { segmentB.Write(state); segmentC.Write(state); } private void turnNumberZero(bool state) { turnNumberSeven(state); segmentD.Write(state); segmentE.Write(state); segmentF.Write(state); } //...... }
I can't see the Problem at all with this code
#7
Posted 07 March 2011 - 12:21 AM
Your first problem is that you have an infinite recursion on this code, on both the getter and setter side:I can't see the Problem at all with this code
public int MaxNumber { get { return MaxNumber; } set { MaxNumber = value > 9 ? 9 : value; } }
One way to solve this is to back it with a field, like so:
private int maxNumber; public int MaxNumber { get { return maxNumber; } set { maxNumber = value > 9 ? 9 : value; } }
- Ruff likes this
#8
Posted 07 March 2011 - 12:38 AM
Total BOCS Traveled Distance: 9708 miles | States Visited: 5
Track the Box
#9
Posted 07 March 2011 - 06:55 AM
You can use ArrayList if you want, you simply have to use a cast when retrieving the object from the list.So am I able to use any kind of Collection?
class Test { ...} ArrayList a = new ArrayList(); Test t = new Test(); a.Add(t); . . . t = (Test) a[4]; // Get an object from the list.It's not as pleasant as generics but it should work.
regards,
Mark
To be or not to be = 0xFF
Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life
Follow @nevynuk on Twitter
#10
Posted 07 March 2011 - 12:09 PM
Your first problem is that you have an infinite recursion on this code, on both the getter and setter side:
public int MaxNumber { get { return MaxNumber; } set { MaxNumber = value > 9 ? 9 : value; } }
One way to solve this is to back it with a field, like so:
private int maxNumber; public int MaxNumber { get { return maxNumber; } set { maxNumber = value > 9 ? 9 : value; } }
Thanks for that!!! I think it will solve a bunch of problems. The Tutorial I use (german http://openbook.gali...ting.de/csharp/) didn't make that clear, but as You told me now, it makes absolutely sense.Now it looks more like a VB.Net Property
Also it's fairly common to forgo the formal property syntax and just use public variables, but it's certainly valid code once you address the other comments. Anyways part of the reason for the Java/VB 'confusion' was we were addressing different things. I think the other poster's explanation was a little technical, but accurate. When I said it was like VB I meant, if you're familiar with using VB.NET then that knowledge will translate more directly to C# since the .net libraries are identical between each. The way you knew to ask if there was a collection you could use means you're familiar with that set of classes, and that set is available to anything referencing the .net libraries. Whether they're in the microframework is the only place the learning curve should exist. I really wasn't addressing what it compiles to or alternative libraries from Oracle or any of that stuff which for a beginning programmer is less important than "how do I make it work"
I can't forget this Property if I have logic in the setter, and youre right that VB helps me to understand internal class libs, while java helps more for the syntax. Am I right, that everything in C# 'methods' is passed as 'Copy' like in VB 'ByVal' and If I wanna have a reference I have to declare VB's 'ByRef' simply as 'ref Type Name' or 'out Type Name'?
thx, that is what I have expected. So I can put i.e. a Test.Class instance as a[0] and Test2.class instance as a[1]?You can use ArrayList if you want, you simply have to use a cast when retrieving the object from the list.
class Test { ...} ArrayList a = new ArrayList(); Test t = new Test(); a.Add(t); . . . t = (Test) a[4]; // Get an object from the list.It's not as pleasant as generics but it should work.
regards,
Mark
Should I prefer to use Arrays(I think performance can get important programming Micocontrollers)?
#11
Posted 07 March 2011 - 12:47 PM
Yep: you may set any kind of object in the ArrayList, since is actually an object[].
About parameters. In C# everything is considered by reference, but when the parameter inherits from ValueType (e.g. a struct), there is a copy of the object.
This sounds slightly different that Java, because there the native types don't derive from "object", but in C# does. Formally, I like more the C# approach, so *any* object inherits from "object"...
It worths noting that *NOT* all inherits from object, even in C#: read this...
http://blogs.msdn.co...rom-object.aspx
Hope it helped.
Cheers
Mario
#12
Posted 08 March 2011 - 03:38 PM
thx for the answer. So my assumption is right, that I can put different Objects into the same ArrayList.Ruff, an ArrayList is just an Array (i.e. object[]), but wraps it by adding some useful function like Add and Remove. "Pure" arrays aren't modifiable, but ArrayList resize automatically the underlying array upon the real usage.
Yep: you may set any kind of object in the ArrayList, since is actually an object[].
About parameters. In C# everything is considered by reference, but when the parameter inherits from ValueType (e.g. a struct), there is a copy of the object.
This sounds slightly different that Java, because there the native types don't derive from "object", but in C# does. Formally, I like more the C# approach, so *any* object inherits from "object"...
It worths noting that *NOT* all inherits from object, even in C#: read this...
http://blogs.msdn.co...rom-object.aspx
Hope it helped.
Cheers
Mario
What I'm not yet understanding 100% is these 'struct' ValueType.
I know structs from C, But there it makes sense for the lack of Objects. Are ValueTypes the same that in Java is known as Raw-Rypes?Does it mean I can derive from ValueTypes to design my own?
another question: does one 'bool' take more space in Ram than a byte? Does it make sense to use bytes instead of ints?
I have an issue that compiler won't compile this statement:
byte mybyte = 100 mybyte = mybyte + x > 255 ? mybyte + x - 255 : mybyte+x; // where x stands for example for 150Compiler tells me that byte is explicity convertet and cannot be converted implicit.
The problem in this line is i.e. the '255' -> recognized as an int while 100 in instantiation makes no problems at all.
#13
Posted 08 March 2011 - 04:01 PM
Instead of:
byte mybyte = 100 mybyte = mybyte + x > 255 ? mybyte + x - 255 : mybyte+x; // where x stands for example for 150Try this:
byte mybyte = 100 mybyte = mybyte + x > 255 ? (byte)(mybyte + x - 255) : (byte)(mybyte+x); // where x stands for example for 150While the ++ and -- operators "roll over" in C#, you need to manually cast values and/or expressions which could result in a value outside the range of the result's type.
Chris
#14
Posted 08 March 2011 - 04:04 PM
Hi,[...] What I'm not yet understanding 100% is these 'struct' ValueType.
I know structs from C, But there it makes sense for the lack of Objects. Are ValueTypes the same that in Java is known as Raw-Rypes?Does it mean I can derive from ValueTypes to design my own? [...]
no, structs in .NET are not the same as raw types in Java, as far as I know. There is no real equivalent for structs in Java. The only thing which is similiar to structs are primitive types in Java.
In .NET primitive types (= structs) can also include Methods.
You can make your own struct by using the struct keyword instead of the class keyword.
Regarding the ValueType class:
[...] Aside from serving as the base class for value types in the .NET Framework, the ValueType structure is generally not used directly in code. However, it can be used as a parameter in method calls to restrict possible arguments to value types instead of all objects, or to permit a method to handle a number of different value types. [...]
#15
Posted 08 March 2011 - 04:16 PM
You can also use modulus operator:
byte mybyte = 100 mybyte = mybyte + x > 255 ? mybyte + x - 255 : mybyte+x; // where x stands for example for 150
mybyte = (byte)((mybyte + x) % 255);
#16
Posted 08 March 2011 - 04:29 PM
http://blogs.msdn.com/b/ericlippert/
I also don't know so much Java, but I knew there are two kinds of "integers", for example: one is "native" (raw, I guess) and the other is an object (where you may apply methods).
In C# there's not difference: you may use "int" and "Int32". The first syntax is C# and it's compiled always as the "Int32" object.
I think that the blog of Eric contains a lot of useful answers for you.
About the "bool".
I think that a bool is a 32-bit field, masked to a flag (here in Venezia is Carnival ), but that's my hypothesis.
I've done some test time ago, and I've seen that manipulating types other than a 32-bit, it takes much more instructions (so time) to compute. That's on a 32-bit machine, of course.
You may try to yourself: declare two variables, an int and a byte, then make some basic operation (e.g. increment, addition, etc). When you peek the IL-op codes that the compiler generates, you'll see that the int is much more close to the CPU other than the byte.
Finally, about the code.
C# tries to consider the integer numbers as "int", so you should write:
byte mybyte = 100; mybyte = mybyte + x > 255 ? (byte)(mybyte + x -255) : (byte)(mybyte+x);even "x" is a byte.
NOTE: by writing "mybyte+x", you *know* that it's an integer addition. If it would be a byte, it won't get never a value higher than 255 and the condition would be always false.
Instead, if you must have a byte, I'd suggest:
byte mybyte = 100; mybyte = (byte)(mybyte + x);or *more correctly*:
byte mybyte = 100; mybyte = unchecked((byte)(mybyte + x));The "unchecked" keyword disables the overflow checking for the basic data operations. Normally that's the default, so you may discard it.
Hope it helped.
Cheers
Mario
#17
Posted 08 March 2011 - 06:26 PM
Whilst you can put any type of object in the list, you need to make sure the object is the right type when you take it out. Consider the following:thx for the answer. So my assumption is right, that I can put different Objects into the same ArrayList.
class A {...} class B {...} A a = new A(); B b = new B(); ArrayList list = new ArrayList(); list.Add(a); list.Add(B); class A result; result = (A) list[1];This should raise an error as list element 1 is of type B and not type A. In order to get around this problem you need to check out the is and as keywords.
Regards,
Mark
To be or not to be = 0xFF
Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life
Follow @nevynuk on Twitter
#18
Posted 09 March 2011 - 12:21 AM
You can also use modulus operator:
mybyte = (byte)((mybyte + x) % 255);
uhh I like that: Simple, short and clear.
did I hear "roll over"?? thats interesting! What else in C# rolls over?Ruff,
Instead of:
byte mybyte = 100 mybyte = mybyte + x > 255 ? mybyte + x - 255 : mybyte+x; // where x stands for example for 150Try this:
byte mybyte = 100 mybyte = mybyte + x > 255 ? (byte)(mybyte + x - 255) : (byte)(mybyte+x); // where x stands for example for 150While the ++ and -- operators "roll over" in C#, you need to manually cast values and/or expressions which could result in a value outside the range of the result's type.
Chris
Thanks for all the help. These questions came up as I tried to write a library for DotMatrixes. I want to share this code here if it is working. I have some issues debugging arrays. I have lots of 'flags' to store. These Flags currently look for example like this:
byte[][] A = { new byte[]{1, 1, 1, 1, 1}, new byte[]{1, 0, 0, 0, 1}, new byte[]{1, 0, 0, 0, 1}, new byte[]{1, 0, 0, 0, 1}, new byte[]{1, 1, 1, 1, 1}, new byte[]{1, 0, 0, 0, 1}, new byte[]{1, 0, 0, 0, 1} }And this for the whole Alphabet. I considered to store them as bool but
bool[][] A = { new bool[]{true, true, true, true, true}, new bool[]{true, false, false, false, true}, new bool[]{true, false, false, false, true}, new bool[]{true, false, false, false, true}, new bool[]{true, true, true, true, true}, new bool[]{true, false, false, false, true}, new bool[]{true, false, false, false, true}, }isn't very readable while it didn't feel to be faster.
While my Code is working if I'm only showing one Letter, or simply "switch" them it won't work if I want to let them 'run' over the screen. There have to be a big logical issue in my shift method, nut I can't find it since debugger is only showing me '?' as value for a byte!
I think memory is very limited in comparation with a "normal" .Net system or even a mobile device. What can I do to a) see whats going on and b ) not burning amounts of Bytes?
An idea I had was to store one int as a column taking its binary Value as flags. But I could't find any useful implementation or API to get or set binary values like I can do in C. I only find same Syntax as in C for Hex using 0x..
To convert them manually store them as String and parse it.....
isn't binary data the closest 'type' to a processor?
I'll uploaded the as-is state. I hope it is clear enaugh. Feel free to ask, correct or suggest anything you like.
I had every bit[][] as ref in every method before, because i thought that caused the problem in the shiftPattern method
Attached Files
#19
Posted 09 March 2011 - 07:28 AM
Not sure if this is implemented in NETMF (it is part of the full .NET), but there is a Flags attribute which can be applied to enums (can't verify it at the moment as I'm away from my N+ dev machine). This might make your code more readable and could make it more compact depending upon what you are doing.I have lots of 'flags' to store. These Flags currently look for example like this:
[Flags] public enum Sides = { Left = 1, Right = 2, Top = 4, Bottom = 8 }You can then use these with the bitwise operators to provide combinations:
Sides leftAndRight = Sides.Left | Sides.Right;As I say, this will need verifying to see if it is in NETMF.
Regards,
Mark
To be or not to be = 0xFF
Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life
Follow @nevynuk on Twitter
#20
Posted 09 March 2011 - 07:51 AM
What's wrong in the following code?
byte[] vector = new byte[256]; for (byte i = 0; i < 256; i++) { vector[i] = i; }The problem is that the for-loop will never completes, because once i=255, its increment rolls it to zero again. So the condition is always satisfied.
I'd rather use int anywhere and bytes only for raw-streaming. I guess there's no overhead of memory, nor loss of performance.
Cheers
Mario
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users