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.

Foozinator

Member Since 06 Dec 2010
Offline Last Active Nov 27 2012 04:24 AM
-----

Posts I've Made

In Topic: coding style discussion

16 March 2011 - 07:32 PM

Looks to me that the caller of GetValue() made an error (when declaring his local variable), not that var assumed the wrong type returned from GetValue().


My point was that error (incorrect assumption) doesn't show up as a compile error in this case, where if the developer used an explicit type instead of var, there would be either a compile error or, in this case, no problem at all.

In Topic: coding style discussion

15 March 2011 - 08:32 PM

The interface approach does not work, and in fact your code does not even compile. I think it would help you understand what I am getting at if you try to compile and run your proposed solution using the test program I painstakingly provided. If the output matches what is expected that would be a very interesting result. If not, you can think about alternate approaches. This will help you verify whether your ideas work without having to post proposals to this forum. I don't think this will take very much additional effort, and additionally I think it will bring clarity to the discussion.


Sorry about the compiler errors, but those are easily fixed:

		public static void Func2(ABOnly value)
		{
			value.A = 13;
			value.B = 14;
		}

		public static void Func3(MyStruct[] array)
		{
			ABOnly local = array[3];

			local.A = 13;
			local.B = 14;
		}

		public interface ABOnly
		{
			int A { get; set; }
			int B { get; set; }
		}

		public struct MyStruct : ABOnly
		{
			public int A { get; set; }
			public int B { get; set; }
			public int C { get; set; }
		}

It's better encapsulation to use properties instead of fields, anyway.

More importantly, does this solve the problem?

I'm sorry I don't have the time to retrofit my example into yours. I'm posting from work and they expect me to get stuff done, sometimes. ;)

In Topic: coding style discussion

15 March 2011 - 07:59 PM

I think by changing 'struct' to 'class' you have redefined the question and sidestepped the whole point. You won't be able to get your code to work with 'struct'.

If it helps, here is a self-contained program. The goal is to set the values A and B of array[3] in-place

  • without disturbing the value of C
  • without copying a whole new struct
  • only referring to array[3] inside Func2() once
  • without making any function calls from Func2


I'm still not sure I understand what you're getting at. Using an interface will satisfy the requirements you listed, I think, but the structs will be boxed. If protecting the value of C is what's important, than either interfaces or better encapsulation looks like a good solution.

		public static void Func2(ABOnly value)
		{
			value.A = 13;
			value.B = 14;
		}

		public static void Func3(MyStruct[] array)
		{
			ABOnly local = array[3];

			local.A = 13;
			local.B = 14;
		}

		public interface ABOnly
		{
			int A;
			int B;
		}

		public struct MyStruct : ABOnly
		{
			public int A;
			public int B;
			public int C;
		}


If you mean accessing the struct without boxing (for performance reasons), I can see your point, but the performance hit from boxing is negligible compared to many other things you find in managed programming and algorithms in general. Unless this is a thought exercise, you should keep in mind that performance tuning is best when surgically applied to specific problem areas.

In Topic: coding style discussion

15 March 2011 - 07:27 PM

Found a reference to overflow checking in the full Framework. http://www.codeproje...w_checking.aspx

In Topic: coding style discussion

15 March 2011 - 07:16 PM

That's not true.
The problem usually arises because none activates the checking options on numerical operations.
By default, you implicitly accept that operations on numbers could roll-over, overflows, etc.


Yes, turning on overflow checking would help find the problem. Is this available on Compact Framework projects? I can't find it.

Also, in at least some implementations (if not all), this is still not a compile-time check, but a runtime check, IIRC.

It does not matter to implement that function, but it would solve the problem I have posted above.
On a PC you won't care about it, but on a small device where the performance is important, the ability to access the field of a structure as fast as possible I think it's important.
Actually there's no way in C#, unless you create a custom function just to do it.
Upon this point (that I raised), I'd agree with Eric Lippert of MS, that often explains us the ratio of benefits/costs of some feature.


I'm not sure what you're getting at. The code example you gave does work (with a few fixes).

	class SA
	{
		public int A { get; set; }
		public int B { get; set; }

		public static void Method()
		{
			SA[] sa = new SA[10];
			sa[3] = new SA();
			sa[3].A = 123;
			var s = sa[3];
			s.B = 456;    //won't work!
		}
	}

If you mean accessing an object without the "unboxing" you get with a reference, you can make the declaration using struct instead of class (DateTime is an example).

EDIT: Corrected the spelling of "struct"

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.