简体   繁体   中英

Can't get the right datatype to pass parameter by reference

I need help here, I keep receiving an error ... has some invalid arguments in this part:

this.Search(ref objResult, sSQL);  

I can't pass the collection as a reference.

internal override Models.BaseCollectionModel<Models.CategoryModel> OnFind(string objQueryArgs)
{
    Models.Collection.CategoryCollectionModel objResult = new Models.Collection.CategoryCollectionModel();
    string sSQL = string.Empty;

    sSQL = "SELECT * FROM " + this.TableName;

    this.Search(ref objResult, sSQL);            

    return objResult;
}

internal virtual void Search(ref System.Collections.IEnumerable  objResult, string sQuery)
{
    //statement goes here...
}

Just additional info CategoryCollectionModel inherits from
Models.BaseCollectionModel<Models.CategoryModel> which also inherits the System.Collections.ObjectModel.ObservableCollection<T>

Make the type for objResult IEnumerable , as per the method declaration:

System.Collections.IEnumerable objResult = 
^------+----------^
       |
       +- with the right using directives, this part is redundant

A ref parameter has to match the declaration, it can't be assignment compatible, it has to be that exact declaration.

The reason is that the method could swap out the content of that variable for a different value, of the same type, but that would not be guaranteed to be CategoryCollectionModel.

Here's a control question: Do you really need ref ?

Why do you have a ref parameter? Is it because you want a reference to the collection, and not a copy of the collection? Or do you actually intend to switch out the collection for a completely different one?

Note that a List is a reference type. This means it (the value) is passed by reference. Manipulating the values or content from inside the invoked method will also change the source object. This is the default behaviour for reference types.

Remember using ref or out the method signature must match: params have to be exactly of the same type like the passed object. This is nessesary for memory allocation. Let's say that a List and IEnumerable can be substituted for accessing a List on a IEnumerable object level but they both allocate a different amount of memory at different locations due to their difference in type. Their frames in memory are not the same so the pointer would become invalid. So show some respect to the types when using ref or coding unmanaged (using pointers).

Both lists (original and copy) are pointing to the same values (default behaviour when passing a reference type):

List<int> originalList = new List<int>();
originalList.add(1);
AddMore(originalList);

private void AddMore(List<int> list)
{
    // OriginalList will now also contain two objects (Count = 2)
    // Both lists are sharing the same reference and therefore are pointing
    // to the same memory location
    list.Add(2);

    // Create a new reference for variable of type List<int>. 
    // This will override list but not originalList 
    // originalList.Count is still 2! 
    list = new List<int>();

    // originalList still has two objects. Not three!
    // The local variable is now pointing to a new/ different memomory location
    // than originalList is pointing to
    list.Add(3)
}

If the desired behaviour is that originalList variable should also point to a new reference (the same!) than ref will do the job. list and originalList are then equal objects ( same references ):

private void AddMore(ref List<int> list)
{
    // OriginalList will now also contain two objects (Count = 2)
    // Both (original and copy) are pointing to the same values since the variables 
    // are now equal
    list.Add(2);

    // This creates a new reference for BOTH variables!
    // originalList.Count is now 0
    list = new List<int>();

    // Both list's count is now 1!
    list.Add(3);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM