Question

Here is my code in VB.net. It displays AB, CD, EF. How do I make a subroutine so I can call like: Sub-name(field65, field547, field66)?

Sub main()
    Dim fileName As String = "C:\AAA\test5.xml"
    Dim root As XElement = XDocument.Load(fileName).Root
    Dim a = root.<field65>.Value
    Dim b = root.<field547>.Value
    Dim c = root.<field66>.Value
    Console.WriteLine(a)
    Console.WriteLine(b)
    Console.WriteLine(c)
    Console.WriteLine("Please Press any key to continue!")
    Console.ReadKey()
End Sub
Was it helpful?

Solution

Sub Foo(ByVal name1 As String, ByVal name2 As String, ByVal name3 As String)
    Dim fileName As String = "C:\AAA\test5.xml"
    Dim root As XElement = XDocument.Load(fileName).Root
    Dim a = root.Element(name1).Value
    Dim b = root.Element(name2).Value
    Dim c = root.Element(name3).Value
    Console.WriteLine(a)
    Console.WriteLine(b)
    Console.WriteLine(c)
    Console.WriteLine("Please Press any key to continue!")
    Console.ReadKey()

End Sub

should do, you can then call Foo("field65", "field547", "field547").

But using

Sub Foo(ByVal name1 As XName, ByVal name2 As XName, ByVal name3 As XName)
    Dim fileName As String = "C:\AAA\test5.xml"
    Dim root As XElement = XDocument.Load(fileName).Root
    Dim a = root.Element(name1).Value
    Dim b = root.Element(name2).Value
    Dim c = root.Element(name3).Value
    Console.WriteLine(a)
    Console.WriteLine(b)
    Console.WriteLine(c)
    Console.WriteLine("Please Press any key to continue!")
    Console.ReadKey()

End Sub

is nicer and works with namespaces to.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top