COM Object declarations in C#

  • Thread starter Thread starter nique
  • Start date Start date
N

nique

Hello,

I am trying to convert a simple application I have
written, first in vb6 then to vb.net and now I would like
to convert it to c#.

The part that I am having problems with in c# is
referencing the objects.

I keep getting the error "Can not implicitly
convert 'object' to(whatever object i'm trying to set it
at)"

I have added the com reference in my c# project and when I
am declaring objects, seems to be 'okay' before runtime.

The part of the vb.net code I would like to convert to c#
is:

Public Class Form1

Dim objApplication As COMMNAME.Application
Dim objCurrentScreen As COMNAME.COMNAMEScreen
Dim objSession As COMNAME.COMNAMESESSION

Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click

objApplication = CreateObject("COMMNAME.Application")
objSession = objApplication.ACTIVESESSION
objCurrentScreen = objSession.Screen

End sub

End Class

This part of the code was written originally in vb6, and
works fine in vb.net.

What is the equivalent version of this code in c#(without
the button1_click part)?

I have tried the following code:

COMMNAME.Application objApplication;
COMNAME.COMNAMESESSION objSession;
COMNAME.COMNAMEScreen objCurrentScreen =

objApplication = COMMNAME.Application;
objSession = objApplication.COMNAMESESSION;
objCurrentScreen = objSession.SCREEN;

And I get that error:"Can not implicitly convert 'object'
to'.

Anyone's help will be greatly appreciated.

Thanks!
 
nique,

The code:

COMNAME.COMNAMEScreen objCurrentScreen =

Is not going to compile, and the rest of your code should not compile as
a result.

In order to get the same object in C#, you can do it in one of two ways.
The first would be to call the CreateObject static method on the Interaction
class in the Microsoft.VisualBasic namespace. This can be found in
Microsoft.VisualBasic.dll.

The other way would be to create a type definition from a program id,
using the static GetTypeFromProgId method on the Type class. Once you do
that, you can pass that Type instance to the static CreateInstance method on
the Activator class, and then cast the return type to your interface
definition (COMMNAME.Application).

Hope this helps.
 
thanks nicholas for your response!

I made a mistake on that line you have mentioned, I meant
to put the ";" instead of the "=".

:)

I'm still unclear of what to do to achieve what i can do
in vb.net/vb6 in c# despite of what you have suggested.

I thought that all you had to do to be able to use the
objects properties and methods was to add it in the
reference.

Which I do in vb.net, and seems like is similar process in
c#(which i did).

Please humor me and let me try to retype the code again
(coz i'm probably,well I know, i was not clear).
========================================================
For declarations:

In vb.net I typed:

Dim objAPPLICATION As APPLICATION.SYSTEMObject
Dim objSession As APPLICATION.SESSIONObject
Dim objSCREEN As APPLICATION.SCREENObject

In c# I typed:

APPLICATION.SYSTEMObject objAPPLICATION;
APPLICATION.SESSIONObject objSession;
APPLICATION.SCREENObject objSCREEN;
=======================================================

For setting:

In vb.net I typed:

objAPPLICATION = CreateObject("APPLICATION.SYSTEMObject")
objSession = objAPPLICATION.ActiveSession
objSCREEN = objSession.Screen

In c# I typed:

objAPPLICATION = APPLICATION.SYSTEMObject;
objSession = APPLICATION.SESSIONObject;
objSCREEN = APPLICATION.SCREENObject;
=========================================================

In vb.net I go on and code the rest using the objects.

In c# i get a "can not implicitly" error.

What am i doing wrong in c#?

I'm guessing that "dim"'ing an object and "set"'ing it is
not as simple in c# as in vb.net.

What help references, books, websites do you think I
should read upon to understand more fully of what you had
suggested to do and to be able to achieve what is
seemingly more simple in vb.net to c#?

thanks so much for your help!
-----Original Message-----
nique,

The code:

COMNAME.COMNAMEScreen objCurrentScreen =

Is not going to compile, and the rest of your code should not compile as
a result.

In order to get the same object in C#, you can do it in one of two ways.
The first would be to call the CreateObject static method on the Interaction
class in the Microsoft.VisualBasic namespace. This can be found in
Microsoft.VisualBasic.dll.

The other way would be to create a type definition from a program id,
using the static GetTypeFromProgId method on the Type class. Once you do
that, you can pass that Type instance to the static CreateInstance method on
the Activator class, and then cast the return type to your interface
definition (COMMNAME.Application).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


nique said:
Hello,

I am trying to convert a simple application I have
written, first in vb6 then to vb.net and now I would like
to convert it to c#.

The part that I am having problems with in c# is
referencing the objects.

I keep getting the error "Can not implicitly
convert 'object' to(whatever object i'm trying to set it
at)"

I have added the com reference in my c# project and when I
am declaring objects, seems to be 'okay' before runtime.

The part of the vb.net code I would like to convert to c#
is:

Public Class Form1

Dim objApplication As COMMNAME.Application
Dim objCurrentScreen As COMNAME.COMNAMEScreen
Dim objSession As COMNAME.COMNAMESESSION

Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click

objApplication = CreateObject("COMMNAME.Application")
objSession = objApplication.ACTIVESESSION
objCurrentScreen = objSession.Screen

End sub

End Class

This part of the code was written originally in vb6, and
works fine in vb.net.

What is the equivalent version of this code in c# (without
the button1_click part)?

I have tried the following code:

COMMNAME.Application objApplication;
COMNAME.COMNAMESESSION objSession;
COMNAME.COMNAMEScreen objCurrentScreen =

objApplication = COMMNAME.Application;
objSession = objApplication.COMNAMESESSION;
objCurrentScreen = objSession.SCREEN;

And I get that error:"Can not implicitly convert 'object'
to'.

Anyone's help will be greatly appreciated.

Thanks!


.
 
Man why does reading you're code feel like COBOL?

--

Jack Mayhoff
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

thanks nicholas for your response!

I made a mistake on that line you have mentioned, I meant
to put the ";" instead of the "=".

:)

I'm still unclear of what to do to achieve what i can do
in vb.net/vb6 in c# despite of what you have suggested.

I thought that all you had to do to be able to use the
objects properties and methods was to add it in the
reference.

Which I do in vb.net, and seems like is similar process in
c#(which i did).

Please humor me and let me try to retype the code again
(coz i'm probably,well I know, i was not clear).
========================================================
For declarations:

In vb.net I typed:

Dim objAPPLICATION As APPLICATION.SYSTEMObject
Dim objSession As APPLICATION.SESSIONObject
Dim objSCREEN As APPLICATION.SCREENObject

In c# I typed:

APPLICATION.SYSTEMObject objAPPLICATION;
APPLICATION.SESSIONObject objSession;
APPLICATION.SCREENObject objSCREEN;
=======================================================

For setting:

In vb.net I typed:

objAPPLICATION = CreateObject("APPLICATION.SYSTEMObject")
objSession = objAPPLICATION.ActiveSession
objSCREEN = objSession.Screen

In c# I typed:

objAPPLICATION = APPLICATION.SYSTEMObject;
objSession = APPLICATION.SESSIONObject;
objSCREEN = APPLICATION.SCREENObject;
=========================================================

In vb.net I go on and code the rest using the objects.

In c# i get a "can not implicitly" error.

What am i doing wrong in c#?

I'm guessing that "dim"'ing an object and "set"'ing it is
not as simple in c# as in vb.net.

What help references, books, websites do you think I
should read upon to understand more fully of what you had
suggested to do and to be able to achieve what is
seemingly more simple in vb.net to c#?

thanks so much for your help!
-----Original Message-----
nique,

The code:

COMNAME.COMNAMEScreen objCurrentScreen =

Is not going to compile, and the rest of your code should not compile as
a result.

In order to get the same object in C#, you can do it in one of two ways.
The first would be to call the CreateObject static method on the Interaction
class in the Microsoft.VisualBasic namespace. This can be found in
Microsoft.VisualBasic.dll.

The other way would be to create a type definition from a program id,
using the static GetTypeFromProgId method on the Type class. Once you do
that, you can pass that Type instance to the static CreateInstance method on
the Activator class, and then cast the return type to your interface
definition (COMMNAME.Application).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


nique said:
Hello,

I am trying to convert a simple application I have
written, first in vb6 then to vb.net and now I would like
to convert it to c#.

The part that I am having problems with in c# is
referencing the objects.

I keep getting the error "Can not implicitly
convert 'object' to(whatever object i'm trying to set it
at)"

I have added the com reference in my c# project and when I
am declaring objects, seems to be 'okay' before runtime.

The part of the vb.net code I would like to convert to c#
is:

Public Class Form1

Dim objApplication As COMMNAME.Application
Dim objCurrentScreen As COMNAME.COMNAMEScreen
Dim objSession As COMNAME.COMNAMESESSION

Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click

objApplication = CreateObject("COMMNAME.Application")
objSession = objApplication.ACTIVESESSION
objCurrentScreen = objSession.Screen

End sub

End Class

This part of the code was written originally in vb6, and
works fine in vb.net.

What is the equivalent version of this code in c# (without
the button1_click part)?

I have tried the following code:

COMMNAME.Application objApplication;
COMNAME.COMNAMESESSION objSession;
COMNAME.COMNAMEScreen objCurrentScreen =

objApplication = COMMNAME.Application;
objSession = objApplication.COMNAMESESSION;
objCurrentScreen = objSession.SCREEN;

And I get that error:"Can not implicitly convert 'object'
to'.

Anyone's help will be greatly appreciated.

Thanks!


.
 
You need to create a new instance of your object. If you have a
reference to the COM library, then you should be able to do this:

objApplication = new COMMNAME.ApplicationClass();
objSession = objApplication.COMNAMESESSION;
objCurrentScreen = objSession.SCREEN;

Assuming that the application is where you can get access to the other
instances.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

thanks nicholas for your response!

I made a mistake on that line you have mentioned, I meant
to put the ";" instead of the "=".

:)

I'm still unclear of what to do to achieve what i can do
in vb.net/vb6 in c# despite of what you have suggested.

I thought that all you had to do to be able to use the
objects properties and methods was to add it in the
reference.

Which I do in vb.net, and seems like is similar process in
c#(which i did).

Please humor me and let me try to retype the code again
(coz i'm probably,well I know, i was not clear).
========================================================
For declarations:

In vb.net I typed:

Dim objAPPLICATION As APPLICATION.SYSTEMObject
Dim objSession As APPLICATION.SESSIONObject
Dim objSCREEN As APPLICATION.SCREENObject

In c# I typed:

APPLICATION.SYSTEMObject objAPPLICATION;
APPLICATION.SESSIONObject objSession;
APPLICATION.SCREENObject objSCREEN;
=======================================================

For setting:

In vb.net I typed:

objAPPLICATION = CreateObject("APPLICATION.SYSTEMObject")
objSession = objAPPLICATION.ActiveSession
objSCREEN = objSession.Screen

In c# I typed:

objAPPLICATION = APPLICATION.SYSTEMObject;
objSession = APPLICATION.SESSIONObject;
objSCREEN = APPLICATION.SCREENObject;
=========================================================

In vb.net I go on and code the rest using the objects.

In c# i get a "can not implicitly" error.

What am i doing wrong in c#?

I'm guessing that "dim"'ing an object and "set"'ing it is
not as simple in c# as in vb.net.

What help references, books, websites do you think I
should read upon to understand more fully of what you had
suggested to do and to be able to achieve what is
seemingly more simple in vb.net to c#?

thanks so much for your help!
-----Original Message-----
nique,

The code:

COMNAME.COMNAMEScreen objCurrentScreen =

Is not going to compile, and the rest of your code should not compile as
a result.

In order to get the same object in C#, you can do it in one of two ways.
The first would be to call the CreateObject static method on the Interaction
class in the Microsoft.VisualBasic namespace. This can be found in
Microsoft.VisualBasic.dll.

The other way would be to create a type definition from a program id,
using the static GetTypeFromProgId method on the Type class. Once you do
that, you can pass that Type instance to the static CreateInstance method on
the Activator class, and then cast the return type to your interface
definition (COMMNAME.Application).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


nique said:
Hello,

I am trying to convert a simple application I have
written, first in vb6 then to vb.net and now I would like
to convert it to c#.

The part that I am having problems with in c# is
referencing the objects.

I keep getting the error "Can not implicitly
convert 'object' to(whatever object i'm trying to set it
at)"

I have added the com reference in my c# project and when I
am declaring objects, seems to be 'okay' before runtime.

The part of the vb.net code I would like to convert to c#
is:

Public Class Form1

Dim objApplication As COMMNAME.Application
Dim objCurrentScreen As COMNAME.COMNAMEScreen
Dim objSession As COMNAME.COMNAMESESSION

Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click

objApplication = CreateObject("COMMNAME.Application")
objSession = objApplication.ACTIVESESSION
objCurrentScreen = objSession.Screen

End sub

End Class

This part of the code was written originally in vb6, and
works fine in vb.net.

What is the equivalent version of this code in c# (without
the button1_click part)?

I have tried the following code:

COMMNAME.Application objApplication;
COMNAME.COMNAMESESSION objSession;
COMNAME.COMNAMEScreen objCurrentScreen =

objApplication = COMMNAME.Application;
objSession = objApplication.COMNAMESESSION;
objCurrentScreen = objSession.SCREEN;

And I get that error:"Can not implicitly convert 'object'
to'.

Anyone's help will be greatly appreciated.

Thanks!


.
 
jack,

i broke down what i wanted to be clear on by seperating
the comparison between vb.net and c# for nicholas.

if you can't be helpful, why comment?

-----Original Message-----
Man why does reading you're code feel like COBOL?

--

Jack Mayhoff
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

thanks nicholas for your response!

I made a mistake on that line you have mentioned, I meant
to put the ";" instead of the "=".

:)

I'm still unclear of what to do to achieve what i can do
in vb.net/vb6 in c# despite of what you have suggested.

I thought that all you had to do to be able to use the
objects properties and methods was to add it in the
reference.

Which I do in vb.net, and seems like is similar process in
c#(which i did).

Please humor me and let me try to retype the code again
(coz i'm probably,well I know, i was not clear).
========================================================
For declarations:

In vb.net I typed:

Dim objAPPLICATION As APPLICATION.SYSTEMObject
Dim objSession As APPLICATION.SESSIONObject
Dim objSCREEN As APPLICATION.SCREENObject

In c# I typed:

APPLICATION.SYSTEMObject objAPPLICATION;
APPLICATION.SESSIONObject objSession;
APPLICATION.SCREENObject objSCREEN;
=======================================================

For setting:

In vb.net I typed:

objAPPLICATION = CreateObject ("APPLICATION.SYSTEMObject")
objSession = objAPPLICATION.ActiveSession
objSCREEN = objSession.Screen

In c# I typed:

objAPPLICATION = APPLICATION.SYSTEMObject;
objSession = APPLICATION.SESSIONObject;
objSCREEN = APPLICATION.SCREENObject;
=========================================================

In vb.net I go on and code the rest using the objects.

In c# i get a "can not implicitly" error.

What am i doing wrong in c#?

I'm guessing that "dim"'ing an object and "set"'ing it is
not as simple in c# as in vb.net.

What help references, books, websites do you think I
should read upon to understand more fully of what you had
suggested to do and to be able to achieve what is
seemingly more simple in vb.net to c#?

thanks so much for your help!
-----Original Message-----
nique,

The code:

COMNAME.COMNAMEScreen objCurrentScreen =

Is not going to compile, and the rest of your code should not compile as
a result.

In order to get the same object in C#, you can do
it
in one of two ways.
The first would be to call the CreateObject static
method
on the Interaction
class in the Microsoft.VisualBasic namespace. This can be found in
Microsoft.VisualBasic.dll.

The other way would be to create a type definition from a program id,
using the static GetTypeFromProgId method on the Type class. Once you do
that, you can pass that Type instance to the static CreateInstance method on
the Activator class, and then cast the return type to your interface
definition (COMMNAME.Application).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hello,

I am trying to convert a simple application I have
written, first in vb6 then to vb.net and now I would like
to convert it to c#.

The part that I am having problems with in c# is
referencing the objects.

I keep getting the error "Can not implicitly
convert 'object' to(whatever object i'm trying to set it
at)"

I have added the com reference in my c# project and when I
am declaring objects, seems to be 'okay' before runtime.

The part of the vb.net code I would like to convert
to
c#
is:

Public Class Form1

Dim objApplication As COMMNAME.Application
Dim objCurrentScreen As COMNAME.COMNAMEScreen
Dim objSession As COMNAME.COMNAMESESSION

Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click

objApplication = CreateObject("COMMNAME.Application")
objSession = objApplication.ACTIVESESSION
objCurrentScreen = objSession.Screen

End sub

End Class

This part of the code was written originally in vb6, and
works fine in vb.net.

What is the equivalent version of this code in c# (without
the button1_click part)?

I have tried the following code:

COMMNAME.Application objApplication;
COMNAME.COMNAMESESSION objSession;
COMNAME.COMNAMEScreen objCurrentScreen =

objApplication = COMMNAME.Application;
objSession = objApplication.COMNAMESESSION;
objCurrentScreen = objSession.SCREEN;

And I get that error:"Can not implicitly convert 'object'
to'.

Anyone's help will be greatly appreciated.

Thanks!




.


.
 
hi nicholas,

i just tried what you suggested in your last reply, and i
still get the errors, like:

"C:\PROJECTFOLDER\PROJECT_TEST_APP\Form.cs(173): Cannot
implicitly convert type 'object' to
COMMNAME.COMNAMESESSION"

thanks for taking the time to help me out.
-----Original Message-----
You need to create a new instance of your object. If you have a
reference to the COM library, then you should be able to do this:

objApplication = new COMMNAME.ApplicationClass();
objSession = objApplication.COMNAMESESSION;
objCurrentScreen = objSession.SCREEN;

Assuming that the application is where you can get access to the other
instances.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

thanks nicholas for your response!

I made a mistake on that line you have mentioned, I meant
to put the ";" instead of the "=".

:)

I'm still unclear of what to do to achieve what i can do
in vb.net/vb6 in c# despite of what you have suggested.

I thought that all you had to do to be able to use the
objects properties and methods was to add it in the
reference.

Which I do in vb.net, and seems like is similar process in
c#(which i did).

Please humor me and let me try to retype the code again
(coz i'm probably,well I know, i was not clear).
========================================================
For declarations:

In vb.net I typed:

Dim objAPPLICATION As APPLICATION.SYSTEMObject
Dim objSession As APPLICATION.SESSIONObject
Dim objSCREEN As APPLICATION.SCREENObject

In c# I typed:

APPLICATION.SYSTEMObject objAPPLICATION;
APPLICATION.SESSIONObject objSession;
APPLICATION.SCREENObject objSCREEN;
=======================================================

For setting:

In vb.net I typed:

objAPPLICATION = CreateObject ("APPLICATION.SYSTEMObject")
objSession = objAPPLICATION.ActiveSession
objSCREEN = objSession.Screen

In c# I typed:

objAPPLICATION = APPLICATION.SYSTEMObject;
objSession = APPLICATION.SESSIONObject;
objSCREEN = APPLICATION.SCREENObject;
=========================================================

In vb.net I go on and code the rest using the objects.

In c# i get a "can not implicitly" error.

What am i doing wrong in c#?

I'm guessing that "dim"'ing an object and "set"'ing it is
not as simple in c# as in vb.net.

What help references, books, websites do you think I
should read upon to understand more fully of what you had
suggested to do and to be able to achieve what is
seemingly more simple in vb.net to c#?

thanks so much for your help!
-----Original Message-----
nique,

The code:

COMNAME.COMNAMEScreen objCurrentScreen =

Is not going to compile, and the rest of your code should not compile as
a result.

In order to get the same object in C#, you can do
it
in one of two ways.
The first would be to call the CreateObject static
method
on the Interaction
class in the Microsoft.VisualBasic namespace. This can be found in
Microsoft.VisualBasic.dll.

The other way would be to create a type definition from a program id,
using the static GetTypeFromProgId method on the Type class. Once you do
that, you can pass that Type instance to the static CreateInstance method on
the Activator class, and then cast the return type to your interface
definition (COMMNAME.Application).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hello,

I am trying to convert a simple application I have
written, first in vb6 then to vb.net and now I would like
to convert it to c#.

The part that I am having problems with in c# is
referencing the objects.

I keep getting the error "Can not implicitly
convert 'object' to(whatever object i'm trying to set it
at)"

I have added the com reference in my c# project and when I
am declaring objects, seems to be 'okay' before runtime.

The part of the vb.net code I would like to convert
to
c#
is:

Public Class Form1

Dim objApplication As COMMNAME.Application
Dim objCurrentScreen As COMNAME.COMNAMEScreen
Dim objSession As COMNAME.COMNAMESESSION

Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click

objApplication = CreateObject("COMMNAME.Application")
objSession = objApplication.ACTIVESESSION
objCurrentScreen = objSession.Screen

End sub

End Class

This part of the code was written originally in vb6, and
works fine in vb.net.

What is the equivalent version of this code in c# (without
the button1_click part)?

I have tried the following code:

COMMNAME.Application objApplication;
COMNAME.COMNAMESESSION objSession;
COMNAME.COMNAMEScreen objCurrentScreen =

objApplication = COMMNAME.Application;
objSession = objApplication.COMNAMESESSION;
objCurrentScreen = objSession.SCREEN;

And I get that error:"Can not implicitly convert 'object'
to'.

Anyone's help will be greatly appreciated.

Thanks!




.


.
 
Back
Top