C# implicitly convertion??

E

Eranga

My Error is " Cannot implicitly convert type 'object'
to'CIODMLib.CatAdmClass' "
The code segment is as follows;
public class Indexing
{
protected CIODMLib.AdminIndexServerClass admNew;
protected CIODMLib.CatAdmClass catNew;

public Indexing()
{
admNew = new CIODMLib.AdminIndexServerClass();
catNew = new CIODMLib.CatAdmClass();
}
public object InitializeCatalog(string strName, string strLoc)
{
admNew.AddCatalog(strName, strLoc);
return admNew.GetCatalog();
}
public void AddNewScope(string strName)
{
catNew = admNew.GetCatalogByName(strName);
}
}
public class IndexingAp
{
public static void Main(String[] args)
{
Indexing newAdm = new Indexing();
Indexing newCat = new Indexing();

newAdm.InitializeCatalog("myCat","C:\\myCatalogFolder");
newCat.AddNewScope("myCat");
}
}

I even tried
"catNew = (CIODMLib.CatAdmClass)admNew.GetCatalogByName(strName);"
Then error "'System.InvalidCastException' occurred Specified cast is not
valid." occurs
Can some one please help me???
 
D

Daniel O'Connell [C# MVP]

Eranga said:
My Error is " Cannot implicitly convert type 'object'
to'CIODMLib.CatAdmClass' "
The code segment is as follows;
public class Indexing
{
protected CIODMLib.AdminIndexServerClass admNew;
protected CIODMLib.CatAdmClass catNew;

public Indexing()
{
admNew = new CIODMLib.AdminIndexServerClass();
catNew = new CIODMLib.CatAdmClass();
}
public object InitializeCatalog(string strName, string strLoc)
{
admNew.AddCatalog(strName, strLoc);
return admNew.GetCatalog();
}
public void AddNewScope(string strName)
{
catNew = admNew.GetCatalogByName(strName);
}
}
public class IndexingAp
{
public static void Main(String[] args)
{
Indexing newAdm = new Indexing();
Indexing newCat = new Indexing();

newAdm.InitializeCatalog("myCat","C:\\myCatalogFolder");
newCat.AddNewScope("myCat");
}
}

I even tried
"catNew = (CIODMLib.CatAdmClass)admNew.GetCatalogByName(strName);"
Then error "'System.InvalidCastException' occurred Specified cast is not
valid." occurs

Are you sure the value returned by GetCatalogByname *IS* of type
CIODMLib.CatAdmClass? I would guess it is not.
 
S

Sean Hederman

Eranga said:
I even tried
"catNew = (CIODMLib.CatAdmClass)admNew.GetCatalogByName(strName);"
Then error "'System.InvalidCastException' occurred Specified cast is not
valid." occurs
Can some one please help me???

GetCatalogByName is not returning a type CasAdmClass, try the following
code:
string name = admNew.GetCatalogByName(strName).GetType().FullName;

This will let you know what type GetCatalogByName is returning.
 
E

Eranga

Sean,
Thanks
I did that and got the type as System.__ComObject.
But on msdn it says;
"The CatAdm catalog administration object manages a catalog definition
for Indexing Service. The catalog defines the set of documents to index
by specifying the list of directories that are used by Indexing Service
to determine the files to index. Catalog objects can be created only by
means of the AddCatalog method on the AdminIndexServer object and can be
retrieved in the following ways.

Through the numeration methods FindFirstCatalog and FindNextCatalog on
the AdminIndexServer class; or
GetCatalogByName on the AdminIndexServer class.
"
So is it because that I have an error in my code that I cant reriev
catalog object by GetCatalogByName method?
Please help me?
 
E

Eranga

Danniel Thanks for your reply.
Yes it is of type System.__ComObject
How can I assign to the catAdm object here?
 
G

Guest

Use Marshal.CreateWrapperOfType to convert the generic System.__ComObject
wrapper to a specific RCW in your interop assembly.
 
E

Eranga

Thanks Daniel.
It worked.
But the new problem is the created on catalog class object I can't call
the method add catalog.
The error returns as "System.ArgumentException".incorrect parameter.

Type TypeHere = typeof(CIODMLib.CatAdmClass);
object ObjectHere = admNew.GetCatalogByName(strName);
object Wrapper = Marshal.CreateWrapperOfType(ObjectHere, TypeHere);
catNew = (CIODMLib.CatAdmClass)Wrapper;

catNew.AddScope(strScope,fExclude,vLogOn,vPassword); // Error here

my parameter values are correct . What is the problem here. Pleas help
me.
 
Top