cannot implicity convert string to int [duplicate] - c#

This question already has answers here:
Cannot implicitly convert type 'string' to 'int'
(7 answers)
Closed 8 years ago.
TextBox1.Enabled = false;
finalpricebox.Items.Clear();
namebox.Items.Clear();
int current = 0;
pricebox.Items.Clear();
if (CheckBox1.Checked == true)
{
request.Navigate("http:----------" + TextBox1.Text);
}
else if (CheckBox1.Checked == false)
{
request.Navigate("http://----" + TextBox1.Text);
}
namebox.Focus();
while (!(request.ReadyState == WebBrowserReadyState.Complete))
{
Application.DoEvents();
}
WebClient tClient = new WebClient();
int resultnr = request.Document.GetElementById("searchResults_total").OuterText;
if (resultnr > 30)
{
resultnr = 30;
}
it says that cannot implicity convert string to int. on the line
int resultnr = request.Document.GetElementById("searchResults_total").OuterText;
if (resultnr > 30)
why do i get this error, i really hope someone can help me out

int resultnr = Convert.ToInt32(request.Document.GetElementById("searchResults_total").OuterText);

Recommend to use Int32.TryParse for safe side
int resultnr =0;
if(int.TryParse(request.Document.GetElementById("searchResults_total").OuterText,out resultnr )
{
if (resultnr > 30)
{
resultnr = 30;
}
}

Related

C# .NET Core 3.1 Windows Forms application: "Object reference not set to an instance of an object" only on target machine [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 6 months ago.
I try to make an application in C# running on .NET Core 3.1. While compiling, I get no errors and the application runs perfectly on the developed PC but if I publish the application and move it to another machine I get below error and I can not identify what is the cause.
I tried a few actions found on google (to update all visual studio, clear %appdata% files .)
"Object reference not set to an instance of an object
Void Program_Form_Load(Object sender, EventArgs)
at Application.Proogram_Form....... Program_Form.cs:Line 98
At line 98 I have:
private void Program_Form_Load(object sender, EventArgs e)
{
// MULTIPLE LINES
Load_Data.Load_To_CMBB_ Ldt = new Load_Data.Load_To_CMBB_();
Ldt.Load_Data_ToCombobox(dataGridView4, 0, Fabricant);
// MULTIPLE LINES
}
Which is calling:
public void Load_Data_ToCombobox(DataGridView dgw, int Row, ComboBox cmb)
{
string val = null;
List<string> l = new List<string>();
l.Clear();
for (int i = 0; i <= dgw.Rows.Count - 1; i++)
{
if (dgw[Row, i].Value != null)
{
val = dgw[Row, i].Value.ToString();
if (!l.Exists(x => x == val))
{
l.Add(val);
cmb.Items.Add(val);
}
}
}
cmb.SelectedIndex = cmb.Items.Count - 1;
}
Try this:
public void Load_Data_ToCombobox(DataGridView dgw, int Row, ComboBox cmb)
{
if(dgw?.Rows?.Count == 0) return; //fix#1, you can throw exception too
string val = null;
List<string> l = new List<string>();
l.Clear();
for (int i = 0; i <= dgw.Rows.Count - 1; i++)
{
if (dgw[Row, i].Value != null)
{
val = dgw[Row, i].Value.ToString();
if (!l.Exists(x => x == val)) { l.Add(val); cmb.Items.Add(val); }
}
}
cmb.SelectedIndex = cmb.Items?.Count == 0 ? 0: cmb.Items.Count - 1; //fix#2
}

Why does my Test not pass for passing a null List? [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 3 years ago.
I am working in C# building a form application. I have a method that takes in a type of List and returns a hashtable. I am trying to throw an argument exception if the the list is null. My program works the way it should and returns the results I need.
This is the error
Message: Expected: <System.Exception>
But was: <System.NullReferenceException: Object reference not set to an instance of an object.
at WordOccurenceCalculator.WordCalculator.CalculateOccurrences(List`1 List)
public void CalculateOccurrencesShouldThrowException() {
List<string> list = null;
WordCalculator calc = new WordCalculator();
Assert.Throws<Exception>(delegate { calc.CalculateOccurrences(list); });
}
public Hashtable CalculateOccurrences(List<string> List)
{
if ( List.Count == 0)
{
throw new System.ArgumentException("Invalid Input");
}
if (List == null)
{
throw new System.ArgumentException("Invalid Input");
}
Hashtable table = new Hashtable();
int counter = 1;
int j = 1;
for (int i = 0; i < List.Count; i++)
{
for(j = i + 1 ; j <= List.Count; j++)
{
if(j < List.Count){
if (List[i] == List[j])
{
counter++;
}
}
}
if (!table.Contains(List[i]))
{
table.Add(List[i], counter);
}
counter = 1;
}
return table;
}
you are checking null after trying to access count property of null list. You would get exception from there too. Try the bellow.
if (List == null)
{
throw new System.ArgumentException("Invalid Input");
}
if ( List.Count == 0)
{
throw new System.ArgumentException("Invalid Input");
}

Check if 2 buttons have the same background image [duplicate]

This question already has answers here:
How to compare two images?
(3 answers)
Closed 4 years ago.
C# - Windows Form Application
I have some troubles with a button BackgroundImage.
I want to check if 2 buttons have the same background image, i try this:
if (button1.BackgroundImage == button2.BackgroundImage)
MessageBox.Show("works!");
but is not working.
How can i check if 2 buttons have the same background image?
Well, then try this
private bool ImageCompareArray(Bitmap firstImage, Bitmap secondImage)
{
bool flag = true;
string firstPixel;
string secondPixel;
if (firstImage.Width == secondImage.Width
&& firstImage.Height == secondImage.Height)
{
for (int i = 0; i<firstImage.Width; i++)
{
for (int j = 0; j<firstImage.Height; j++)
{
firstPixel = firstImage.GetPixel(i, j).ToString();
secondPixel = secondImage.GetPixel(i, j).ToString();
if (firstPixel != secondPixel)
{
flag = false;
break;
}
}
}
if (flag == false)
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}
And usage
private void Form2_Load(object sender, EventArgs e)
{
if(ImageCompareArray((Bitmap)button1.BackgroundImage, (Bitmap)button2.BackgroundImage))
{
MessageBox.Show("Yes");
}
else
{
MessageBox.Show("No");
}
}
Try this
if(button1.Image == button2.Image)
{
MessageBox.Show("Works");
}

check a textboxcontent isnumeric or not [duplicate]

This question already has answers here:
Identify if a string is a number
(26 answers)
Closed 7 years ago.
how to check if the content of Textbox is numeric or not in C# ?
double montant = double.Parse(_controle.Text);
if ( || (double.Parse(_controle.Text) < 0))
{
Declanche_Erreur = true;
_controle.BackColor = System.Drawing.Color.White;
break;
return;
}
else
{
Declanche_Erreur = false;
_controle.BackColor = System.Drawing.Color.White;
}
You can use double.TryParse. If it succeeds, it will set montants value and return true
double montant = -1;
if (!double.TryParse(_controle.Text, out montant))
{
Declanche_Erreur = true;
_controle.BackColor = System.Drawing.Color.White;
return;
}
else
{
Declanche_Erreur = false;
_controle.BackColor = System.Drawing.Color.White;
}

c# Cannot implicitly convert type 'double' to System.Window.Forms.Textbox

I have this error code "Cannot implicitly convert type 'double' to System.Window.Forms.Textbox" and i'm not sure why i'm getting it. If some one could exlpain it it would be great and give me ideas on how to fix it please. I have looked at other post but still can not work it out.
Thank you for your help
int SumOfSquares(int txtSide1, int txtSide2)
{
txtSide1 *= txtSide1;
txtSide2 *= txtSide2;
return txtSide1 + txtSide2;
}
private void btnCalculate_Click(object sender, EventArgs e)
{
int Side1 = int.Parse(txtSide1.Text);
int Side2 = int.Parse(txtSide2.Text);
int SumLessOne = SumOfSquares(Side1, Side2) - 1;
if (SumOfSquares(Side1, Side2) > 50)
{
txtHypotenuse.Text = "Overflow";
}
else
{
txtHypotenuse.Text = "Safe";
}
txtHypotenuse.Text = Math.Sqrt(SumOfSquares(Side1 , Side2)); // this is the line the error is on
}
}
Add ToString()
txtHypotenuse.Text = Math.Sqrt(SumOfSquares(Side1 , Side2)).ToString();
txtHypotenuse.Text = Convert.ToString(Math.Sqrt(SumOfSquares(Side1 , Side2)));

Categories

Resources