Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am doing Api Testing in Ranorex , there is one Test-Case where i have to keep a check that empty string should not be acceptable and if in anycase it would be empty then the error should reflect about empty string and TC have to have fail
At what scenario the Null string or empty string could be utilize in best way , I already read some post on internet but i still have some doubts what to use when
public static void SetStringContent(string content)
{
_request.SetStringContent(content);
Report.Info(_category, string.Format("Request content (string) set to '{0}'.", content));
// Testcode for checking if String is Null or Empty it will reflect an Error.
if(String.IsNullOrEmpty(content))
{
Report.Error (_category,string.Format("is null or empty.",content));
}
else
{
Report.Info (_category,string.Format("(\"{0}\") is neither null nor empty.",content));
}
}
IsNullOrEmpty is a useful method that allows you to simultaneously test whether a String is null or its value is String.Empty.
or you can use this method if blank values can arrive in your api.
String.IsNullOrWhiteSpace(String) Method
It is true if the value parameter is null or Empty, or if value consists only of white space characters.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I work on Xamarin.Forms application and want to check all possible scenarios. For example i got this code:
User user= new User(); string token= string.Empty i got a instance of object user and string token in the ViewModel's Constructor. I call them like this:
user= await GetUser();token = await GetToken();
I want to check every possible return from this calls. For object check if is empty , is null or got data. for string is empty , is null or got data ? Also hint for array of object? How to organize this?
They return what you have defined in the method definition.
The following
Task<ReturnType> GetUser()
returns an object of type ReturnType.
To compare if an object is null:
user == null.
To compare if a string is null or empty:
string..IsNullOrEmpty(<yourstring>)
I suggest you to study OOP in C#
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I wonder why can't we just convert IEnumerable to string using ToString() ! I mean what is the underlying reason behind this.
Microsoft docs say " ToString() returns A string that represents the current object." What is this A string ? Is it a special property of the object ? Why int.ToString() works but IEnumerable.ToString() doesn't ?
An IEnumerable<char> is not neccessarily a string. Imagine you have some service that returns an infinite number of characters (e.g. a stream). As there´s no end of that stream and data flows endlessly you are not able to call ToString and materialize a string from it.
However ToString just returns a representation of the object, not its data. In case of an array for instance, the object is the collection of items, or more general just a container. What you expect is the data that is contained in that container.
So when calling myArray.Totring for example you don´t get { 1, 2, 3 }, but simply System.int[]. That´s what ToString returns if there is no override for the type: its type-name. The same happens in your case: there is no overrdie for ToString defined for char[] or List<char> or whatever, so the method falls back to use typeofObject.FullName.
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 4 years ago.
Improve this question
Say I have a base method GetSong() that fetches songs from a database, and two methods that call this - GetRockSong() and GetPopSong().
Both methods take a string input of the song name, then pass that input to the GetSong() method, along with a genre.
Should the base method be in charge of validating the string input?
I would think the first two methods should, but this would lead to repeating the exact same code (i.e. checking the string isn't empty).
Obviously I have used a hugely simplistic illustration, but the problem is pretty much the same.
Example code:
public Song GetRockSong(string title)
{
// could null check title here before calling the method?
return GetSong(title, "Rock");
}
public Song GetPopSong(string title)
{
// could null check title here before calling the method?
return GetSong(title, "Pop");
}
public Song GetSong(string title, string genre)
{
// example validation, if null checking title above
// then could just check genre here
if (!string.IsNullOrEmpty(title) && !string.IsNullOrEmpty(genre))
{
// fetch song logic here
}
// etc
}
I personally think if the validation is the same then do the check in the shared method.
This will allow for you to maintain the code easier and it is always best to have the method that does the heavy lifting also validate the values passed to it.
I have done similar thing and found that the amount of code I needed to write and maintain is half of what it could have been if I had put the validation in each method that called it.
I hope this helps!
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
In what line of this C#.NET code below would be the best way to print (Console.WriteLine()) the output of non-compatible type conversion:
string Start = "2757457";
int Middle = Convert.ToInt32(Start); // is it: Console.WriteLine(Middle)
int End = int.Parse(Start); // or is it: Console.WriteLine(End)
I don't think your question has anything to do with printing to the console and everything to do with should you use Parse or Convert. Assuming that's correct then you may find the following breakdown of Convert, Parse, & TryParse applicable. If it's not correct, clarify and I'll either edit my answer or delete as applicable.
Parse Takes a string and (assuming it is a number) outputs the number equivalent of it. It will throw an exception if the value is null, not a number, or outside the min/max range of Int.
Convert.ToInt32 Takes a string and (assuming it is a number) checks if it's null. If null it returns 0 otherwise it calls Parse.
TryParse Takes a string and if it's not a number returns false. If it is a number it'll return true. If it's null, it will return 0 in the out parameter (but return false as it's primary return value). If it is a number, it'll return the number as an out parameter.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Within string remove first or last null spaces.used this trim(),trimEnd(),trim("\n","\t") etc but not removed null.
Looks like you are not assigning value back to the textbox after Trim().
Only calling function Trim() or TrimEnd() will not take effect until you assign back it to textbox
string str = " This is a string ";
txtBox.Text = str.Trim();
You can also check for empty or null using IsNullOrEmpty
if(string.IsNullOrEmpty(str))
{
//Console.WriteLine("string is either null or empty");
}