Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am enrolled in a beginner C# online course.
Learning about how to deal with coding errors is mostly for the student to resolve.
Visual Studio 2019 is the preferred course IDE.
Some VS error messages are easy while others are very tricky to understand and resolve.
I am working on an assessable item - so not posting the full program code here.
A key section of code is throwing error CS1501 - I have searched internet for solution though do not understand how those maters fit with my situation.
Key code section as follows:
int abc;
abc = Console.ReadLine(Convert.ToInt32());
Curly red lines in VS are under 'ToInt32' with error notes: int
Convert.ToInt32(bool value) (18+ overloads) CS1501 No overload
for method 'ToInt32' takes 0 arguments.
Thanks.
You have it the wrong way around
int abc;
abc = Convert.ToInt32(Console.ReadLine());
The long story
Console.ReadLine() returns a string
Convert.ToInt32 takes an input (of varying type) to return an int
If you look at all the overloads for Convert.ToInt32 you will soon notice there are no variants that take 0 arguments. Which is what the compiler is telling you.
No overload for method 'ToInt32' takes 0 arguments.
However, never use Convert.ToInt32 to parse user input
Use int.TryParse instead
Converts the string representation of a number to its 32-bit signed
integer equivalent. A return value indicates whether the operation
succeeded.
int abc;
var input = Console.ReadLine();
if(!int.TryParse(input, out abc))
// user couldn't type a number, do something else instead
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am using IndexOf() and it does not work.
The code is:
if (sqlex.Message.IndexOf("Critical") > 0)
and Message does contain the word - Critical (see image), but does not equate to true. It takes the false path.
I also put in test code:
int index = sqlex.Message.IndexOf("Critical");
and index is = 0.
Why?
The first position in the string is 0, not 1. So if your string is
Critical Error - do not continue. Contact IT...
And you search for Critical, then 0 is the expected result.
If the string is not found, the result will be -1 ... unless the string is also empty, so make sure to check that, too.
The method string.IndexOf returns -1 if the character or string is not found. 0 means it is found. Change your code to if (sqlex.Message.IndexOf("Critical") >= 0)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
(beginner here, I'm learning c#) I've just learned about equality operators and was testing one out. For some reason unbeknownst to me (it's probably some really simple mistake I'm overlooking) I'm getting an error. Here's the code:
string number = "number";
number == "number";
I'm getting an error for the line, number == "number". To my knowledge, when I run it, "true" should be printed. Thanks for helping out a beginner, I'll probably be kicking myself once I know the answer.
In the second line you use the equality operator ==. You correctly understand that the equality operator == returns true if its operands are equal, false otherwise. Thus, it returns a value of type bool. But to output the result of this operation to the console, you should use the method Console.WriteLine. So you should first save this value in a variable and then output the value of this variable to the console. This can be done like this:
string number = "number";
bool equalityComparisonResult = number == "number";
Console.WriteLine(equalityComparisonResult);
Or you can do without an intermediate variable and print the result of the equality directly to the console:
string number = "number";
Console.WriteLine(number == "number");
string number = "number";
number == "number"? Console.WriteLine("numbers are equal"): Console.WriteLine("numbers are different");
or you can use Equals Method:
string number = "number";
number.Equals("number")
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Given the Following code:
List<Digit> tempDigits = input.Digits;
string test = tempDigits.ToString(); // -> {2,2,7}
tempDigits.Reverse();
string test2 = tempDigits.ToString(); // -> {2,2,7}
This is my code:
and that the result (in between, I renamed the variable according to naming convention as suggested by Thiessen)
For some reason, the reverse soes not seem to do its job.
It must be something really simple. But I cant get what the Issue is. I hope, someone can help out.
The code you've posted would work correctly, in a vacuum. (Except, in what world does List<>.ToString() produce "{2,2,7}"?)
My main guess would be, as Lesiak commented, that this.Digits and input.Digits points to the exact same List<> instance. So Digits1.Reverse() reverses the list and Digits2.Reverse() reverses the reversal, putting it back how it started. Perhaps this and input are the same thing. Or perhaps they are two different things that happen to use the same underlying Digits list. Who knows?
There are many other possibilities, some of which would be impossible to guess at based on the information provided. For example:
Maybe the input control that's giving you that list is trying to actively manage the list, and reorders it as soon as it notices it's been reversed.
Maybe you're not using the System.Collections.Generic.List<> type, but instead you're referencing some other namespace where someone has written a List<> type that doesn't behave how you'd think it would. Maybe it doesn't even implement Reverse(), so the compiler is picking up on the Enumerable.Reverse() extension method, which does nothing to mutate the underlying data.
It really is impossible to know for sure without knowing more about your environment.
The information you give is incomplete to address the issue.
A reproducible example from the information you provide does not show the same symptoms.
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
List<int> digits = new List<int> {2,2,7};
foreach(int digit in digits)
Console.Write(digit);
Console.WriteLine();
digits.Reverse();
foreach(int digit in digits)
Console.Write(digit);
Console.WriteLine();
}
}
Which gives the expected output
227
722
https://dotnetfiddle.net/Hs9H0k
My guess would be, this.Digits is not a mutable reference. But that is just an hypothesis.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
The static Matches is
public static void Matches(string expectedRegexPattern, string actualString);
What is the difference between when I pass expectedRegexPattern value instead of actualString value and passing actualString value instead of expectedRegexPattern value?
Way 1 - Assert.Matches("EN", result[0].LanguageCode);
Way 2 - Assert.Matches(result[0].LanguageCode,"EN");
Both of ways doing same work with same performance. So am confused about the difference between the above ways and which one is best?
#Stivi correctly points out a significant difference in the case of Matches, but there's another less significant but still important distinction even for Equals and other assertion methods.
You might get an incorrect message in the test log output. Many testing frameworks will log an error such as
Values do not match: expected: {expectedValue}, actual: {actualValue}".
So if you switched them, you'd see the wrong "expected" value, potentially confusing someone trying to diagnose a problem.
Take this test as an example:
void TestValueIsZero():
{
int value = 1;
Assert.Equals(0, value);
// logs "Values to not match. expected: 0, actual: 1
Assert.Equals(value, 0);
// logs "Values to not match. expected: 1, actual: 0
}
Someone looking at the failed test logs might be confused by the incorrect "expected" and "actual" values in the log due to the switched parameters.
The mechanics may be exactly the same regardless of the order of parameters (compare the two objects and return true if no differences are found), but the semantics are still important.
I expect that you are using xunit as test framework because i have found that method there. So if you only want to check if both strings have the same content you could also use Assert.Equal("Your text", "Your text"). The Assert.Matches method is used to validate if your text is in a desired format. The format is described by .net regular expressions. For example with this call Assert.Matches("^[0-9]$", "1") you can check if your string is a number between 0 and 9. If you change the order of the parameters to Assert.Matches( "1", "^[0-9]$") the method will throw an exception and your test will be marked as failed.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am working on a little project, that at a certain point need to convert a string to int.
I tried the following things:
//first try
value1 = int.TryParse(value[0].tostring(), out i)
//second
value1 = Convert.ToInt32(value[0].tostring())
//third
value1 = int.Parse(value[0].tostring())
I even wrote my own conversion method because I was at a loss.
The values I am trying to convert are queried from a MySQL database.
Thank for your help
EDIT:
I know tryparse should have 2 params.
And the error iam getting is a formatexception
Input string was not in a correct format.
I got that on all the tries.
the value in my test case is 2500
Keep in mind that that number is received from a db
I tried the above snippets while using a hard coded value. And that works fine.
EDIT 2:
//http://imgur.com/NSyg2rJ
One:
The signature of int.TryParse() is incorrect:
int result = 0;
bool parsedSuccessfully = Int32.TryParse(value[0].ToString(), out result);
//If successful, result will hold the value.
Two:
The signature of Convert.ToInt32() is correct, minus the case of the .ToString() method. This is failing because value or value[0] is null, or it cannot convert something like the string "NotAnInt" to an int.
Three:
The signature of int.Parse() is correct, but is failing for the same reasons as two.
Try this -
int value1;
if (Int.TryParse(value[0].ToString(), out value1))
{
//conversion successful
}