Answered by:
CreateObject("cast.cipher") equivalent function in c#

Question
-
Dim enCrypt As Variant
Set enCrypt = CreateObject("cast.cipher")strEncryptPWD = enCrypt.encode(strUName, strUPwd)
Thanks for help.
Saturday, December 24, 2011 9:39 AM
Answers
-
Hello Siddheswaran,In VB, CreateObject() creates as object using progId. Here is how you do it in C#,
Type t = Type.GetTypeFromProgID("cast.cipher"); var enCrypt = Activator.CreateInstance(t);
If you are using .NET 4.0, you can make use of dynamic. So above code can be written as,
Type t = Type.GetTypeFromProgID("cast.cipher"); dynamic enCrypt = Activator.CreateInstance(t); enCrypt.encode(strUName, strUPwd);
But if you are not using .NET 4.0, then you can use previous method but you have to call encode method using reflection.
Please mark this post as answer if it solved your problem. Happy Programming!- Proposed as answer by Louis.fr Wednesday, December 28, 2011 7:53 AM
- Marked as answer by Martin_Xie Friday, January 6, 2012 11:37 AM
Saturday, December 24, 2011 3:25 PM -
Ofcourse that will give you error (if you are not using enCrypt variable is of dynamic type). Because, when you use CreateInstance to create an object, the type of the oject will be System.Object. So, enCrypt is of System.Object type which doesn't define encode method.To get this work doe, you need to use reflection. Let me re-write the whole code,
Type t = Type.GetTypeFromProgID("cast.cipher"); var enCrypt = Activator.CreateInstance(t); t.GetMethod("encode").Invoke(enCrypt, new String[] { strUName, strUPwd }); //Call encode method this way.
Note: This is not tested because I don't have "cast.cipher" progId registered in my system. Also I assume that "cast.cipher" progId is availablein your system.
Please mark this post as answer if it solved your problem. Happy Programming!- Marked as answer by Martin_Xie Friday, January 6, 2012 11:37 AM
Thursday, January 5, 2012 7:17 AM
All replies
-
Hi Jamshid Welcome to MSDN Forums.. object enCrypt; Variant Set enCrypt = CreateObject("cast.cipher"); strEncryptPWD = enCrypt.encode(strUName, strUPwd); By A Pathfinder.. JoSwa
If you find an answer helpful, click the helpful button. If you find an answer to your question, mark it as the answer.- Edited by Jo Swa(K.P.Elayaraja)-MCP Saturday, December 24, 2011 11:27 AM
- Proposed as answer by VikkyPatel Saturday, December 24, 2011 11:57 AM
Saturday, December 24, 2011 11:26 AM -
Hai Joswa,
Thanks for answering. I am new to c#.net, kindly guide me to add any namespace or reference for below error.
Variant Set enCrypt = CreateObject("cast.cipher"); ==> It show error like"The type of namespace name 'Variant' could not be found (are you missing a using directive or an assembly reference?)"
With Regards,
Siddheswaran.P
siddheswaran.pSaturday, December 24, 2011 1:22 PM -
Hi siddhesswaran
I have replied only for as you posted..
Also I dont know whether you using windows application or web application..
send full code..
By
A Pathfinder..
JoSwa
If you find an answer helpful, click the helpful button. If you find an answer to your question, mark it as the answer.Saturday, December 24, 2011 2:43 PM -
Hello Siddheswaran,In VB, CreateObject() creates as object using progId. Here is how you do it in C#,
Type t = Type.GetTypeFromProgID("cast.cipher"); var enCrypt = Activator.CreateInstance(t);
If you are using .NET 4.0, you can make use of dynamic. So above code can be written as,
Type t = Type.GetTypeFromProgID("cast.cipher"); dynamic enCrypt = Activator.CreateInstance(t); enCrypt.encode(strUName, strUPwd);
But if you are not using .NET 4.0, then you can use previous method but you have to call encode method using reflection.
Please mark this post as answer if it solved your problem. Happy Programming!- Proposed as answer by Louis.fr Wednesday, December 28, 2011 7:53 AM
- Marked as answer by Martin_Xie Friday, January 6, 2012 11:37 AM
Saturday, December 24, 2011 3:25 PM -
Hello Siddheswaran,
Any updateson this?
Please mark this post as answer if it solved your problem. Happy Programming!Tuesday, January 3, 2012 8:45 AM -
Hai Adavesh,
This statement showing error.
enCrypt.encode(strUName, strUPwd);
siddheswaran.pWednesday, January 4, 2012 3:30 PM -
Ofcourse that will give you error (if you are not using enCrypt variable is of dynamic type). Because, when you use CreateInstance to create an object, the type of the oject will be System.Object. So, enCrypt is of System.Object type which doesn't define encode method.To get this work doe, you need to use reflection. Let me re-write the whole code,
Type t = Type.GetTypeFromProgID("cast.cipher"); var enCrypt = Activator.CreateInstance(t); t.GetMethod("encode").Invoke(enCrypt, new String[] { strUName, strUPwd }); //Call encode method this way.
Note: This is not tested because I don't have "cast.cipher" progId registered in my system. Also I assume that "cast.cipher" progId is availablein your system.
Please mark this post as answer if it solved your problem. Happy Programming!- Marked as answer by Martin_Xie Friday, January 6, 2012 11:37 AM
Thursday, January 5, 2012 7:17 AM -
Thanks Adavesh and JoSwa for your fast help and support.
Hi siddheswaran,
Welcome to MSDN Forum.
We are very glad to hear that you got some good suggestions.
Adavesh is right. The C# equivalent of VB CreateObject is something like this:
dynamic objExcel = Activator.CreateInstance(Type.GetTypeFromProgID("Excel.Application"));
This case discussed the similar issue to yours.
If you have any difficulty in future programming, you are welcome to post here again.
Martin Xie [MSFT]
MSDN Community Support | Feedback to us
Friday, January 6, 2012 11:43 AM