StrataFrame Forum

Passing a Business Object as a parameter and want to change the Calling Business Object

http://forum.strataframe.net/Topic29993.aspx

By Ger Cannoll - 5/17/2011

I am trying to pass a Business Object as a parameter to a routine, and adjust details on the calling Buisness Object

So a simplified version of my code is:    

BoSetDefaults(ref myBO)


My method is:
public static void BoSetDefaults(ref BusinessLayer myBO)
{   myBO["STATE"]= "NY";} 
  

The compliler is objecting that myBO is readonly.

Is there a way to amend the Calling Business Object in the Called Method. If I take out the Call By reference, just the copy that is called gets updated, but I want it to percolate back to the Calling Business Object
By Greg McGuffey - 5/19/2011

I'm not sure what is going on with the compiler error, but you don't really need the ref modifier for the parameter.

BOs are reference types and thus what you are passing is a reference to the object anyway. You'd typically only use ref if you needed to change the object the calling variable was pointing to. If you just need to manipulate the object itself, no ref needed.

BoSetDefaults(myBO)

//My method is:
public static void BoSetDefaults(BusinessLayer myBO)
{   myBO["STATE"]= "NY";} 


This should work fine.  You might have some other issue going on with the readonly issue.

Here's a good article on variable types and parameter types: 

http://www.yoda.arachsys.com/csharp/parameters.html
By Ger Cannoll - 5/20/2011

Hi Greg.  Finally got to the bottom of this

Aside from the confusion with the Ref and Value parameters, I moved the method back to running without the ref. This was not working , so I though it had something to do with the way the parameters were being passed,  but turns out that that was a red herring.

There were two reasons why the fields were not being updated onto the BO field:

1. I had copied the default table over from a VFP table (Into Sql Server). Say I had a 10 character field, IN VFP it was stored as 10 characters with trailing spaces. So for instance NY would be stored as NY and 8 trailing spaces. Now any lookups in VFP did not seem to have any issue with this (even with Set Exact ON) but in C#, this is causing some problems. When I did a trim, it works fine

2. Some of my default values ended up in a Combo Box , whose source is a select out from another table as a Lookup. Again, if the default value I insert into the Lookup field does not exist in the Lookup Source table, it just is not populated. When i ensured that the default field matched exactly at least one of the fields on the lookup, it works fine.

Many thanks again for your very detailed and thorough assistance
By Greg McGuffey - 5/20/2011

Glad you finally got to the bottom of this!