473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Casting - Cant convert VB sample.

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
Nov 15 '05 #1
9 2039
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" <an*******@discussions.microsoft.com> wrote in message
news:62**********************************@microsof t.com...
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.
Can Reflection help here in anyway?

Thanks

Kurt

Nov 15 '05 #2
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
Nov 15 '05 #3
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" <an*******@discussions.microsoft.com> wrote in message
news:7F**********************************@microsof t.com... 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

Nov 15 '05 #4
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" <an*******@discussions.microsoft.com> wrote in message
news:7F**********************************@microsof t.com...
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

Nov 15 '05 #5
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" <an*******@discussions.microsoft.com> wrote in messag
news:7F**********************************@microsof t.com.. 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 tha 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
----- 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

Nov 15 '05 #6
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" <an*******@discussions.microsoft.com> wrote in message
news:07**********************************@microsof t.com...
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" <an*******@discussions.microsoft.com> wrote in message
news:7F**********************************@microsof t.com...
> 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
>>

Nov 15 '05 #7
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" <an*******@discussions.microsoft.com> wrote in message
news:07**********************************@microsof t.com...
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" <an*******@discussions.microsoft.com> wrote in message
news:7F**********************************@microsof t.com...
> 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
>>

Nov 15 '05 #8
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.
Nov 15 '05 #9
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" <an*******@discussions.microsoft.com> wrote in message
news:E0**********************************@microsof t.com...
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.

Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: KC | last post by:
Could some one explain to me the casting rules for sending generic lists, ex. List<Person>, to a function that accepts List<object>? I cannot get the following easy-cheesy app to work. I get the...
23
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than...
8
by: tom | last post by:
Hi All, I'm stuck whit an issue I can't seem to resolve in C#: I have an arry of bytes which I would like to "recast" to an array of structs with an Explicit layout. I tried the...
12
by: 6tc1 | last post by:
Hi all, I just discovered a rounding error that occurs in C#. I'm sure this is an old issue, but it is new to me and resulted in a fair amount of time trying to track down the issue. Basically...
33
by: Mark P | last post by:
A colleague asked me something along the lines of the following today. For some type X he has: X* px = new X; Then he wants to convert px to a char* (I'm guessing for the purpose of...
6
by: Ken Fine | last post by:
This is a basic question. What is the difference between casting and using the Convert.ToXXX methods, from the standpoint of the compiler, in terms of performance, and in other ways? e.g. ...
2
by: =?Utf-8?B?VHJldm9y?= | last post by:
Hi, I'm trying to write a general function to simply clear a collection of fields from both a group box and a tab control. Here is what I want my code to look like: private void...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
101
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.