Originally posted by Spacecadet
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!
Passing objects to methods then changing the object's state
Collapse
X
-
-
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 */ } } }
Comment
-
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.Will work inside IR35. Or for food.Comment
-
Originally posted by VectraMan View PostCode: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.Comment
-
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.Will work inside IR35. Or for food.Comment
-
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.Comment
-
Originally posted by minestrone View PostWhy o why are people still doing this and who is teaching these progammers that they are allowed to do this?
Easily the fastest route to a tulipe codebase.
Does my fuggin head in.
</rantOver>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
- Streamline Your Retirement with iSIPP: A Solution for Contractor Pensions Sep 1 09:13
- Making the most of pension lump sums: overview for contractors Sep 1 08:36
- Umbrella company tribunal cases are opening up; are your wages subject to unlawful deductions, too? Aug 31 08:38
- Contractors, relabelling 'labour' as 'services' to appear 'fully contracted out' won't dupe IR35 inspectors Aug 31 08:30
- How often does HMRC check tax returns? Aug 30 08:27
- Work-life balance as an IT contractor: 5 top tips from a tech recruiter Aug 30 08:20
- Autumn Statement 2023 tipped to prioritise mental health, in a boost for UK workplaces Aug 29 08:33
- Final reminder for contractors to respond to the umbrella consultation (closing today) Aug 29 08:09
- Top 5 most in demand cyber security contract roles Aug 25 08:38
- Changes to the right to request flexible working are incoming, but how will contractors be affected? Aug 24 08:25
Comment