Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is C# null translated as Empty in VB6, instead of Nothing

I have a C# application that reference a VB6 dll. When I pass null from C# into VB6 dll function, the null is translated as value Empty (value) in VB6, instead of Nothing (object). For example:

 // function in vb6 dll that referenced by c# app
 Public Sub TestFunc(ByVal oValue As Variant)
 {
   ...
   if oValue is Nothing then
     set oValue = someObject
   end if
   ...

 }

 // main c# code
 private void Form1_Load(object sender, EventArgs e)
 {
    object testObject = new object();
    testObject = null;
    TestFunc(testObject);
 }

When I pass an object (not null) then it will be passed into the VB6 as object. But when null passed into vb6, it becomes value type Empty, instead of object type Nothing. Any one knows why? and is there anyway I can force null as Nothing in VB6 when passed from c# app?

Thanks a lot.

like image 969
tiftif Avatar asked Jan 13 '10 17:01

tiftif


People also ask

Why is C not A or B?

Because C comes after B The reason why the language was named “C” by its creator was that it came after B language. Back then, Bell Labs already had a programming language called “B” at their disposal.

What is C language called?

C is an imperative, procedural language in the ALGOL tradition. It has a static type system. In C, all executable code is contained within subroutines (also called "functions", though not in the sense of functional programming).

Who named C language?

C, computer programming language developed in the early 1970s by American computer scientist Dennis M. Ritchie at Bell Laboratories (formerly AT&T Bell Laboratories).

Why is C language important?

C is a procedural language that supports structured programming; it has a static system and a compiler written in C itself. Since its release, C became a milestone of computing history and has become the most critical component throughout the computer industry.


1 Answers

Some more information, rather than an answer. I just ran this VB6 scratch program to confirm whether Nothing can be passed ByVal. It can be.

Private Sub Form_Load()
  Call TestSub(Nothing)
End Sub
Private Sub TestSub(ByVal vnt As Variant)
  Debug.Print VarType(Nothing)
  Debug.Print VarType(vnt)
  If vnt Is Nothing Then Debug.Print "vnt Is Nothing"
  If IsEmpty(vnt) Then Debug.Print "vnt Is Empty"
End Sub

I got the following output. Note that 9 is vbObject and indicates a Variant holding an Object reference.

 9 
 9 
vnt Is Nothing

I haven't tested moving TestStub into another component but I think that'd still work. So I think the .NET marshalling to COM could do better.

like image 188
MarkJ Avatar answered Oct 17 '22 02:10

MarkJ