Originally posted by minestrone
View Post
- Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
- Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!
Collapse
You are not logged in or you do not have permission to access this page. This could be due to one of several reasons:
- You are not logged in. If you are already registered, fill in the form below to log in, or follow the "Sign Up" link to register a new account.
- You may not have sufficient privileges to access this page. Are you trying to edit someone else's post, access administrative features or some other privileged system?
- If you are trying to post, the administrator may have disabled your account, or it may be awaiting activation.
Logging in...
Previously on "Passing objects to methods then changing the object's state"
Collapse
-
Thanks for that. It's always good to learn a bit more about the capabilities of other languages.Originally posted by VectraMan View PostThat's not the same effect. You're talking about pass by value, which means a potentially expensive memory copy just to avoid the scenario where the function might change the original data, and wouldn't stop the programmer trying to change the data and spending ages debugging why it wasn't working.
In C++ you pass a constant reference to the object, which means you have limited read-only access to the original object (functions that are marked const), but it's still the original object. Trying to change the object results in a compile error.
The OP's rant was obviously down to spending ages trying to find a bug introduced by some junior programmer. If it had been done in C++, the bug would never have existed because the code wouldn't have compiled.
I don't think there's any way to take a readable/writeable object in C# and temporarily make it read only on the fly in the way you described. The nearest I can think of is implementing an interface in the class that renders desired Properties read only such as:

It still relies on the author of the MangleMyClass method taking ownership of the problem, though. I suspect that the OP's problem is caused by an inexperienced developer that is authoring Methods in a way that allows them to be misused.
Leave a comment:
-
That's not the same effect. You're talking about pass by value, which means a potentially expensive memory copy just to avoid the scenario where the function might change the original data, and wouldn't stop the programmer trying to change the data and spending ages debugging why it wasn't working.Originally posted by Gentile View PostC# has structs that achieve much the same effect; if 'MyClass' were defined as a struct in my example above (which you could do simply by changing the class keyword to struct), then whether you used the ref keyword or not would affect the outcome.
However, there's a trade-off in terms of memory usage since structs are stored on the Stack rather than on the Managed Heap, and so aren't suitable for .net's Garbage Collection mechanism. In order to benefit from Garbage Collection, you need to rely on developers using Reference Types sensibly.
In C++ you pass a constant reference to the object, which means you have limited read-only access to the original object (functions that are marked const), but it's still the original object. Trying to change the object results in a compile error.
The OP's rant was obviously down to spending ages trying to find a bug introduced by some junior programmer. If it had been done in C++, the bug would never have existed because the code wouldn't have compiled.
Leave a comment:
-
C# has structs that achieve much the same effect; if 'MyClass' were defined as a struct in my example above (which you could do simply by changing the class keyword to struct), then whether you used the ref keyword or not would affect the outcome.Originally posted by VectraMan View PostThat's how the professionals do it.Code:void mangle( MyClass& objectToBeMangled ) { } void someOtherFunction( const MyClass& objectNotToBeManged ) { mangle( objectNotToBeMangled ); // compile error : cannot convert const MyClass& to MyClass& }
Seriously I find it utterly incomprehensible that the forefathers of Java and C# didn't carry this over from C++, as it makes such a difference to the reliability and maintainability of software when you use const types properly.
However, there's a trade-off in terms of memory usage since structs are stored on the Stack rather than on the Managed Heap, and so aren't suitable for .net's Garbage Collection mechanism. In order to benefit from Garbage Collection, you need to rely on developers using Reference Types sensibly.
Leave a comment:
-
That's how the professionals do it.Code:void mangle( MyClass& objectToBeMangled ) { } void someOtherFunction( const MyClass& objectNotToBeManged ) { mangle( objectNotToBeMangled ); // compile error : cannot convert const MyClass& to MyClass& }
Seriously I find it utterly incomprehensible that the forefathers of Java and C# didn't carry this over from C++, as it makes such a difference to the reliability and maintainability of software when you use const types properly.Last edited by VectraMan; 8 September 2012, 12:06.
Leave a comment:
-
The trouble is, with Reference Types (e.g., all custom Classes that inherit from System.Object in C#) you are always passing a reference to the object, whether or not you explicitly state that it is by reference.Originally posted by kingcook View PostSurely you mean just passing a reference to an object, and not the object itself?
Surely, Shirley?
i.e., the following code will have identical outcomes whether or not you include the highlighted ref keywords. :
Code:namespace DontChangeTheStateOfReferenceObjectsInPublicMethods { class MyClass { public int SomeIntProperty { get; set; } } static class MyStaticClass { public static void MangleMyClass(ref MyClass someClassToBeMangled) { someClassToBeMangled.SomeIntProperty = 100; //I don't care what it was when you passed this object in } } public class DoStuff { MyClass myInstanceOfMyClass = new MyClass { SomeIntProperty = 900 }; //I really want this to still be '900' later! public DoStuff() { MyStaticClass.MangleMyClass(ref myInstanceOfMyClass); /* myInstanceOfMyClass will be 100 at this point, and I wont be sure why if I don't have access to the code for MyStaticClass.MangleMyClass */ } } }
Leave a comment:
-
-
Good reading about GOTO statementsOriginally posted by hyperD View PostI find the Goto method a great lifesaver. Use it all over the place.
Code Complete, First Edition
Leave a comment:
-
I find the Goto method a great lifesaver. Use it all over the place.
Leave a comment:
-
People still want to create anemic domain models for some reason, domain logic gets littered all over the code base in random places. That goes had in hand with...
iAmGoingToTakeThisObjectsAndDoFiftyChangesToIt( aboutToBeAbusedUpTheArseObject ) ;
which is usually written as...
setState( user ) ;
Leave a comment:
-
Great suggestion....You should be a lecturer.Originally posted by RasputinDude View PostAnd don't forget to make them all static. That'll help out too.
From my experience, education in programming teaches how to write code to solve a problem, it doesn't teach how to structure it, standards and best practices. When I was a perm, i hired a number of graduates, they all completed whatever programming tasks i set them, but the code was atrocious.
I think it should be mandatory for any programming degree to teach design patterns, the standards and naming conventions for the languages they are teaching and testing, programmers i deal with in all my contracts do not have a clue about testing even thought most claim they "in-depth" experience of TDD.
Rant over
Leave a comment:
-
And don't forget to make them all static. That'll help out too.Originally posted by Ketchup View PostJust stick to primitive types and you don't have this problem
Leave a comment:
-
Just make everything global; then you can do away with silly little things like parameters.
Leave a comment:
- Home
- News & Features
- First Timers
- IR35 / S660 / BN66
- Employee Benefit Trusts
- Agency Workers Regulations
- MSC Legislation
- Limited Companies
- Dividends
- Umbrella Company
- VAT / Flat Rate VAT
- Job News & Guides
- Money News & Guides
- Guide to Contracts
- Successful Contracting
- Contracting Overseas
- Contractor Calculators
- MVL
- Contractor Expenses
Advertisers
Contractor Services
CUK News
- How to land a temporary technology job in 2026 Today 07:01
- Spring Forecast 2026 ‘won’t put up taxes on contractors’ Yesterday 07:26
- Six things coming to contractors in 2026: a year of change, caution and (maybe) opportunity Jan 7 06:24
- Umbrella companies, beware JSL tunnel vision now that the Employment Rights Act is law Jan 6 06:11
- 26 predictions for UK IT contracting in 2026 Jan 5 07:17
- How salary sacrifice pension changes will hit contractors Dec 24 07:48
- All the big IR35/employment status cases of 2025: ranked Dec 23 08:55
- Why IT contractors are (understandably) fed up with recruitment agencies Dec 22 13:57
- Contractors, don’t fall foul of HMRC’s expenses rules this Christmas party season Dec 19 09:55
- A delay to the employment status consultation isn’t why an IR35 fix looks further out of reach Dec 18 08:22

Leave a comment: