Solved

Cannot implicitly convert

Posted on 2011-02-24
8
624 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 81 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
Industry Leaders: 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 81 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 81 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 175 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 82 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why use this lambda? 12 97
Include MS Database in Winforms Project using Intellilock 5 34
Inserting LocalDB Table to SQL Server C# 3 51
C# Change the instruction to user. 11 39
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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 Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

751 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