Solved

Object to array

Posted on 2016-10-12
7
48 Views
Last Modified: 2016-11-07
Is it possible to turn an object to an array in VB.net?

I have a object from outlook which is a mailitem, and I want to turn all the variables into an array to store in a file.

I know Ill loose the functionality of the object (such as move, copy etc) however i only want to store all the information in the object (in this case mailID, recipents etc).

Usually I could just count the elements and then copy the values one at a time, however the mailitem is a nested array inside a nested array inside a nested array so could be quite confusing.

Any ideas?
0
Comment
Question by:tonelm54
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41839660
I have a object from outlook which is a mailitem, and I want to turn all the variables into an array to store in a file.

yes, that's possible. you may want to consider to store it in a List(Of T) or look into the ToArray() method

List(Of T) Class
https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx
(look for the possible constructors and click on it to see the examples)

List<T>.ToArray Method ()
https://msdn.microsoft.com/en-us/library/x303t819(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
0
 

Author Comment

by:tonelm54
ID: 41841016
Couldnt get these to work :-(

So my code at the moment is:-
            Dim mail As Outlook.MailItem = item
            Dim output() As String = mail.toarray()

            Dim serializer As New JavaScriptSerializer()
            Dim arrayJson As String = serializer.Serialize(output)

            MsgBox(arrayJson)

Open in new window


Any ideas what Im doing wrong?
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41841403
what info you wish to read from MailItem and put into JSON array ?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tonelm54
ID: 41841448
Problem is, I dont know yet :-S

The data to store in the database (to search) keeps changing, so my plan was to store all the information in a JSON text file, then when a request comes along to add xxx to the search I can simply write some code to look at the JSON file and pull the requested field into the database and build the indexes for searching.

Initially they only wanted the `Date Received` and the `Subject`. Now its the `recipient` and `sender`, and already I've heard rumours they want the `conversationID` and Ive had requests to add `attachments` into the search. Hence store everything in a JSON text file and pull as needed, instead of trying to upload from the PST files again (which takes ages).
0
 

Author Comment

by:tonelm54
ID: 41841452
So looking at the element for mail (which is a mailtem from outlook) I get the following elements:-
Untitled.png
I did think I could loop through each element and store in an array via:-
Dim objElement As Object
For Each objElement in mail

Next

Open in new window


But I cant figure a way of reading all the elements inside mail.
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41841464
perhaps it doesn't make sense if we are looping through all the elements in mailtem since it's quite complex to read through all its content, as back to the basic, we only store useful info into database, and hence we only extract useful info from the source.
0
 
LVL 52

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41841488
I come out with something like this, not too sure if this is useful for you.

Private Sub Test()
        Dim tempApp As Outlook.Application
        Dim tempInbox As Outlook.MAPIFolder
        Dim InboxItems As Outlook.Items

        tempApp = CreateObject("Outlook.Application")
        tempInbox = tempApp.GetNamespace("Mapi").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
        InboxItems = tempInbox.Items

        Dim MailList As New List(Of MyMailItem)

        'Dim newMail As Outlook.MailItem
        For Each newMail In InboxItems
            If TypeOf newMail Is Outlook.MailItem Then
                'You may want to put some conditions here?
                If newMail.Subject.IndexOf("whatever") > 0 Then
                    Dim itm As New MyMailItem
                    itm.Subject = newMail.Subject
                    itm.ReceivedTime = newMail.ReceivedTime
                    itm.SenderName = newMail.SenderName
                    itm.SenderEmailAddress = newMail.SenderEmailAddress

                    For Each x As Outlook.Recipient In newMail.Recipients
                        Dim y As New MyRecipient()
                        y.Name = x.Name
                        y.EmailAddress = x.Address
                        itm.Recipient.Add(y)
                    Next
                    MailList.Add(itm)
                End If
            End If
        Next
        Dim serializer As New JavaScriptSerializer()
        Dim arrayJson As String = serializer.Serialize(MailList)
        MsgBox(arrayJson)
    End Sub

Open in new window

then you would need to have some classes like these:
Class MyRecipient
    Public Name As String
    Public EmailAddress As String
End Class

Class MyMailItem
    Public Subject As String
    Public ReceivedTime As String
    Public SenderName As String
    Public SenderEmailAddress As String
    Public Recipient As New List(Of MyRecipient)
End Class

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question