Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

encapsulation

Posted on 2004-04-16
18
Medium Priority
?
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 5
  • 2
18 Comments
 
LVL 1

Author Comment

by:alain123
ID: 10845732
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 750 total points
ID: 10845759
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
ID: 10846375
hello,

that did not work...please can you explain?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:gregoryyoung
ID: 10847335
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
ID: 10863291
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
ID: 10875694
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
ID: 10879595
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
ID: 10879631
ob: <undefined value>
0
 
LVL 2

Expert Comment

by:Vaishali_chaudhry
ID: 10885368
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
ID: 10885453
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
ID: 10889262
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
ID: 10892779
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
ID: 10915329
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
ID: 10949222
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
ID: 10949327
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 750 total points
ID: 10956310
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

610 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