VB.net to C#

G

Guest

I'm trying to convert this VB.net function to C#:

Public Overridable Function nbhdValidate() As Boolean
If ((strFrom <> "") Or (strTo <> "")) Then
Return False
Else
Return True
End If
End Function

This is what I'm trying:
public bool nbhdValidate(bool bVal)
{
if (strFrom = "" | strTo = "")
return false;
else
return true;
}

This is the error: Operator | cannot be applied to operands of type string:

Please advise.
 
T

Tim Wilson

public virtual bool nbhdValidate()
{
if ((strFrom != "") || (strTo != ""))
{
return false;
}
else
{
return true;
}
}
 
G

Guest

Thanks for the quick response Tim. Can you help with another issue?

In my class module on this line in the C# version:
Form myform = new frmMessage();
I get missing directive or assembly ref. I am using system.data.

This is the VB code that works:
Dim myform As frmMessage

myform = New frmMessage()
myform.Label1.Text = "Sending Mail..."
myform.Show()


This is the C# code I'm trying:
Form myform = new frmMessage();

frmMessage.Label1.Text = "Sending Mail...";
frmMessage.ShowDialog();
 
O

Octavio Hernandez

A shorter version:

public virtual bool nbhdValidate()
{
return strFrom == "" && strTo == "";
}

Function returns true if BOTH strFrom and strTo are empty.

Regards - Octavio
 
T

Tim Wilson

The line-by-line translation of the VB code would be...

frmMessage myform;
myform = new frmMessage();
myform.Label1.Text = "Sending Mail...";
myform.Show();

Also, make sure that "Label1" is actually accessible outside of the
"frmMessage" class.
 
G

Guest

Thanks again Tim. As you alluded there is an issue with label1. I've declared
it public like this:
public System.Windows.Forms.Label label1;
on this line:
myform.label1.Text = "Sending Mail...";
I get this error:
frmMessage does not contain a definition for label1.
Even though intellisence sees it (label1) OK

PLease advise, I've converted the whole module and this is the last build
error (I think).
 
G

Guest

Thanks, I appreciate the advice.

Octavio Hernandez said:
A shorter version:

public virtual bool nbhdValidate()
{
return strFrom == "" && strTo == "";
}

Function returns true if BOTH strFrom and strTo are empty.

Regards - Octavio
 
T

Tim Wilson

That should work, it seems. Is the definition for "frmMessage" in the same
project as the definition of the form that you're using to consume it? If
not try rebuilding the project that contains the definition for
"frmMessage". If they're in the same project then post the definition for
"frmMessage" and the code that you're using to create and display it. It may
be something simple here.
 
G

Guest

Sorry Tim, frmMessage is working now, but I forgot that I have to also
convert the properties, and now I'm getting this error:
; expected

It comes on the 1st bracket after set and get:
public string nbhdBody()
{
set {strBody = value;}
get {return strBody;}

}
Here's the VB.net code I'm trying to emulate that works:
Public Property nbhdBody() As String
Get
nbhdBody = strBody
End Get
Set(ByVal Value As String)
strBody = Value
End Set
End Property
Please advise
 
T

Tim Wilson

You need to remove the "method parenthesis" at the end of the property
definition...

public string nbhdBody
{
set {strBody = value;}
get {return strBody;}
}
 
C

Chris Dunaway

Tim said:
public virtual bool nbhdValidate()
{
if ((strFrom != "") || (strTo != ""))
{
return false;
}
else
{
return true;
}
}

This would be more concisely written as:

public virtual bool nbhdValidate()
{
return !((strFrom != "") || (strTo != ""));
}
 
J

Jon Skeet [C# MVP]

Chris Dunaway said:
This would be more concisely written as:

public virtual bool nbhdValidate()
{
return !((strFrom != "") || (strTo != ""));
}

Or more readably written as:

return (strFrom=="" && strTo=="");

There's no need for all the "nots" around there :)
 

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

Top