Solved

Cannot implicitly convert

Posted on 2011-02-24
8
619 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now