Solved

Cannot implicitly convert

Posted on 2011-02-24
8
621 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 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

809 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