C# Best practices for check objects and strings [closed] - 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 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#

Related

How to check array for a value in C#? [closed]

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'm trying to help someone out and this isn't my area of expertise so thought maybe someone can help me help someone else.
I have a field called Master that contains an array. I also have a field called Original that contains a string. I want to check if the string in Original is in the array field called Field1 and then with an if statement do something if true / false
"Original":"1234",
"Master":[{"ID":1,"Field1":12345},
{"ID":2,"Field1":123456},
{"ID":3,"Field1":1234},
{"ID":4,"Field1":12344}]
The array can be different each time and have a different amount of records in it.
Can anyone help me?
if Original and Master would be properties of the same class and in a variable named instance then you could use Linq to do:
bool isPresent = instance.Master.Any(entry => entry.Field1 == instance.Original);
Obviously you would need to first serialize the json to an instance of this class.

String NULL OR EMPTY [closed]

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.

Access part of the list in c# [closed]

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 8 years ago.
Improve this question
I have this two list here:
List<KeyValuePair<TextBox, KeyValuePair<string, Type>>> textbox1
So, i need that to get the Textbox i need to write:
textbox1.Key
What should i type to get the KeyValuePair<string, Type>> Type, like textbox1.Value.Value?
The type contains string or int.What i need is to access it's value so i can assign an if operator but i don't know how to. Then i need to modify it but that's my next step and i can arrange that myself.
I suppose textbox1.Key won´t compile since textbox1 is a List and therefor does not have any member called Key. Having said this the following may work for you:
textbox[i].Value.Value
Where i is the index of your KV-pair within the list (rather then the actual key)

Return a String to Controller from a View [closed]

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 8 years ago.
Improve this question
My project structure looks like this:
Solution-->Project-->Views-->Name.xaml-->Name.xaml.cs
-->Controllers-->NameController.cs
I'm trying to get string name from the Views folder. name is an .xaml file, and the string is in the back code.
I'm trying to do a simple get and return in NameController.cs to get string name. String name is the text from a textbox.
When I attempt this:
public string StringName
{
get
{
return Views.Name.Namebox.Text;
}
}
I get this error:
An object reference is required for the non-static field,
method, or property 'Project.Views.Name.Name'
Thank you for all of the help.
Edit:
Here's some pictures of my current code.
Textbox code--
Underlying code--
Controller code <-- This code in unfinished on purpose. It doesn't even see ProductId as an object.
The error is pretty clear about what's going on: you're trying to access an instance value as if it were a static one.
So the question now becomes:
How do I access an instance value?
The answer to which likely won't solve your problem:
Instance values can only be accessed via an instance of the object.
So in the end, what you're really asking is this:
How do I get an instance of my ProductIdView object?
You can go about it a number of different ways, but in the end it all comes down to you calling new ProductIdView() somewhere and then putting that reference someplace where it'll be in scope when you need it.

Strange C# syntax [closed]

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 8 years ago.
Improve this question
I keep seeing the following type of syntax:(string[])myList.ToArray(typeof(string));
What does it mean when the object type is declared at the front of the object in brackets, before calling a method on it?
I am struggling to locate explanations becuase I don't know what this setup would be called.
Any help appreciated.
Thanks
it's Called a Casting, Casting is usually a matter of telling the compiler that although it only knows that a value is of some general type, you know it's actually of a more specific type. For example:
object x="any string";
string s=(string)x;
if we are using the upper one then it may possible that it will through the exception at runtime like if you are using
object x="string";
int s=(int)x;
it will through the Exception at runtime unable to cast
but if you use as oprator then it will return a null rather then throwing an exception.
object x = new object();
string y = x as string; // Now y is null because x isn't a string

Categories