3

I would like to use the below VB.Net class in my C# Project. Is there any way to do it?. Thanks in advance.

 Public Class XMLItemList
        Private sb As System.Text.StringBuilder

        Public Sub New()
            sb = New System.Text.StringBuilder
            sb.Append("<items>" & vbCrLf)
        End Sub

        Public Sub AddItem(ByVal Item As String)
            sb.AppendFormat("<item id={0}{1}{2}></item>{3}", Chr(34), Item, Chr(34), vbCrLf)
        End Sub

        Public Overrides Function ToString() As String
            sb.Append("</items>" & vbCrLf)
            Return sb.ToString
        End Function     
    End Class
2
  • 1
    1) It appears to be simple enough to translate 6 lines of VB into C#, if you don't need to keep it as VB, and 2) Why do people keep constructing XML using string concatenation, rather than, I don't know, actual components that understand XML. Commented Dec 11, 2011 at 8:02
  • 7
    Please, also, note how broken this class is. You can only call ToString() once, otherwise what it's returning definitely isn't XML. Commented Dec 11, 2011 at 8:04

4 Answers 4

4

You need to create VB.NET DLL assembly (using Library project template from Visual Studio) and then you can add the reference of DLL assembly to your C# project. Be sure that the only public types (classes) are visible outside the assembly.

Sign up to request clarification or add additional context in comments.

Comments

4

Yes. Because both VB.NET and C# compile into IL (intermediate language), you can simply create a library (a DLL file) in VB.NET, and then use that library in C#.

Comments

3

It's better to translate it into C#, and to fix the broken ToString() implementation:

  using System.Xml;

  public class XMLItemList
  {
    XmlElement el;
    XmlDocument doc;

    public XMLItemList()
    {
      doc = new XmlDocument();
      el = doc.CreateElement("items");
    }

    public void AddItem(string item)
    {
      var itemXml = doc.CreateElement("item");
      var attr = doc.CreateAttribute("id");
      attr.Value = item;
      itemXml.Attributes.Append(attr);

      el.AppendChild(itemXml);

    }

    public override string ToString()
    {
      return el.OuterXml;
    }
  }

3 Comments

It isn't necessarily the best idea. Code duplication is never good. This would be the best idea if they don't want to keep the original VB.Net version.
Well, my opinion is that the original VB version isn't worth keeping. The above, could of course, be translated back into VB.
Would like to +100 if I could..... that original code is indeed beyond repair - you can just hope it's biologically disposable bytes :-)
0

Convert the class into C# or save the class in a class library and reference it in the C# project.

1 Comment

more precise, in a class library project

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.