PC Review


Reply
Thread Tools Rate Thread

basic c# problem

 
 
dot Net Pa Ji
Guest
Posts: n/a
 
      21st Sep 2005
Hi guys,
I work mostly with vb.net and today only started doping some C#. Can
some translate the following vb code in C#
'--Here conn is connection object
If Not (conn Is Nothing) Then
'-----
End If


 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWFyayBSLiBEYXdzb24=?=
Guest
Posts: n/a
 
      21st Sep 2005
if (conn != null)
{
...do something
}

null is the c# equivalent of VB Nothing
! is c# equivalent of VB Not

Hope that helps
Mark R Dawson
http://www.markdawson.org



"dot Net Pa Ji" wrote:

> Hi guys,
> I work mostly with vb.net and today only started doping some C#. Can
> some translate the following vb code in C#
> '--Here conn is connection object
> If Not (conn Is Nothing) Then
> '-----
> End If
>
>
>

 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      21st Sep 2005
> If Not (conn Is Nothing) Then
> '-----
> End If
>

if (!(conn == null)){//so something
}

I would do it in VBNet as well as this bellow

If Not conn Is Nothing then
'do something
End if

Than it becomes more as Mark showed you.

I hope this helps,

Cor


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      21st Sep 2005
> null is the c# equivalent of VB Nothing

While that is true for reference types, Nothing is valid for value
types as well in VB. I *believe* that the default value for a member
variable of any particular type is the value of VB.NET's Nothing for
that same type - so for boolean it's false, for int it's 0 etc.

Not relevant in this particular example, but worth noting.

Jon

 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      21st Sep 2005
Hi Robinhood.

For your information in VB.Net is a difference between = Nothing and Is
Nothing.

= Nothing means for a value type empty or default
Is Nothing means for a reference type no reference.

By instance a string can have both

Setting to nothing is for both =

(Robin Hood because he came from Nothingham)

:-)

Cor

"Jon Skeet [C# MVP]" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed)...
>> null is the c# equivalent of VB Nothing

>
> While that is true for reference types, Nothing is valid for value
> types as well in VB. I *believe* that the default value for a member
> variable of any particular type is the value of VB.NET's Nothing for
> that same type - so for boolean it's false, for int it's 0 etc.
>
> Not relevant in this particular example, but worth noting.
>
> Jon
>



 
Reply With Quote
 
dot Net Pa Ji
Guest
Posts: n/a
 
      21st Sep 2005
Thanks for the information.
---DNPJ
"Cor Ligthert [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Robinhood.
>
> For your information in VB.Net is a difference between = Nothing and Is
> Nothing.
>
> = Nothing means for a value type empty or default
> Is Nothing means for a reference type no reference.
>
> By instance a string can have both
>
> Setting to nothing is for both =
>
> (Robin Hood because he came from Nothingham)
>
> :-)
>
> Cor
>
> "Jon Skeet [C# MVP]" <(E-Mail Removed)> schreef in bericht
> news:(E-Mail Removed)...
>>> null is the c# equivalent of VB Nothing

>>
>> While that is true for reference types, Nothing is valid for value
>> types as well in VB. I *believe* that the default value for a member
>> variable of any particular type is the value of VB.NET's Nothing for
>> that same type - so for boolean it's false, for int it's 0 etc.
>>
>> Not relevant in this particular example, but worth noting.
>>
>> Jon
>>

>
>



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      21st Sep 2005
Cor Ligthert [MVP] <(E-Mail Removed)> wrote:
> For your information in VB.Net is a difference between = Nothing and Is
> Nothing.
>
> = Nothing means for a value type empty or default
> Is Nothing means for a reference type no reference.
>
> By instance a string can have both


What makes a string different from other reference types though? That
doesn't make sense to me. Is it just to make it more like VB6?

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
david
Guest
Posts: n/a
 
      22nd Sep 2005
On 2005-09-21, Jon Skeet C# MVP <(E-Mail Removed)> wrote:
> Cor Ligthert [MVP] <(E-Mail Removed)> wrote:
>> For your information in VB.Net is a difference between = Nothing and Is
>> Nothing.
>>
>> = Nothing means for a value type empty or default
>> Is Nothing means for a reference type no reference.
>>
>> By instance a string can have both

>
> What makes a string different from other reference types though? That
> doesn't make sense to me. Is it just to make it more like VB6?


Pretty much. It's to make string compares work in a way similar to the
way they did in VB6. String comparisons are special-cased in VB.Net not
just for the reference/value type/empty means nothing/ issue, but
because VB also needs to support things like the Option Compare
statement, and there's some automatic culture stuff that's handled
during string compares (I doubt that one in a thousand VB developers
could tell you what String = String actually does in any detail.).

In VB.Net, for a string s

If s Is Nothing
works identically to if(s == null)

If s = Nothing

is equivalent to if(s == null || s == String.Empty)

Note this isn't polymorphic, you can't do

Dim s As Object = "Hello World"

If s = Nothing

That won't compile, because "= Nothing" is not valid for reference
types, except for Strings.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Another basic problem Niddeh Microsoft Excel Programming 1 10th Jul 2006 07:37 PM
Basic Problem Oli Microsoft Access 1 27th Apr 2004 11:48 PM
basic problem jay Microsoft Excel Worksheet Functions 2 4th Oct 2003 09:49 AM
basic problem kat Microsoft C# .NET 2 4th Aug 2003 08:53 PM
basic problem kat Microsoft Dot NET 1 4th Aug 2003 02:53 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:12 AM.