Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Cannot convert from ref Class to ref Object

Posted on 2006-06-29
18
Medium Priority
?
1,367 Views
Last Modified: 2012-05-05
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


0
Comment
Question by:Tpvv
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
  • +2
18 Comments
 
LVL 23

Expert Comment

by:apresto
ID: 17008149
Can you not change this:

public static int gfncintProcessTemplateLoop(ref ClassCompany clsRecordset)

to this:

public static int gfncintProcessTemplateLoop(ref object clsRecordset)

its barfing because of the combination of ref and object (i think )

will the method expect different types of objects?
0
 

Author Comment

by:Tpvv
ID: 17008218
Hello,

Thank you very much for your comment.
No I cannot change
public static int gfncintProcessTemplateLoop(ref object clsRecordset)

to

public static int gfncintProcessTemplateLoop(ref ClassCompany clsRecordset)

as the same function is used by other objects as well to do various things.

Regards
TV
0
 
LVL 23

Expert Comment

by:apresto
ID: 17008227
ok can you change this:

ClassCompany clsCompany = new ClassCompany();

to this then:

object clsCompany = new ClassCompany();
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:Tpvv
ID: 17008258
If I do that I cannot acces any of the Properties or methods inside ClassCompany.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17008259
Is your static method designed to work with value types ? If not, then you don't need "ref" at all because classes are reference types and get passed by reference already.
0
 

Author Comment

by:Tpvv
ID: 17008309
The static method accepts both value types and reference types as parameters and works with both. If I remove the ref, then the error goes away. However whether it will provide me with the same result I am not sure.

The properties and methods within the passed object is called multiple times in gfncintProcessTemplateLoop to do various things.

I am not able to test it as I am still in the process of debugging the code and it will take a while before I will be able to do some test on it.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17008416
You said you can't use:

    object clsCompany = new ClassCompany();

Because you won't be able to access the type-specific methods and properties. Are you intending to access its type-specific methods from inside gfncingProcessTemplateLoop() ?
0
 

Author Comment

by:Tpvv
ID: 17008499
Yes gfncingProcessTemplateLoop() also accesses type specific methods of the object that was passed. The code does a casting into the specific type to access the members.
This is my next problem as even though the casting is done, none of the properties and methods can be accessed from the passed object in gfncingProcessTemplateLoop()
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 17008687
Would this work:

intCount = ModUtils.gfncintProcessTemplateLoop(ref clsCompany.ToObject())

Bob
0
 

Author Comment

by:Tpvv
ID: 17008711
No I tired that. It does not work

Regards
MTV
0
 
LVL 7

Expert Comment

by:mjmarlow
ID: 17008906
try:

object refobject = clsCompany as Object();
intCount = ModUtils.gfncintProcessTemplateLoop(ref refobject);

0
 
LVL 7

Expert Comment

by:mjmarlow
ID: 17008928
change:
object refobject = clsCompany as Object;
intCount = ModUtils.gfncintProcessTemplateLoop(ref refobject);
0
 

Author Comment

by:Tpvv
ID: 17008987
Yes it works.
Great!!! Thanks for that.

There is one more issue that I have related to the same static method.
After I pass clsCompany as an object to gfncintProcessTemplateLoop, I am casting it back to its original type depending on the type. Then I am accessing the properties and methods within the object.

However this throws an error in spite of casting it back to its original type.

clsRecordset = ((ClassCompany) clsRecordset);
if (clsRecordset.RecordCount != 0)

This gives an error saying that Object does not contain a definition for RecordCount.

Any idea what should be done here?

Thank you
MTV
0
 
LVL 7

Expert Comment

by:mjmarlow
ID: 17009107
change:
clsRecordset = ((ClassCompany) clsRecordset);
to:
ClassCompany myClassCompany = clsRecordset as ClassCompany;
if( myClassCompany.RecordCount!=0)....


0
 
LVL 7

Accepted Solution

by:
mjmarlow earned 1000 total points
ID: 17009140
BTW,
When you use the "as" operator, it will return null if the object can not be cast into the type.  So safer usage is:

ClassCompany myClassCompany = clsRecordSet as ClassCompany;

if(myClassCompany!=null) // is clsRecordSet really a "ClassCompany"?
{


}
0
 

Author Comment

by:Tpvv
ID: 17009148
That is great!!

Thank you. It works.

Regards
MTV
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question