Coding Nothing in C#

  • Thread starter Thread starter Eric Stott
  • Start date Start date
E

Eric Stott

I have the following code in VB that needs to be done in C#
Dim o As New Company.Common.QueueItem

o.OrderID = 123456789

o.QueueItemType = Company.Common.QueueItemType.StockOrder

Dim oDispatch As New Company.Queuing.CompanyDispatchQueue(Nothing)

oDispatch.Dispatch(o, True)



I have created the following code in C#, but I am getting a Null Exception
when running it:

queue=new Company.Common.QueueItem();

queue.OrderID=123456789;

queue.QueueItemType=Company.Common.QueueItemType.Refill;

dispatch=new Company.Queuing.CompanyDispatchQueue(null);

dispatch.Dispatch(queue,true);



The definition of Nothing in VB is: Represents the default value of any data
type

Whereas Null in C# is: The null keyword is a literal that represents a null
reference, one that does not refer to any object



What can I use in subtitute of Nothing in C# in this instance?
 
Hi,
What can I use in subtitute of Nothing in C# in this instance?

"Nothing" and "null" are semantically equivalent for reference types and
will produce the same CIL. For value types, null in C# cannot be used but
Nothing in VB.NET will provide the default value, AFAIK.

Assuming that the exception is being thrown on the line that calls the
CompanyDispatchQueue constructor, the argument is a reference type, and that
the exception is an ArgumentNullException being thrown because of the "null"
argument that you have supplied, then I must assume that the VB.NET code
will fail as well.
 
Since I don't have access to the underlying code, this is the argument:
Company.Queuing.CompanyDispatchQueue(System.ComponentModel.ISyncronizeInvoke
HostForm)

and Nothing works in the case of VB.

So you are saying that Null should work in this case?
 
Since I don't have access to the underlying code, this is the
argument:
Company.Queuing.CompanyDispatchQueue(System.ComponentModel.ISyncronize
Invoke HostForm)

and Nothing works in the case of VB.

So you are saying that Null should work in this case?

yes

Best Regards,
Dustin Campbell
Developer Express Inc
 
Hi,
and Nothing works in the case of VB.

So you are saying that Null should work in this case?

From your response I take it that my assumptions were all accurate, so
unless the context in which the code is executed makes a difference, then
yes.

You can verify whether the constructor is throwing the ArgumentNullException
because of the null reference you are supplying by taking a peek at the
source code in Reflector:

"Lutz Roeder's Programming .NET" (first item in list is Reflector)
http://www.aisto.com/roeder/dotnet/
 
'Nothing' in VB is a strange beast - it is the equivalent of the C# 'null'
plus more.
If the parameter of the method is a value type, then 'Nothing' gets compiled
to be the default or freshly-constructed value type instance. If the value
type of the parameter is 'foo', then you can pass "new foo()" to get the same
result as the VB code.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
 
Hi Eric,

null will work, but Null wont. Remember: C# is case sensitive.
If null doesn't work, because the parameter is of a value-type, you will get
a compiler error. So if it compiles, yes it works.
If it really is a value-type, and you don't know the default value of that
type "new TypeName()" will work, because this gives the default value of the
type.
 
Thanks everyone for their input, what I have done is created a function in
VB where I send the OrderId and I call it from C#. It looks like the
underlying code is incorrect, because when I call it from VB, it still gives
me the error that I have been seeing in C#.
Thanks,
Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top