0

I'm suddenly stuck. I defined an object:

public class AddResult { public AddResult(string id, bool success) { this.ID = id; this.Success = success; } public string ID { get; set; } public bool Success { get; set; } public string ErrorMessage { get; set; } } 

I want to return this object in another method.

public AddResult Load(string userName, string password) { // validate if(userName = "") { AddResult.ErrorMessage = "wrong"; AddResult.ID = ""; } // want to return object AddResult } 

The intellisense is not working for AddResult.ID etc, I know something is wrong but need your advice.

5 Answers 5

3

You need to instantiate an object of the class AddResult, your code should look like this:

var addResult = new AddResult("", false) { ErrorMessage = "wrong" }; return addResult; 
Sign up to request clarification or add additional context in comments.

3 Comments

Should I define the method as public object Load(...) rather than public AddResult?
No the return type of the method should be of the instantiated object not a generic object.
I'd rather return an AddResult instance than an object because you won't be able to access its fields without casting otherwise.
0

You can use

AddResult.ID = ""; 

but in this case, ID must be static

Also, you can create object of AddResult class:

AddResult addResult = new AddResult(); 

And access ID from addResult object, like this:

addResult.ID = ""; 

Comments

0

Try This,

 public AddResult Load(string userName, string password) { AddResult addResult = null; // validate if (userName == "") { addResult = new AddResult("", false) { ErrorMessage = "wrong" }; } // want to return object AddResult return addResult; } 

Comments

0

you should define ID as static :

public static string ID { get; set; } 

and use it this way :

AddResult.ID = 40; 

Comments

0
var addResult = new AddResult("", false) { ErrorMessage = "wrong" }; return addResult as AddResult; 

2 Comments

Converting var to class object
I was suggesting that you actually edit your post with an explanation of how your code work. I do understand what it does.