Q: Newbee question, regarding a couple of syntaxes in c#

  • Thread starter Thread starter Visual Systems AB \(Martin Arvidsson\)
  • Start date Start date
V

Visual Systems AB \(Martin Arvidsson\)

Hi!

I was testing an application that is writing a mail and sends it to a
recieptiens. I then came across
a couple of ??? in my head, coul anyone briefly tell me what the difference
is between this and where in the help i can read more about it

First out...

Outlook.Application oApp = new Outlook.Application();
My guess is that this create a new object oApp from this
Outlook.Application? Right?

Then the following, i just don't get, what they do and what it is called...

Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
(Why not xxx = new xxx)

Outlook.MailItem oMsg =
(Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;

Thanx in advance for any help

Regards

Martin Arvidsson
 
Hi,
Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
(Why not xxx = new xxx)

Outlook.MailItem oMsg =
(Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;

You don't need to create new namespace (therefore there is no xxx = new xxx
:)), but you get the namespace from the outlook application object you
created. Same goes with MailItem -> you call method CreateItem with
parameter which descibes the item you are creating (olMailItem) and because
CreateItem returns object you have to cast that to Outlook.MailItem.

Hope this helps..
 
Outlook.Application oApp = new Outlook.Application();
My guess is that this create a new object oApp from this
Outlook.Application? Right?
Yes!

Then the following, i just don't get, what they do and what it is
called...

Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
(Why not xxx = new xxx)

GetNamespace is a factory method. A factory is a method that creates an
object instance for you based on parameters or other stimuli.
On the inside the GetNamespace method could do something like this:
public NameSpace GetNamespace(string ns) {
if (ns=="mapi") return new MapiNamespace();
else return new OtherNamespace();
}

Outlook.MailItem oMsg =
(Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

The CreateItem is another factory method. This method creates the item type
you ask for, in your example an olMailItem. Because the return type of the
factory is a base class you have to cast it to the more specific type
MailItem.
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
Same as above. The Recipients property is casted to a Recipients type.

Anders Norås
blog: http://dotnetjunkies.com/weblog/anoras
 
Back
Top