• Using C# code in VB.Net project (VS.Net 2002)

    Home » Forums » Developers, developers, developers » DevOps Lounge » Using C# code in VB.Net project (VS.Net 2002)

    Author
    Topic
    #389541

    I’m traversing our Active Directory and hauling out info about each user. One piece of information I’d like is the SID, but it is stored as a byte array and I need to convert it to a string to make use of it. I found a piece of code written in C# that somebody was kind enough to post on http://www.codetools.com that has this functionality. I’m bright enough to copy the code from the web page, but am not so bright as to know what to do with it from there. Could somebody walk me through accessing a C# function in a VB.Net project, please?

    Alternatively, if there’s a VB.Net solution out there for converting a byte array to a string, I’d love to use it!

    Viewing 3 reply threads
    Author
    Replies
    • #688353

      In Getz & Gilbert’s VBA Developer’s Handbook (1997), they show how to convert between strings and byte arrays in VBA:

      dim aByt() as Byte
      aByt() = “Hello”

      That’s it! To convert back

      dim strText as String
      strText = aByt()

      Now… assuming this implicit casting was preserved in VB.NET (big assumption) there is the issue of getting your program to perceive the byte array for what it is. Not sure this helps you there.

    • #688389

      You should be able to use the C# code as is in a VB.Net project. That’s one of the benefits of the environment. I haven’t done this myself but I do have both VB and C# installed for my VS. I do not know if you NEED to have both languages installed in order to call both languages (although it seems to make sense that you would have to).

      So I’d try that first. If it does’t work, post the C# code and I can probably re-write it as VB. crossfingers

      Deb

    • #689858

      Thanks, all, for the suggestions. After trying a number of variations in VB.Net on System.Text.Encoding..GetString() with no great luck, I tried re-writing the C# function linked in my first post, but…well, let’s just say it didn’t work. Ultimately, rather than mess with rewriting it in VB.Net, I decided just to include the C# code and call the function from my VB.Net app. How?, you ask. Thusly:

      (1) Create a new C# project.
      File -> New Project ; Visual C# Projects -> Class Library.
      (2) Paste in your borrowed C# code
      Sounds easy enough, but if you’re a complete C based novice like me, the first question is, “Where do I paste it?”. You paste it below the opening squiggly bracket of the public class bit.
      (3) Add the C# project to your VB.Net solution
      Right-click on the VB.Net solution name in the Solution Explorer window and choose Add -> New Project. Browse for the location of your newly created C# project.
      (4) Build your C# Project
      Right-click on your C# project name in the Solution Explorer window and choose Build.
      (5) Reference and call your C# function from VB.Net
      Again, sounds simple but if you’ve never done it, here’s how. First, some names you

    • #690461
    Viewing 3 reply threads
    Reply To: Using C# code in VB.Net project (VS.Net 2002)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: