1

I am consuming a third party web service and I want to add value to match the service reference class, and i am not sure how to add value to the following:

in reference:

public partial class UserInfor: object, System.ComponentModel.INotifyPropertyChanged 
{
        private ABC[] listOfABCField;

        public ABC[] ListOfABC 
    {
            get {
                return this.listOfABCField;
            }
            set {
                this.listOfABCField = value;
                this.RaisePropertyChanged("ListOfABC");
            }
        }
}

public partial class ABC : object, System.ComponentModel.INotifyPropertyChanged 
{
    private string ipField;

private string fristNameField;

private string lastNameField;       

}

////////////////////////////////////////////////////// in my service.asmx file have tried to put value as below: in below code i got exception in line ABC[] abc=new ABC[0]; error code:(NullReferenceException)

    UserInfor user = new UserInfor();
    ABC[] abc=new ABC[0];
        abc[0].firstName= "petter";
        abc[0].lastName = "lee";

        user.ListOfABC = abc[1];

i also tried in below code i got exception in line user.ListOfABC[0] = abc; error code:(NullReferenceException)

    UserInfor user = new UserInfor();
    ABC abc=new ABC[0];
        abc.firstName= "petter";
        abc.lastName = "lee";

        user.ListOfABC[0] = abc;

any idea how to add abc to user class ? thank you in advance

2 Answers 2

3

This'll probably be easier if you use a List<> instead of an array. Change the property:

private List<ABC> listOfABCField;

public List<ABC> ListOfABC
{
    // etc.
}

Don't forget to initialize it in the class' constructor so it's not null:

public UserInfor()
{
    listOfABCField = new List<ABC>();
}

Then you can just add an object to it, which doesn't need any of the array syntax you were trying to use:

UserInfor user = new UserInfor();
ABC abc = new ABC();
abc.firstName= "petter";
abc.lastName = "lee";

user.ListOfABC.Add(abc);
Sign up to request clarification or add additional context in comments.

1 Comment

David thabk you so much ..But the thing is that I am consuming a third party web service and the reference class above is generated by import wsdl file ... preferablely not to change any structure ...
2

You are doing it wrong, first instantiate the array, if you know in advance how many items it would contain then specify that as well in the square brackets like:

ABC[] abc=new ABC[1]; // this array will contain 1 item maximum

now instantiate that item and then set values of properties :

    abc[0] = new ABC(); // instantiating first item of array which is at 0th index
    abc[0].firstName= "petter";
    abc[0].lastName = "lee";

If you don't know how many item would come in it, then go with @David's suggestion of using List<T>

2 Comments

thank you for your comment, but how about the user class ? how to add abc to user class?
user.ListOfABC[0] = abc;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.