?
Solved

encapsulation

Posted on 2004-04-16
18
Medium Priority
?
237 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
Technology Partners: 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

762 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