Solved

encapsulation

Posted on 2004-04-16
18
228 Views
Last Modified: 2011-04-14
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?



            
0
Comment
Question by:alain123
  • 9
  • 5
  • 2
18 Comments
 
LVL 1

Author Comment

by:alain123
Comment Utility
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
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 250 total points
Comment Utility
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
 
LVL 1

Author Comment

by:alain123
Comment Utility
hello,

that did not work...please can you explain?
0
 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
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
 
LVL 1

Author Comment

by:alain123
Comment Utility
Hello,

I create an instance of OMAccount, but  i don't have any currency in my objects..
0
 
LVL 2

Expert Comment

by:Vaishali_chaudhry
Comment Utility
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
 
LVL 1

Author Comment

by:alain123
Comment Utility
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
 
LVL 1

Author Comment

by:alain123
Comment Utility
ob: <undefined value>
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Expert Comment

by:Vaishali_chaudhry
Comment Utility
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
 
LVL 2

Expert Comment

by:Vaishali_chaudhry
Comment Utility
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
 
LVL 1

Author Comment

by:alain123
Comment Utility
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
 
LVL 1

Author Comment

by:alain123
Comment Utility
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
 
LVL 2

Expert Comment

by:Vaishali_chaudhry
Comment Utility
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
 
LVL 1

Author Comment

by:alain123
Comment Utility
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
 
LVL 1

Author Comment

by:alain123
Comment Utility
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
 
LVL 2

Assisted Solution

by:Vaishali_chaudhry
Vaishali_chaudhry earned 250 total points
Comment Utility
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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now