Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Cannot implicitly convert

Posted on 2011-02-24
8
Medium Priority
?
631 Views
Last Modified: 2012-05-11
Hi, I'm new to c# and am trying to convert this code from VB but I am getting the following errors.
Thanks!

if (lblTotalItemNo.Text > 10)
  {
    Label1.Visible = true;

Error Operator '>' cannot be applied to operands of type 'string' and 'int'      

if (lblCount.Text = 0)  
            {      
                Label1.Visible = true;
                …
            }
Error Cannot implicitly convert type 'int' to 'string'
     
txtEmail.Text = Left(txtEmail.Text, Len(txtEmail.Text) - 2);

Error The name 'Left' does not exist in the current context      
Error The name 'Len' does not exist in the current context      

if (RTrim(lblStatus.Text) == "new")
{
  …
}
Error The name 'RTrim' does not exist in the current context

private int Validation()
{
  if (lblCount.Text == 0)   <----
 {
     if (Label1.Text == "A")                
    {  
        if (Label30.Text != "")
       {
            Amount1 = Label30.Text; <---
       }
       else
       {      
           Amount1 = 0.0;
        }
    }.......
Error Operator '==' cannot be applied to operands of type 'string' and 'int'      
Error Cannot implicitly convert type 'string' to 'double'      


0
Comment
Question by:avi7
8 Comments
 
LVL 8

Expert Comment

by:crysallus
ID: 34976743
int totalItemNo = 0;
Int32.TryParse(lblTotalItemNo.Text, out totalItemNo);
if (totalItemNo > 10)
    Label1.Visible = true;

int count = 0;
Int32.TryParse(lblCount.Text, out count);
if (count == 0)
    Label1.Visible = true;

Open in new window

That's for the first 2.
0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 34976756
1)
if (lblTotalItemNo.Text > 10)
=>
if (Convert.ToInt32(lblTotalItemNo.Text) > 10)

2)
if (lblCount.Text = 0)
=>
if (Convert.ToInt32(lblCount.Text) = 0)
or
if (lblCount.Text = "0")

3)
txtEmail.Text = Left(txtEmail.Text, Len(txtEmail.Text) - 2);
=>
txtEmail.Text = txtEmail.Text.Substring(txtEmail.Text.Length - 2)

4)
if (RTrim(lblStatus.Text) == "new")
=>
char[] carray = {' '};
if (lblStatus.Text.TrimEnd(carray) == "new")

5)
Amount1 = Label30.Text;
=>
Amount1 = Convert.ToDouble(Label30.Text);
0
 
LVL 16

Assisted Solution

by:Kalpesh Chhatrala
Kalpesh Chhatrala earned 324 total points
ID: 34976757
 

if (Convert.ToInt32(lblTotalItemNo.Text) > 10)
  {
    Label1.Visible = true;

if (Convert.ToInt32lblCount.Text) == 0)  
            {      
                Label1.Visible = true;

            }
      
 txtEmail.Text = Left(txtEmail.Text, Len(txtEmail.Text) - 2);  // you can create your customize function

if (lblStatus.Text.Trim() == "new")
{

}
Error The name 'RTrim' does not exist in the current context

private int Validation() 
{
  if (Convert.ToInt32(lblCount.Text) == 0)   
 {
     if (Label1.Text == "A")                
    {  
        if (Label30.Text != "") 
       {
            Amount1 = Convert.ToDouble(Label30.Text);
       }
       else
       {       
           Amount1 = 0.0;
        }
    }

Open in new window


Left Function
public static string Left(string text, int length)
{
if (length < 0)
throw new ArgumentOutOfRangeException(…);
else if (length == 0 || text.Length == 0)
return "";
else if (text.Length <= length)
return text;
else
return text.Substring(0, length);
}

Open in new window


0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Assisted Solution

by:Peter Kwan
Peter Kwan earned 324 total points
ID: 34976759
The second one should be:
if (lblCount.Text = 0)
=>
if (Convert.ToInt32(lblCount.Text) == 0)
or
if (lblCount.Text == "0")
0
 
LVL 8

Assisted Solution

by:crysallus
crysallus earned 324 total points
ID: 34976795
All the methods in the Convert class will throw exceptions if the textbox data isn't in a suitable format, which is why I suggested using TryParse, as with that method, you don't have to worry about catching exceptions.

Also, pkwan, should no. 3 be

txtEmail.Text = txtEmail.Text.Substring(0, txtEmail.Text.Length - 2);
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 700 total points
ID: 34976802
1. You need to convert strings to int/decimal before you can test it.  Something like this:

int totalItemNo = -1;
int.TryParse(lblTotalItemNo.Text, out totalItemNo);

if (totalItemNo > 10)
  {
    Label1.Visible = true;
    ....
  }

int count = -1;
int.TryParse(lblCount.Text, out count);
if (count = 0)  
            {      
                Label1.Visible = true;
                …
            }

decimal Amount1 = 0;
double.TryParse(Label30.Text, out Amount1);


2. Left, Length, and Trim are methods of string not a function by itself

txtEmail.Text = txtEmail.Text.Left(txtEmail.Text.Length - 2);

if (lblStatus.Text.Trim() == "new")


I hope this helps.
0
 

Author Comment

by:avi7
ID: 34976811
Thanks!
0
 
LVL 10

Assisted Solution

by:John Claes
John Claes earned 328 total points
ID: 34977412
kalpesh2804:
The conversion gives faults  : the String to Int cast of the variables is not done.

informaniac:
it indeed looks
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_26846820.html

I thinck the Code From kalpesh2804 is used to create that Question ;-)


avi7:
Very short answering on both of the Case:

When converting Code look for folowing generated issues

Automaticaly VB. Casts are not supported in c#
so
       if  lblCount.Text = 0
should be translated into some of next
       if ( lblCount.Text = "0")
OR
       if ( (int)lblCount.Text = 0)
OR
       int count;
       if (int.TryParse(lblCount.Text, out count) && count==0)


Returnvalues !

Sub Validation()
{
        Validation=1;
}

must be translated in

int Validation ()
{
       return 1;
}

There are more issues after conversion because of defaulted casts and syntaxing

regards

poor beggar
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question