Results 1 to 9 of 9

Thread: how can I use a VB.NET file in C#?

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    how can I use a VB.NET file in C#?

    I have a VB.NET class, I added it to a C# project and I tried to make an instance of that class in C#.....
    I'm getting this error: "The type or namespace name 'Resizer' could not be found (are you missing a using directive or an assembly reference?)"

    Rezier is the name of my VB class and this is my code:
    Resizer a;


    I dont even know if this is possible or not I just gave it a try....so is it possible? if so, what am I doing wrong?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I don't believe you can use it as a class specifically but you could compile it to a dll and then use it in the C# project.

  3. #3
    Hyperactive Member
    Join Date
    Feb 2002
    Posts
    261
    Originally posted by Edneeis
    I don't believe you can use it as a class specifically but you could compile it to a dll and then use it in the C# project.
    Hmm. I read somewhere that you can mix vb and c# code in the same project (so long as their in different files). I'll go look into it.

  4. #4
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Make sure that you put the using statement at the top of the forms that you want to use that class in.

    eg.
    using Resizer;
    Dont gain the world and lose your soul

  5. #5
    Junior Member
    Join Date
    Sep 2002
    Location
    Belgium
    Posts
    16

    Runtime OK - Dev not sure

    Hi,

    The IL - Intermediate Language... call it byte code - is definitely interchangeable. I'm not sure this is true for the source code in projects.

    What certainly should work (didn't do it yet myself):
    - write your VB.Net Class (.vb file)
    - compile the assembly
    - switch to the C# project
    - add the VB assembly to the references of your C# project
    - import (or use) the namespace -- not shure this is necessary
    - you should now be able to declare a type variable with the VB class and instanciate it

    I'll let you know if this works.

  6. #6

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    confused
    first... If I dont have a WINDOWS APPLICATION project, I'm not gunno be able to use my class. My class uses the label control and some other controls which are in Windows.Forms....
    I tried to import system.windows.Forms, but it says that the namespace cannot be found (in a console app project)


    other things that I need to know -> how do I compile my class to a dll as Edneeis said?
    how do I compile an assembly as marcg suggested?


    thanks for the help
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  7. #7
    Junior Member
    Join Date
    Sep 2002
    Location
    Belgium
    Posts
    16

    Some testing results

    Hi,

    I did the test and I could make it work as described in my previous post:

    - build VB.Net project
    - add it to references in my C# project (used the "Projects" tab and then "Browse..." to find the compiled VB assembly - otherwise you should add it to the GAC - good luck with that)
    - instantiate it either by fully qualified name or by using a "using" directive at the top of the C# file

    Some pitfalls:
    - don't use the same name for both projects (.net gets confused then)
    - you can't just add the project to the c# solution (I could't get this to work) and defenitely not add the VB file to the C# project (it doesn't even show up as a class in this case)
    - beware of upper/lowercase (c# is case sensitive, VB not)
    - don't use dashes (-) in any of the names (seems to be valid in VB but not in C#)

    I hope this helps.

    Bye!

  8. #8
    Junior Member
    Join Date
    Sep 2002
    Location
    Belgium
    Posts
    16
    Hi,

    To compile - just open the project and hit Build (or Rebuild). You find the DLL (that contains the assembly) in the \bin subfolder of your project folder.

    Success!

  9. #9
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    And when compiling, make sure you have the word 'Release' in the text box instead of Debug. This will create a folder called release in the \bin folder. That will be your fully compiled dll that doesn't have debug info in it.

    You have to make the reference to it before you can use it (early binding). I am sure you can still late bind in VB.Net, but I don't know how yet.

Posting Permissions

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



Click Here to Expand Forum to Full Width