Casting - Cant convert VB sample.

G

Guest

Hi I was trying to get this VB type code to work in C

Sub SetColumns_Example(
Dim ol As Outlook.Applicatio
Dim MyFolder As MAPIFolde
Dim itms As Item
Dim itm As Objec
Dim dtmStart As Date, dtmEnd As Dat
Dim lngElapsed As Lon
Set ol = New Outlook.Applicatio
Set MyFolder = ol.Session.GetDefaultFolder(10
Set itms = MyFolder.Item
itms.SetColumns "[FullName],[CompanyName]
For Each itm In itm
Debug.Print itm.FullName & ", " & itm.CompanyNam
Nex

Here is my first C# attempt

items.SetColumns("[FullName],[CompanyName]")
foreach( Object o in items

if(o != null

Console.WriteLine(o.FullName); // Gives compile error (251): 'object' does not contain a definition for 'FullName



Heres another attempt

items.SetColumns("[FullName],[CompanyName]")
foreach( Microsoft.Office.Interop.Outlook.ContactItem ci in items) // Generates invalid cast exception

if(ci != null

Console.WriteLine(ci.FullName);



Another attempt

items.SetColumns("[FullName],[CompanyName]")
foreach( Object o in items

Microsoft.Office.Interop.Outlook.ContactItem ci = o as Microsoft.Office.Interop.Outlook.ContactItem
// ci is ALWAYS null... :-
if(ci != null

Console.WriteLine(o.FullName); // Gives compile error (251): 'object' does not contain a definition for 'FullName



without the items.SetColumns, items contains ContactItems, and It works in the simple case. The .SetColumns seems to change the type of object inside

Can Reflection help here in anyway

Thank

Kurt
 
V

Vadym Stetsyak

Maybe something like this?

void SetColumns_Example()
{
Outlook.Application ol;
MAPIFolder MyFolder;
Items itms;
// object itm;
Date dtmStart, dtmEnd;
long lngElapsed;

ol = new Outlook.Application();
MyFolder = ol.Session.GetDefaultFolder(10);
itms = MyFolder.Items;
//IMHO
itms.SetColumns[FullName,CompanyName]
foreach(Item itm in itms)
{
Debug.Print itm.FullName & ", " & itm.CompanyName
}
}

If you will post more code then it will be easier to help you

Kurt said:
Hi I was trying to get this VB type code to work in C#

Sub SetColumns_Example()
Dim ol As Outlook.Application
Dim MyFolder As MAPIFolder
Dim itms As Items
Dim itm As Object
Dim dtmStart As Date, dtmEnd As Date
Dim lngElapsed As Long
Set ol = New Outlook.Application
Set MyFolder = ol.Session.GetDefaultFolder(10)
Set itms = MyFolder.Items
itms.SetColumns "[FullName],[CompanyName]"
For Each itm In itms
Debug.Print itm.FullName & ", " & itm.CompanyName
Next


Here is my first C# attempt:

items.SetColumns("[FullName],[CompanyName]");
foreach( Object o in items)
{
if(o != null)
{
Console.WriteLine(o.FullName); // Gives compile error (251):
'object' does not contain a definition for 'FullName'
}
}

Heres another attempt:

items.SetColumns("[FullName],[CompanyName]");
foreach( Microsoft.Office.Interop.Outlook.ContactItem ci in items) //
Generates invalid cast exception.
{
if(ci != null)
{
Console.WriteLine(ci.FullName);
}
}

Another attempt:

items.SetColumns("[FullName],[CompanyName]");
foreach( Object o in items)
{
Microsoft.Office.Interop.Outlook.ContactItem ci = o as Microsoft.Office.Interop.Outlook.ContactItem;
// ci is ALWAYS null... :-(
if(ci != null)
{
Console.WriteLine(o.FullName); // Gives compile error (251):
'object' does not contain a definition for 'FullName'
}
}

without the items.SetColumns, items contains ContactItems, and It works in
the simple case. The .SetColumns seems to change the type of object inside.
 
G

Guest

Well in this case I would get an invalid cast exception in th
foreach(Item itm in itms
line

As I mentioned I tried myself to do th
foreach( Microsoft.Office.Interop.Outlook.ContactItem ci in items) // Generates invalid cast exception
in my first example

It works if we never do the items.SetColumns

Before we do the items.SetColumns, items appears to contain objects that can be casted to Outlook.ContactItem objects. After we do the items.SetColums, items appears to contain objects that CANNOT be casted to Outlook.ContactItem objects

Just wondering what I was doing wrong


----- Vadym Stetsyak wrote: ----

Maybe something like this

void SetColumns_Example(

Outlook.Application ol
MAPIFolder MyFolder
Items itms
// object itm
Date dtmStart, dtmEnd
long lngElapsed

ol = new Outlook.Application()
MyFolder = ol.Session.GetDefaultFolder(10)
itms = MyFolder.Items
//IMH
itms.SetColumns[FullName,CompanyName
foreach(Item itm in itms

Debug.Print itm.FullName & ", " & itm.CompanyNam



If you will post more code then it will be easier to help yo
 
J

Jay B. Harlow [MVP - Outlook]

Kurt,
Do you have Distribution Lists in your contacts folder?

The Contacts folder will normally contain both ContactItem & DistListItem
objects, however if you manage to get another Item Type in the folder there
may be more!

Also SetColumns itself may be returning an object of a different type, as
you are effectively indicating that you want a subtype of Contact, not a
Contact. I will need to test SetColumns later, as I have not used SetColumns
from .NET yet...

I would use an object variable for the for each itself, then cast it to the
respective object type above.

Something like (untested):
foreach(object itm in itms)
{
if (itm is ContactItem)
{
ContactItem contact = itm as ContactItem;
Debug.Print contact.FullName & ", " & contact.CompanyName
}
if (itm Is DistListItem)
{
DistListItem dist = itm as DistListItem ;
Debug.Print dist.DLName;
}
if (itm Is ???) ' check for SetColumn type of object
...

If you don't have it, the following site provides a plethora of information
on using Outlook from .NET.

http://www.microeye.com/resources/res_outlookvsnet.htm

Hope this helps
Jay


Kurt said:
Well in this case I would get an invalid cast exception in the
foreach(Item itm in itms)
line.

As I mentioned I tried myself to do the
foreach( Microsoft.Office.Interop.Outlook.ContactItem ci in items) //
Generates invalid cast exception.
in my first example.

It works if we never do the items.SetColumns.

Before we do the items.SetColumns, items appears to contain objects that
can be casted to Outlook.ContactItem objects. After we do the
items.SetColums, items appears to contain objects that CANNOT be casted to
Outlook.ContactItem objects.
Just wondering what I was doing wrong.


----- Vadym Stetsyak wrote: -----

Maybe something like this?

void SetColumns_Example()
{
Outlook.Application ol;
MAPIFolder MyFolder;
Items itms;
// object itm;
Date dtmStart, dtmEnd;
long lngElapsed;

ol = new Outlook.Application();
MyFolder = ol.Session.GetDefaultFolder(10);
itms = MyFolder.Items;
//IMHO
itms.SetColumns[FullName,CompanyName]
foreach(Item itm in itms)
{
Debug.Print itm.FullName & ", " & itm.CompanyName
}
}

If you will post more code then it will be easier to help you
 
J

Jay B. Harlow [MVP - Outlook]

Kurt,
I'm still investigating this one. I can reproduce the same behavior in VBA &
VB.NET.

I think it has to do with the "type" of object that is returned when you use
Items.SetColumns, in that I don't think its a normal ContactItem any
longer...

I'll keep you posted.

Jay

Kurt said:
Well in this case I would get an invalid cast exception in the
foreach(Item itm in itms)
line.

As I mentioned I tried myself to do the
foreach( Microsoft.Office.Interop.Outlook.ContactItem ci in items) //
Generates invalid cast exception.
in my first example.

It works if we never do the items.SetColumns.

Before we do the items.SetColumns, items appears to contain objects that
can be casted to Outlook.ContactItem objects. After we do the
items.SetColums, items appears to contain objects that CANNOT be casted to
Outlook.ContactItem objects.
Just wondering what I was doing wrong.


----- Vadym Stetsyak wrote: -----

Maybe something like this?

void SetColumns_Example()
{
Outlook.Application ol;
MAPIFolder MyFolder;
Items itms;
// object itm;
Date dtmStart, dtmEnd;
long lngElapsed;

ol = new Outlook.Application();
MyFolder = ol.Session.GetDefaultFolder(10);
itms = MyFolder.Items;
//IMHO
itms.SetColumns[FullName,CompanyName]
foreach(Item itm in itms)
{
Debug.Print itm.FullName & ", " & itm.CompanyName
}
}

If you will post more code then it will be easier to help you
 
G

Guest

Thanks a lot for answering that Jay. I believe all my test folders contain only ContactItems. There are no DistListItems

Before I call SetColumns on Items, (itm is ContactItem) returns true for each one. After I call the SetColumns, (itm is ContactItem) returns false for every item, as does (itm is DistListItem

if I do a itm.GetType().ToString(), I see that each object is a System.__ComObjec

It appears as though visual basic can cast this into a ContactItem but C# cannot

Is it likely that functions such as SetColumns will only be useful in visual basic, and c# lacks the casting abilities of VB that a function like SetColumns relies on

I would love to see some example c# code that uses SetColumns, or some similar category of function

What can we use to look inside an object and find out what values of SomeClass will evaluate (itm is SomeClass) to true

Thanks a lot again Jay

----- Jay B. Harlow [MVP - Outlook] wrote: ----

Kurt
Do you have Distribution Lists in your contacts folder

The Contacts folder will normally contain both ContactItem & DistListIte
objects, however if you manage to get another Item Type in the folder ther
may be more

Also SetColumns itself may be returning an object of a different type, a
you are effectively indicating that you want a subtype of Contact, not
Contact. I will need to test SetColumns later, as I have not used SetColumn
from .NET yet..

I would use an object variable for the for each itself, then cast it to th
respective object type above

Something like (untested)
foreach(object itm in itms
if (itm is ContactItem

ContactItem contact = itm as ContactItem
Debug.Print contact.FullName & ", " & contact.CompanyNam

if (itm Is DistListItem

DistListItem dist = itm as DistListItem
Debug.Print dist.DLName

if (itm Is ???) ' check for SetColumn type of objec
..
If you don't have it, the following site provides a plethora of informatio
on using Outlook from .NET

http://www.microeye.com/resources/res_outlookvsnet.ht

Hope this help
Ja


Kurt said:
Well in this case I would get an invalid cast exception in th
foreach(Item itm in itms
line
foreach( Microsoft.Office.Interop.Outlook.ContactItem ci in items) /
Generates invalid cast exception
in my first example
can be casted to Outlook.ContactItem objects. After we do th
items.SetColums, items appears to contain objects that CANNOT be casted t
Outlook.ContactItem objects
Just wondering what I was doing wrong
Maybe something like this
void SetColumns_Example(

Outlook.Application ol
MAPIFolder MyFolder
Items itms
// object itm
Date dtmStart, dtmEnd
long lngElapsed
ol = new Outlook.Application()
MyFolder = ol.Session.GetDefaultFolder(10)
itms = MyFolder.Items
//IMH
itms.SetColumns[FullName,CompanyName
foreach(Item itm in itms

Debug.Print itm.FullName & ", " & itm.CompanyNam

If you will post more code then it will be easier to help yo
 
J

Jay B. Harlow [MVP - Outlook]

Kurt,
I have not heard back from Microsoft themselves, I'll reask the question...

However a couple of fellow Outlook MVPs have confirmed the same behavior in
Outlook 2000 and in VBA. So its not a C# feature per se.

As far as I can tell when you use the SetColumns, the objects returned from
the collection are no longer ContactItem's they are some other interface
(IDispatch) and you will need to use Late Binding to get to the
properties... However that's currently only a theory, I'm still
investigating.

There is an article on the following site that uses Late Binding (via
Reflection) to get at properties of CDO, you should be able to do the same
with the above object.

http://www.microeye.com/resources/res_outlookvsnet.htm

I'll see if I can find something in the KB also...

Hope this helps
Jay


Kurt said:
Thanks a lot for answering that Jay. I believe all my test folders
contain only ContactItems. There are no DistListItems.
Before I call SetColumns on Items, (itm is ContactItem) returns true for
each one. After I call the SetColumns, (itm is ContactItem) returns false
for every item, as does (itm is DistListItem)
if I do a itm.GetType().ToString(), I see that each object is a System.__ComObject

It appears as though visual basic can cast this into a ContactItem but C# cannot.

Is it likely that functions such as SetColumns will only be useful in
visual basic, and c# lacks the casting abilities of VB that a function like
SetColumns relies on?
I would love to see some example c# code that uses SetColumns, or some similar category of function.

What can we use to look inside an object and find out what values of
SomeClass will evaluate (itm is SomeClass) to true?
Thanks a lot again Jay.

----- Jay B. Harlow [MVP - Outlook] wrote: -----

Kurt,
Do you have Distribution Lists in your contacts folder?

The Contacts folder will normally contain both ContactItem & DistListItem
objects, however if you manage to get another Item Type in the folder there
may be more!

Also SetColumns itself may be returning an object of a different type, as
you are effectively indicating that you want a subtype of Contact, not a
Contact. I will need to test SetColumns later, as I have not used SetColumns
from .NET yet...

I would use an object variable for the for each itself, then cast it to the
respective object type above.

Something like (untested):
foreach(object itm in itms)
{
if (itm is ContactItem)
{
ContactItem contact = itm as ContactItem;
Debug.Print contact.FullName & ", " &
contact.CompanyName
}
if (itm Is DistListItem)
{
DistListItem dist = itm as DistListItem ;
Debug.Print dist.DLName;
}
if (itm Is ???) ' check for SetColumn type of object
...

If you don't have it, the following site provides a plethora of information
on using Outlook from .NET.

http://www.microeye.com/resources/res_outlookvsnet.htm

Hope this helps
Jay


Kurt said:
Well in this case I would get an invalid cast exception in the
foreach(Item itm in itms)
line.
foreach( Microsoft.Office.Interop.Outlook.ContactItem ci in items)
//
Generates invalid cast exception.
in my first example.
objects that
can be casted to Outlook.ContactItem objects. After we do the
items.SetColums, items appears to contain objects that CANNOT be casted to
Outlook.ContactItem objects.
Just wondering what I was doing wrong.
----- Vadym Stetsyak wrote: -----
Maybe something like this?
void SetColumns_Example()
{
Outlook.Application ol;
MAPIFolder MyFolder;
Items itms;
// object itm;
Date dtmStart, dtmEnd;
long lngElapsed;
ol = new Outlook.Application();
MyFolder = ol.Session.GetDefaultFolder(10);
itms = MyFolder.Items;
//IMHO
itms.SetColumns[FullName,CompanyName]
foreach(Item itm in itms)
{
Debug.Print itm.FullName & ", " & itm.CompanyName
}
}
If you will post more code then it will be easier to help you
 
J

Jay B. Harlow [MVP - Outlook]

Kurt,
I found this KB article.

http://support.microsoft.com/?id=292062

Which allows you to identify a ContactItem by item type, however you are
still left with Late Binding getting to the properties of the item...

Hope this helps
Jay

Kurt said:
Thanks a lot for answering that Jay. I believe all my test folders
contain only ContactItems. There are no DistListItems.
Before I call SetColumns on Items, (itm is ContactItem) returns true for
each one. After I call the SetColumns, (itm is ContactItem) returns false
for every item, as does (itm is DistListItem)
if I do a itm.GetType().ToString(), I see that each object is a System.__ComObject

It appears as though visual basic can cast this into a ContactItem but C# cannot.

Is it likely that functions such as SetColumns will only be useful in
visual basic, and c# lacks the casting abilities of VB that a function like
SetColumns relies on?
I would love to see some example c# code that uses SetColumns, or some similar category of function.

What can we use to look inside an object and find out what values of
SomeClass will evaluate (itm is SomeClass) to true?
Thanks a lot again Jay.

----- Jay B. Harlow [MVP - Outlook] wrote: -----

Kurt,
Do you have Distribution Lists in your contacts folder?

The Contacts folder will normally contain both ContactItem & DistListItem
objects, however if you manage to get another Item Type in the folder there
may be more!

Also SetColumns itself may be returning an object of a different type, as
you are effectively indicating that you want a subtype of Contact, not a
Contact. I will need to test SetColumns later, as I have not used SetColumns
from .NET yet...

I would use an object variable for the for each itself, then cast it to the
respective object type above.

Something like (untested):
foreach(object itm in itms)
{
if (itm is ContactItem)
{
ContactItem contact = itm as ContactItem;
Debug.Print contact.FullName & ", " &
contact.CompanyName
}
if (itm Is DistListItem)
{
DistListItem dist = itm as DistListItem ;
Debug.Print dist.DLName;
}
if (itm Is ???) ' check for SetColumn type of object
...

If you don't have it, the following site provides a plethora of information
on using Outlook from .NET.

http://www.microeye.com/resources/res_outlookvsnet.htm

Hope this helps
Jay


Kurt said:
Well in this case I would get an invalid cast exception in the
foreach(Item itm in itms)
line.
foreach( Microsoft.Office.Interop.Outlook.ContactItem ci in items)
//
Generates invalid cast exception.
in my first example.
objects that
can be casted to Outlook.ContactItem objects. After we do the
items.SetColums, items appears to contain objects that CANNOT be casted to
Outlook.ContactItem objects.
Just wondering what I was doing wrong.
----- Vadym Stetsyak wrote: -----
Maybe something like this?
void SetColumns_Example()
{
Outlook.Application ol;
MAPIFolder MyFolder;
Items itms;
// object itm;
Date dtmStart, dtmEnd;
long lngElapsed;
ol = new Outlook.Application();
MyFolder = ol.Session.GetDefaultFolder(10);
itms = MyFolder.Items;
//IMHO
itms.SetColumns[FullName,CompanyName]
foreach(Item itm in itms)
{
Debug.Print itm.FullName & ", " & itm.CompanyName
}
}
If you will post more code then it will be easier to help you
 
G

Guest

Hi Jay

finally worked it out. I have to use the GetType().InvokeMember() thing. i.e

string fullName = (string)o.GetType().InvokeMember("FullName", BindingFlags.Default |BindingFlags.GetProperty
null, o, null)

Seems a bit long winded but it works

Thanks a lot for your help.
 
J

Jay B. Harlow [MVP - Outlook]

Kurt,
If you are getting a number of properties, you may want to encapsulate the
GetType().InvokeMember() in its own class of "helper" methods...

VB.NET hides the GetType().InvokeMember() from us, which in this case is
nice...

Hope this helps
Jay

Kurt said:
Hi Jay,

finally worked it out. I have to use the GetType().InvokeMember() thing. i.e.

string fullName = (string)o.GetType().InvokeMember("FullName",
BindingFlags.Default |BindingFlags.GetProperty,
 
K

kique net

Hi,

I need do something similar to that code source in C# language.

My problem, in C# and Outlook 2003, developing in Visual Studio 2003:

I need access to MapiFolders, All Public Folders, and in that folder, I
want access Contacts and Distribution List, but I have problems with
types. I cannot know the real type

Outlook.Items itemsContacts =mapiFolder.Items;

Console.WriteLine("Contactos: " + mapiFolder.Items.Count);
Console.WriteLine(" mapi " +mapiFolder.Description +
mapiFolder3.AddressBookName );

// Iterating to the allcontacts items collection
for(int cnt = 1 ; cnt < itemsContacts.Count ; cnt++)
{

Outlook.ContactItem myContact3
== (Outlook.ContactItem)itemsContacts[cnt];

..

The big problem: the type of itemsContacts[cnt], I don't know it,
beacuse itemsContacts[cnt].GetType() returns the same value always.

Can you help me, please?

Greetings...sorry, my english is poor.
 
R

Ravichandran J.V.

Set itms = MyFolder.Items
itms.SetColumns "[FullName],[CompanyName]"

What have you done with the above two vb statements in C#?

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top