converting values of java.util.list to string or other normal format in vb

前提是你 提交于 2019-12-13 06:43:53

问题


in my application, I am pulling in items from a microsoft project file using mpxj - one of the items I need is the predecessors. The way I am able to pull the predecessor is using a build in function of mpxj which returns a type of java.util.list - I can save this to variable as an object, but I need to find a way to bring the data to a format I can easily use so I can store it into a database. Listed below is the line of code I am using to pull the predecessors from the project file.

Dim predecessors = task.getPredecessors

and here is the result when putting a tracepoint in to get the value of predecessors

[[Relation [Task id=4 uniqueID=45577 name=Standards Training - Round 2] -> [Task id=3 uniqueID=45576 name=Process Excellence Training]]]

Even if I could get the above as a string, I could work with it enough to get the data I need. The above example is where there is 1 item in the predecessor list, but sometimes there are multiple items. Here is an example of the tracepoint when there are multiple items.

[[Relation [Task id=63 uniqueID=45873 name=Complete IP Binder] -> [Task id=47 uniqueID=45857 name=Organizational Assessment]], [Relation [Task id=63 uniqueID=45873 name=Complete IP Binder] -> [Task id=49 uniqueID=45859 name=Document Deliverables]], [Relation [Task id=63 uniqueID=45873 name=Complete IP Binder] -> [Task id=56 uniqueID=45866 name=Infrastructure Deliverables]], [Relation [Task id=63 uniqueID=45873 name=Complete IP Binder] -> [Task id=58 uniqueID=45868 name=IT Deliverables]], [Relation [Task id=63 uniqueID=45873 name=Complete IP Binder] -> [Task id=60 uniqueID=45870 name=Organizational Deliverables]]]

Thank you for your help.


回答1:


In C# 3.5+, creating an extension method to give you a nice, type-safe enumerator for these Java Lists is really easy because of the yield keyword. You can do it like so:

public static class JavaExtensions
{

        public static IEnumerable<T> toIEnumerable<T>(this java.util.List list)
        {
            if (list != null)
            {
                java.util.Iterator itr = list.iterator();

                while (itr.hasNext())
                {
                    yield return (T)itr.next();
                }
            }
        }

}

As long as the namespace containing this extension is visible to your code, then you can just do the following to work with the predecessors:

foreach (var relation in task.getPredecessors().toIEnumerable<Relation>())
{
   Task sourceTask = relation.getSourceTask();
   //etc.
}

Unfortunately for you, you are using VB.Net. I was in the same boat as you, and ended up making a separate C# class library that I reference in my VB ASP.NET project. That way I get the benefits of C# syntax in dealing with the IKVM Java-style objects without having to convert my whole project.

If you don't want to do that, you can use an online code converter to change Jon's code (which doesn't use any C#-only features) to VB and include it in your project.

The main thing in your case above, is that you don't need to work with the string representation you see in the debugger (that's just calling toString() on the Relation objects in that list). The getPredecessors() function is returning a list of Relation objects.

Dim predecessorList as java.util.List = task.getPredecessors()
Dim iter as java.util.Iterator = predecessorList.iterator()

Do While iter.hasNext()
    Dim curRelation as Relation = iter.next()

    'gets the left side of the relationship (the task you are dealing with)
    Dim sourceTask as Task = curRelation.getSourceTask()

    'gets the task that is the predecessor to the  'source' task
    Dim targetTask as Task = curRelation.getTargetTask()

    'from here you can call methods on the Task objects to get their other properties
    'like name and id
Loop



回答2:


There are two ways to work with Java collections when using a .Net assembly produced by IKVM. The first is to do things the Java way and work with an iterator:

java.util.List predecessors = task.getPredecessors();
java.util.Iterator iter = predecessors.iterator();
while (iter.hasNext())
{
   Relation rel = (Relation)iter.next();
   System.Console.WriteLine(rel);
}

The other way is to add a little "usability" code to hide this:

foreach(Relation rel in ToEnumerable(task.getPredecessors()))
{
   System.Console.WriteLine(rel);
}

To do this I've create the "ToEnumerable" method:

private static EnumerableCollection ToEnumerable(Collection javaCollection)
{
   return new EnumerableCollection(javaCollection);
}

and the EnumerableCollection class, which hides the use of the iterator:

class EnumerableCollection
{
    public EnumerableCollection(Collection collection)
    {
        m_collection = collection;
    }

    public IEnumerator GetEnumerator()
    {
        return new Enumerator(m_collection);
    }

    private Collection m_collection;
}


public struct Enumerator : IEnumerator
{
    public Enumerator(Collection collection)
    {
        m_collection = collection;
        m_iterator = m_collection.iterator();
    }

    public object Current
    {
        get
        {
            return m_iterator.next();
        }
    }

    public bool MoveNext()
    {
        return m_iterator.hasNext();
    }

    public void Reset()
    {
        m_iterator = m_collection.iterator();
    }

    private Collection m_collection;
    private Iterator m_iterator;
}

A more interesting way to do this would be to use Extension Methods to add this functionality to the java Collection classes, which would make your code a bit less cluttered. I plan to try and ship an assembly containing useful extension methods as part of MPXJ in the next release.




回答3:


was able to complete this by using tostring then right and left to cut out what i need, much quicker - dont know why this was deleted before



来源:https://stackoverflow.com/questions/9708320/converting-values-of-java-util-list-to-string-or-other-normal-format-in-vb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!