encapsulation

i have a class in a webservice:

public class OMAccount
            {

                  [XmlElement("User")]
                  public string User;
                  [XmlElement("BizID")]
                  public string BizID;
                  [XmlElement("SecurityCID")]
                  public string SecurityCID;
                  [XmlElement("ScriptName")]
                  public string ScriptName;
                  [XmlElement("ID")]
                  public string ID;
                  [XmlElement("WebServiceError")]
                  public string WebServiceError;

public OMAccount()
                  {
                  }

            }

I want to have another class inside it  with 2 properties:
currency.Symbol
currency.Code

OMAccount test = new OMAccount();
test.currency.Symbol = "$";
test.currency.Code = "USD";

how do i do that?



            
LVL 1
alain123Asked:
Who is Participating?
 
gregoryyoungConnect With a Mentor Commented:
public class OMAccount
          {

               [XmlElement("User")]
               public string User;
               [XmlElement("BizID")]
               public string BizID;
               [XmlElement("SecurityCID")]
               public string SecurityCID;
               [XmlElement("ScriptName")]
               public string ScriptName;
               [XmlElement("ID")]
               public string ID;
               [XmlElement("WebServiceError")]
               public string WebServiceError;          ****************************************************
              public Currency Currency;
**********************************************************
               public class Currency
               {
                    private string symbol;
                   
                    public string Symbol
                    {
                         get
                         {
                              return symbol;
                         }
                         set
                         {
                              symbol=value;
                         }
                    }
               }
               
make sure to set currency = new Currency at some point ...
0
 
alain123Author Commented:
i already tried this, did not worked

public class OMAccount
            {

                  [XmlElement("User")]
                  public string User;
                  [XmlElement("BizID")]
                  public string BizID;
                  [XmlElement("SecurityCID")]
                  public string SecurityCID;
                  [XmlElement("ScriptName")]
                  public string ScriptName;
                  [XmlElement("ID")]
                  public string ID;
                  [XmlElement("WebServiceError")]
                  public string WebServiceError;
            
                  public class Currency
                  {
                        private string symbol;
                        
                        public string Symbol
                        {
                              get
                              {
                                    return symbol;
                              }
                              set
                              {
                                    symbol=value;
                              }
                        }
                  }
                  
0
 
alain123Author Commented:
hello,

that did not work...please can you explain?
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
gregoryyoungCommented:
did you remember to instantiate a currency object ?

foo.Currency = new Currency() before you accessed it ?

then you could do say foo.Currency.Symbol
0
 
alain123Author Commented:
Hello,

I create an instance of OMAccount, but  i don't have any currency in my objects..
0
 
Vaishali_chaudhryCommented:
Follow this code ,it works :

public class OuterClass
      {
            public String a;
            public String b;
            public InnerClass ob;
            public OuterClass()
            {
                  a="hello";
                  b="Hi";
                  ob=new InnerClass();
            }
            
                  public class InnerClass
                  {
                        public String p;
                        public String q;
                        public InnerClass()
                        {
                              p="in";
                              q="inn";
                        }
                        
                   }
            }



OuterClass obj=new OuterClass();
obj.a="outer aa";
obj.b="outer bb";
obj.ob.p="Inner pp";
obj.ob.q="Inner qq";
MessageBox.Show(obj.ToString());
0
 
alain123Author Commented:
Hello,

That's exactly what I'm looking for, now, when i try to do this --> obj.ob.q="Inner qq";  

I get an error" object is not set to an instance of an object"


public class OMSQL : System.Web.Services.WebService
      {
            public OMSQL()
            {
            
                  InitializeComponent();
            }

            #region Component Designer generated code
            
            //Required by the Web Services Designer
            private IContainer components = null;
                        
            
            #endregion

            public class OMAccount
            {
                  
                  public InnerClass ob;
                  
                  public OMAccount()
                  {
                              ob=new InnerClass();
                  }

                              public class InnerClass
                              {
                                    public String p;
                                    public String q;
                                    public InnerClass()
                                    {
                                          p="in";
                                          q="inn";
                                    }
                               
                              }


                  [XmlElement("User")]
                  public string User;
                  [XmlElement("BizID")]
                  public string BizID;
                  [XmlElement("SecurityCID")]
                  public string SecurityCID;
                  [XmlElement("ScriptName")]
                  public string ScriptName;
                  [XmlElement("ID")]
                  public string ID;
                  [XmlElement("WebServiceError")]
                  public string WebServiceError;
      }
      }




==============

On test.cs ==

path.OMSQL obj = new OMSQL();
obj.User = "test";
obj.ob.p = "test";  -- error
0
 
alain123Author Commented:
ob: <undefined value>
0
 
Vaishali_chaudhryCommented:
I will try that with webservices and then get back to you as the code i have given works just fine without any errors...
0
 
Vaishali_chaudhryCommented:
I created a webservice and tested the code,it runs fine.If you still get an error sent me the complete code.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Xml.Serialization;


namespace LearningService
{
      /// <summary>
      /// Summary description for Service1.
      /// </summary>
      [WebService(Namespace="http://microsoft.com/webservices/")]
      public class OMSQL : System.Web.Services.WebService
      {
            public OMSQL()
            {
         
                  InitializeComponent();
            }

            private void InitializeComponent()
            {

            }

            #region Component Designer generated code
         
            //Required by the Web Services Designer
            private IContainer components = null;
                   
         
            #endregion
            [WebMethod]
            public String ShowData()
            {
                  OMAccount obj=new OMAccount();

                  return obj.ob.p;
            }

            public class OMAccount
            {
               
                  public InnerClass ob;
               
                  public OMAccount()
                  {
                        ob=new InnerClass();
                  }

                  public class InnerClass
                  {
                        public String p;
                        public String q;
                        public InnerClass()
                        {
                              p="in";
                              q="inn";
                        }
                             
                  }

                  [XmlElement("User")]
                  public string User;
                  [XmlElement("BizID")]
                  public string BizID;
                  [XmlElement("SecurityCID")]
                  public string SecurityCID;
                  [XmlElement("ScriptName")]
                  public string ScriptName;
                  [XmlElement("ID")]
                  public string ID;
                  [XmlElement("WebServiceError")]
                  public string WebServiceError;
            }
      }

}
0
 
alain123Author Commented:
hello vai,

how are you calling the webservice?  i create an instance of OMAccount, then I do have OMAccount.User ..etc available but when I debug and get to OMAccount.ob.test1 =  "test";

ob says "undefined value", i don't undertand why..
0
 
alain123Author Commented:
obiously ob it's not getting instantiated for some reason, i would think that it would get instantiated at the moment that you call OMAccount?
0
 
Vaishali_chaudhryCommented:
I call it a webservice because OMSQL is inheriting from the WebService class.

And yes the innerclass object gets initialized as soon as you make an object of the outer class because we have called the innerclass public constructor there....i dont see the reason why u are getting errors .If you can post your code....i can help..
0
 
alain123Author Commented:
hello,

I still don't get it, it just does not creates an instance of the inner class, here is my code:
on my .cs
// create object
                        Company.com.test.OMAccount  OMAccount = new Company.com.test.OMAccount();
OMAccount.Currency.test1 =  "$";  --> problem is here
======

on the webservice
public class OMSQL : System.Web.Services.WebService
      {
            public OMSQL()
            {
            
                  InitializeComponent();
            }

            #region Component Designer generated code
            
            //Required by the Web Services Designer
            private IContainer components = null;
                        
      
            private void InitializeComponent()
            {
            }


            protected override void Dispose( bool disposing )
            {
                  if(disposing && components != null)
                  {
                        components.Dispose();
                  }
                  base.Dispose(disposing);            
            }
            
            #endregion


            [WebMethod]
            public string ShowData()
            {
                  OMAccount obj=new OMAccount();

                  return obj.Currency.test1;
            }
            


            public class OMAccount
            {
                  
                  public InnerClass Currency;
                  
                  public OMAccount()
                  {
                              Currency = new InnerClass();
                  }

                              public class InnerClass
                              {
                                    public String test1;
                                    public String test2;
                                    
                                    public InnerClass()
                                    {
                                          test1="in";
                                          test2="inn";

                                    }
                               
                              }
                           

                  [XmlElement("User")]
                  public string User;
                  [XmlElement("BizID")]
                  public string BizID;
                  [XmlElement("SecurityCID")]
                  public string SecurityCID;
                  [XmlElement("ScriptName")]
                  public string ScriptName;
                  [XmlElement("CID")]
                  public string CID;
                  [XmlElement("WebServiceError")]
                  public string WebServiceError;
                  [XmlElement("Symbol")]
                  public string Symbol;
                  [XmlElement("Code")]
                  public string Code;
            
            }
0
 
alain123Author Commented:
and if i do this

            Company.com.test.home.OMSQL OMDb = new Company.com.test.home.OMSQL();
                        string test = OMDb.ShowData();

i  get the value, but what i'm looking is to Set the value to it so it's available across the app.
0
 
Vaishali_chaudhryConnect With a Mentor Commented:
try writing a Accesser (get & set property)method for both the objects test1 and test2 in the inner class and then try doing it...I dont see the reason of err because a public inner class is logically same as a public outer class.

What is the error that it throws!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.