Solved

encapsulation

Posted on 2004-04-16
18
231 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
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 250 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
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 250 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

Suggested Solutions

Title # Comments Views Activity
C# guarantee sql connection close 6 50
MediaHelp 4 26
C# Desktop Application 3 54
I need help adding validation to my MVC.Net view 6 5
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 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