Solved

Cannot implicitly convert

Posted on 2011-02-24
8
625 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

617 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