locked
Cannot Convert ref Class to ref Object RRS feed

  • Question

  • Hello,

    I have convert a VB application to C#. I am trying to debug it. I have got one error where I am calling a function proving it an instance of a class to an object as a ref.

    ClassCompany clsCompany = new ClassCompany();
    intCount = ModUtils.gfncintProcessTemplateLoop(ref clsCompany)


    public static int gfncintProcessTemplateLoop(ref object clsRecordset)

    I get the following error.

    cannot convert from 'ref SiteGen.ClassCompany' to 'ref object'

    gfncintProcessTemplateLoop is a generic function and is used to pass different classes and is the reason why the class is delcares as an object.

    I also tried casting clsCompany into an Object.
    intCount = ModUtils.gfncintProcessTemplateLoop(ref (object)clsCompany)
    Then I got another error saying
    A ref or out argument must be an lvalue

    Can anybody help me with this one please.

    Thank you
    TV
    Thursday, June 29, 2006 10:28 AM

Answers

  • clsRecordset is defined in the method as an object. You cast it to ClassCompany, but put the (cast) value back into that same variable of type object. The next thing you're doing is asking for the property 'RecordCount'. But you are asking an object (who just so happens to contain a ClassCompany, but C# doesn't know that!) for a property. You are actually trying to read object.RecordCount. That won't work well ;)

    The solution: define a variable of the type ClassCompany to hold the cast object, and ask that one about the RecordCount.

    ClassCompany classCompany;

    classCompany = clsRecordset as ClassCompany;

    if (classCompany != null)
    {
        // put code here
    }

    Thursday, June 29, 2006 12:07 PM

All replies

  • Do you really need ref parameter in the gfncintProcessTemplateLoop function? ref parameter is required only if function needs to remove (set to null) or create new instance of object passed to function. To access object methods and change something in the object, you don't need ref.
    Thursday, June 29, 2006 11:45 AM
  • Hello,

    Thank you very much for the response. The code was converted by a tool from VB to C#. I guess you are right. All I am doing is accessing the properties and methods within the object. However I still have one problem. In gfncintProcessTemplateLoop, I am casting the passed object to the correct type. In spite of that I am not able to access any properties or methods from the passed object.

    clsRecordset = (ClassCompany)clsRecordset;

    if (clsRecordset.RecordCount != 0)

    It says Object does not have a definition for RecordCount.

    Any Ideas how I solve this?

    Thank you

    MTV

     

    Thursday, June 29, 2006 11:54 AM
  • clsRecordset is defined in the method as an object. You cast it to ClassCompany, but put the (cast) value back into that same variable of type object. The next thing you're doing is asking for the property 'RecordCount'. But you are asking an object (who just so happens to contain a ClassCompany, but C# doesn't know that!) for a property. You are actually trying to read object.RecordCount. That won't work well ;)

    The solution: define a variable of the type ClassCompany to hold the cast object, and ask that one about the RecordCount.

    ClassCompany classCompany;

    classCompany = clsRecordset as ClassCompany;

    if (classCompany != null)
    {
        // put code here
    }

    Thursday, June 29, 2006 12:07 PM
  • I think this will solve my problem.

    Thank you very much for all your help.

    MTV

    Thursday, June 29, 2006 12:22 PM