1

I have one class like below. I want to send one Messageformat like this for one webservice, so I need to add values to Attributecollection and Input.

public class Messageformat
{
    public class Attributecollection
    {
        public string name { get; set; }

        public int id { get; set; }
    }

    public class Input
    {
        public string soid { get; set; }

        public string itemname { get; set; }

        public int qty { get; set; }
    }

    public class output
    {
        public string soid { get; set; }
    }
}

I want to add values to this class. I can call only one class at a time and add values to that class. Is there any way to call the class Messageformat and add values to sub classes through that object. I know its a nested class structure. I am not good in oop so can anyone share some idea about this?

1
  • Not sure but that thing can be achieved by Reflection. You need to write down reflection code to fill the subclasses of your class Filling Data objects using Reflection Commented Apr 17, 2013 at 8:06

5 Answers 5

1

You should make each class separate for definition. Then (WCF) you'll be able to create your DataContract and its specify its datamembers - the properties, you what to send in your messages:

  [DataContract]
  public class Messageformat
  {    
     [DataMember]
     public Attributecollection prop1;
     [DataMember]
     public Input prop2;
     [DataMember]
     public output prop3;
  } 

  [DataContract]
  public class Attributecollection
  {
     [DataMember]
     public string name { get; set; }
     [DataMember]
     public int id { get; set; }

  }

  [DataContract]
  public class Input
  {
      [DataMember]
      public string soid { get; set; }
      [DataMember]
      public string itemname { get; set; }
      [DataMember]
      public int qty { get; set; }
  }

  [DataContract]
  public class output
  {
      [DataMember]
      public string soid { get; set; }
  }

Then you should generate your Service Contract, where you can use your class

  [ServiceContract]
  public interface IService
  {
     [OperationContract]
     SendMessage(Messageformat message);
  }
Sign up to request clarification or add additional context in comments.

Comments

1

Your question is not that clear, but if you're asking how to populate data, you'll have to add what's called properties to your parent class. Do note that you don't have to use inner classes.

public class Attributecollection
{
    public string name { get; set; }
    public int id { get; set; }
}

public class Input
{
    public string soid { get; set; }
    public string itemname { get; set; }
    public int qty { get; set; }
}

public class output
{
    public string soid { get; set; }
}

public class Messageformat
{
    public Attributecollection MyAttributecollection { get; set; }
    public Input MyInput { get; set; }
    public output Myoutput { get; set; }
}

...

Messageformat test = new Messageformat
    {
        MyAttributecollection = new Attributecollection { name = "", id = 1 },
        MyInput = new Input { soid = "", itemname ="", qty = 1 },
        Myoutput = new output { soid = "" }
    };

Comments

1

From the looks/sounds of it, you want an instance of one class that exposes properties of the others:

public class Messageformat {

  public MessageFormat() {
    Attributes = new Attributecollection();
    Input = new Input();
    Output = new output();
  }

  public Attributecollection Attributes { get; set; }
  public Input Input { get; set; }
  public output Output { get; set; }
}

public class Attributecollection {
  public string name { get; set; }
  public int id { get; set; }
}

public class Input {
  public string soid { get; set; }
  public string itemname { get; set; }
  public int qty { get; set; }
}

public class output {
  public string soid { get; set; }
}

Then you can do:

var format = new MessageFormat();
format.Input.soid = "something";
format.Input.itemname = "something";

And so on. There, are lots of improvements to be had here, to be sure, but there you have it.

1 Comment

What if I did List<Messageformat> lmf = new List<Messageformat>(); How do I populate it than?
1

why are you doing it like this? Instead of nesting your classes, declare your classes at same level and create properties inside the MessageFormat of other classes i.e.

  public class Messageformat
  {
     public Attributecollection AtrributeCollection{get;set;}
     public Input Input{get;set;}
     public output Output{get;set;}
  }
  public class Attributecollection
  {
     public string name { get; set; }
     public int id { get; set; }
  }
  public class Input
  {
     public string soid { get; set; }
     public string itemname { get; set; }
     public int qty { get; set; }
  }
  public class output
  {
     public string soid { get; set; }
  }

This way, you can easily create an Object of MessageFormat and insert data of all the other three classes i.e

  [WebMethod]
  public string GetMessage()
  {
        Messageformat msgFrmt = new Messageformat();
        msgFrmt.AtrributeCollection = new Atrributecollection()
                                         { 
                                             name="test",
                                             id=1
                                         };

        msgFrmt.Input = new Input()
                            {
                               soid= "soid value",
                               itemname="item",
                               qty=10
                            };
        msgFrmt.Output = new output()
                             {
                                soid="soid value"
                             };

        return new JavaScriptSerializer().Serialize(msgFrmt);
  }

above is a simple example, how you can organize your classes and use them for any purpose. (above is an example of a webmethod, but you can do whatever you want)

1 Comment

What if I did List<Messageformat> lmf = new List<Messageformat>(); How do I populate it than?
1

Why not make the inner class and its properties static in its access level like this:

 class MessageFormat
 {
    public static class Attributecollection
    {
        public static string name { get; set; }

        public static int id { get; set; }
    }

    public static class Input
    {
        public static string soid { get; set; }

        public static string itemname { get; set; }

        public static int qty { get; set; }
    }

    public static class output
    {
        public static string soid { get; set; }
    }
 }

And then you could access the inner classes although without instantiation:

  MessageFormat.Attributecollection.id = 1;
  MessageFormat.Attributecollection.name = "test";
  MessageFormat.Input.itemname = "Soda";
  MessageFormat.Input.qty = 10;

Although it defeats the purpose of a nested class because supposedly an inner class of a nested class should be accessible only by the outer or parent class. But as per your requirement this code would meet that requirement.

Comments

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.