Results 1 to 6 of 6
  1. #1
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Minneapolis, Minnesota, USA
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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 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!
    <font face="Comic Sans MS"><font color=blue>~Shane</font color=blue></font face=comic>

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

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

    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.

  3. #3
    5 Star Lounger jujuraf's Avatar
    Join Date
    Jun 2001
    Location
    San Jose, California, USA
    Posts
    1,061
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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. <img src=/S/crossfingers.gif border=0 alt=crossfingers width=17 height=16>

    Deb

  4. #4
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Minneapolis, Minnesota, USA
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Thanks, all, for the suggestions. After trying a number of variations in VB.Net on System.Text.Encoding.<encoding_name>.GetString(<by te_array>) 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 <font color=blue>public class</font color=blue> 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
    <font face="Comic Sans MS"><font color=blue>~Shane</font color=blue></font face=comic>

  5. #5
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Shane,
    Good job on figuring this out. There's a June 2003 MSDN mag. article that covers something similar: using the J# zip namespace (java.util.zip) from within a C# project. C# doesn't have any built-in classes for writing out to zip files; but you can use the J# classes to do it.

    http://msdn.microsoft.com/msdnmag/issues/0...on/default.aspx
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  6. #6
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •