473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to reference a collection object in another class

Hi guys,
I have a collection in a class, and I have another class and I need to reference this collection in another class. How can I do that? This is my code:
Expand|Select|Wrap|Line Numbers
  1.  
  2. namespace CashRegister.Reports
  3. {
  4.      public partial class CustLevelReportPrint : Window
  5.     {
  6.       public CustLevelReportPrint(string zipfrom, string zipto, int catId,
  7.          string custNumber, bool sortbyzip, bool sortbyname, bool sortbycat)
  8.                {
  9.                  InitializeComponent();
  10.                  Customers = CustLabels.LoadDatas(zipfrom, zipto, catId,     <--- HERE I FILL MY COLLECTION
  11.                                       custNumber, sortbyzip, sortbyname, sortbycat);
  12.                  this.DataContext = this;
  13.                }
  14.  
  15.          public List<CustLabels> Customers { get; set; }   <--- HERE I DECLARE THE COLLECTION
  16. }
  17.  
  18. public class CustLabelConverter : IValueConverter
  19. {
  20.       int i ,j = 0;
  21.       StringBuilder Line1 = new StringBuilder();
  22.       StringBuilder Line2 = new StringBuilder();
  23.       StringBuilder Line3 = new StringBuilder();
  24.       StringBuilder theLabelText = new StringBuilder();
  25.       public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  26.      {     <--- HERE I NEED TO REFERENCE THE COLLECTION
  27. E.G  i = Customers.Count;
  28.         string cad;
  29.  
  30.         if (value == null || !(value is CustLabels))
  31.           {
  32.              if (theLabelText.ToString() == string.Empty)
  33.                 return Binding.DoNothing;
  34.              else
  35.                 return theLabelText.ToString();
  36.          }
  37.          if (i < 3)
  38.          {
  39.              CustLabels val = value as CustLabels;
  40.  
  41.              cad = (val.Name.ToString().Length > 50 ? val.Name.ToString().Substring(0, 50) : val.Name.ToString();
  42.              Line1.AppendFormat(cad + "\t");
  43.              cad = (val.Address.ToString().Length > 50 ? val.Address.ToString().Substring(0, 50) : val.Address.ToString());
  44.              Line2.AppendFormat(cad + "\t");
  45.              cad = val.City.ToString().Trim() + ", " + val.State.ToString().Trim() + ", " + val.ZipCode.ToString().Trim(); 
  46.              cad = (cad.Length > 50 ? cad.ToString().Substring(0, 50) : cad.ToString().PadRight(50));
  47.              Line3.AppendFormat(cad + "\t");
  48.              i++;
  49.              if (i == 3)
  50.              {
  51.                 i = 0;
  52.                 theLabelText.AppendFormat("{0}" + Line1.ToString(), Environment.NewLine);
  53.                 theLabelText.AppendFormat("{0}" + Line2.ToString(), Environment.NewLine);
  54.                 theLabelText.AppendFormat("{0}" + Line3.ToString(), Environment.NewLine);
  55.                 Line1.Remove(0, Line1.Length);
  56.                 return theLabelText.ToString();
  57.             }
  58.             else
  59.                return Binding.DoNothing;
  60.            }
  61.         else
  62.          {
  63.            i = 0;
  64.            return theLabelText.ToString();
  65.          }
  66. }
  67. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  68. {
  69.        return Binding.DoNothing;
  70. }
  71. }
  72.  
Jul 30 '10 #1
2 1357
Sfreak
64
You didnt define your CustLabels class there to show us where is the List attribute. I will show an usual example and tell me if is not the case.


Expand|Select|Wrap|Line Numbers
  1. public class Address
  2. {
  3.     public string Street {get; set;}
  4.     public string City   {get; set;}
  5. }
  6. public class Customer
  7. {
  8.     public string        Name    {get; set;}
  9.     public int           Age     {get; set;}
  10.     public List<Address> Address {get; set;}
  11. }
  12. The usual operation is:
  13.  
  14. public List<Address> AddressList()
  15. {
  16.    List<Address> lst = new List<Address>();
  17.    Address  address  = new Address();
  18.    address.Street    = "Paranagua";
  19.    address.City      = "Londrina";
  20.    lst.Add(address);
  21.  
  22.    Address  address2  = new Address();
  23.    address2.Street    = "Brazil";
  24.    address2.City      = "Londrina";
  25.    lst.Add(address2);
  26.  
  27.    return lst;
  28. }
  29.  
  30. public void CreateCustomer()
  31. {
  32.    Customer customer = new Customer();
  33.    customer.Name     = "Fernando Mello";
  34.    customer.Age      = 29;
  35.    customer.Address  = AddressList();
  36. }
I think you are trying to cast a third object into Address class and then bind into "customer.Address" list right?

Ok here we go. I suppose you have a DTO class (Data Transfer Object).

Expand|Select|Wrap|Line Numbers
  1. public class DTOCustomer
  2. {
  3.    public string Name   {get; set;}
  4.    public string Age    {get; set;}
  5.    public string Street {get; set;}
  6.    public string City   {get; set;}
  7.    // and some other attributes
  8. }
  9. public Address AddressConverter(DTOCustomer dto)
  10. {
  11.    Address address = new Address();
  12.    address.Street  = dto.Street;
  13.    address.City    = dto.City;
  14.    return address;
  15. }
And then you have a compatible class to bind into your original class. Try to see how to use DTO classes, it might help you.

I hope it can help you.

Fernando Mello
Jul 31 '10 #2
@Sfreak
Fernando,
Thank you very much for responding. I have tried to instantiate the class but I get other errors
always related to null references. I send you the complete code to see if you can help.
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Shapes;
  13. using System.Data;
  14. using System.Data.SqlClient;
  15. using System.ComponentModel;
  16. using System.Globalization;
  17. using System.Collections;
  18.  
  19. namespace CashRegister.Reports
  20. {
  21.     /// <summary>
  22.     /// Interaction logic for CustLevelReportPrint.xaml
  23.     /// </summary>
  24.     public partial class CustLabelReportPrint : Window
  25.     {
  26.         public CustLabelReportPrint()
  27.         { }
  28.  
  29.         public CustLabelReportPrint(int zipfrom, int zipto, int catId,
  30.             string sortby)
  31.         {
  32.             InitializeComponent();
  33.             Customers = CustLabels.LoadDatas(zipfrom, zipto, catId,
  34.                 sortby);
  35.             this.DataContext = this;
  36.         }
  37.  
  38.         public List<CustLabels> Customers { get; set; }
  39.  
  40.     }
  41.  
  42.     public partial class CustLabels : INotifyPropertyChanged
  43.     {
  44.         public event PropertyChangedEventHandler PropertyChanged;
  45.  
  46.         private string _name;
  47.         public string Name
  48.         {
  49.             get { return _name; }
  50.             set
  51.             {
  52.                 _name = value;
  53.                 if (PropertyChanged != null)
  54.                 {
  55.                     PropertyChanged(this, new PropertyChangedEventArgs("Name"));
  56.                 }
  57.             }
  58.         }
  59.  
  60.         private string _address;
  61.         public string Address
  62.         {
  63.             get { return _address; }
  64.             set
  65.             {
  66.                 _address = value;
  67.                 if (PropertyChanged != null)
  68.                 {
  69.                     PropertyChanged(this, new PropertyChangedEventArgs("Address"));
  70.                 }
  71.             }
  72.         }
  73.  
  74.         private string _city;
  75.         public string City
  76.         {
  77.             get { return _city; }
  78.             set
  79.             {
  80.                 _city = value;
  81.                 if (PropertyChanged != null)
  82.                 {
  83.                     PropertyChanged(this, new PropertyChangedEventArgs("City"));
  84.                 }
  85.             }
  86.         }
  87.  
  88.         private string _state;
  89.         public string State
  90.         {
  91.             get { return _state; }
  92.             set
  93.             {
  94.                 _state = value;
  95.                 if (PropertyChanged != null)
  96.                 {
  97.                     PropertyChanged(this, new PropertyChangedEventArgs("State"));
  98.                 }
  99.             }
  100.         }
  101.  
  102.         private string _zipcode;
  103.         public string ZipCode
  104.         {
  105.             get { return _zipcode; }
  106.             set
  107.             {
  108.                 _zipcode = value;
  109.                 if (PropertyChanged != null)
  110.                 {
  111.                     PropertyChanged(this, new PropertyChangedEventArgs("ZipCode"));
  112.                 }
  113.             }
  114.         }
  115.  
  116.        public CustLabels(string name, string address, string city, string state,
  117.              string zipcode)
  118.         {
  119.             Name = name;
  120.             Address = address;
  121.             City = city;
  122.             State = state;
  123.             ZipCode = zipcode;
  124.         }
  125.  
  126.         public static List<CustLabels> LoadDatas(int zipfrom, int zipto, int category,
  127.             string sortby)
  128.         {
  129.             List<CustLabels> list = new List<CustLabels>();
  130.             string conn = Common.Common.GetConnectionString();
  131.             using (SqlConnection con = new SqlConnection(conn))
  132.             {
  133.                 try
  134.                 {
  135.                     SqlCommand cmd = new SqlCommand("spCustomerLabelReport", con);
  136.                     cmd.CommandType = CommandType.StoredProcedure;
  137.                     //Parameters to Store Procedure
  138.                     cmd.Parameters.Add("@Param_ZipCodeFrom", SqlDbType.Int);
  139.                     cmd.Parameters["@Param_ZipCodeFrom"].Value = zipfrom;
  140.                     cmd.Parameters.Add("@Param_ZipCodeTo", SqlDbType.Int);
  141.                     cmd.Parameters["@Param_ZipCodeTo"].Value = zipto;
  142.                     cmd.Parameters.Add("@Param_Category", SqlDbType.SmallInt);
  143.                     cmd.Parameters["@Param_Category"].Value = category;
  144.                     cmd.Parameters.Add("@Param_SortBy", SqlDbType.VarChar);
  145.                     cmd.Parameters["@Param_SortBy"].Value = sortby;
  146.  
  147.                     con.Open();
  148.                     SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  149.                     while (dr.Read())
  150.                     {
  151.                         CustLabels custreporting = new CustLabels(dr["name"].ToString().Trim(),
  152.                             dr["address"].ToString().Trim(), dr["city"].ToString().Trim(),
  153.                             dr["state"].ToString().Trim(), dr["zip"].ToString().Trim());
  154.                         list.Add(custreporting);
  155.                     }
  156.  
  157.                     return list;
  158.                 }
  159.                 catch (SqlException ex)
  160.                 {
  161.                     throw (ex);
  162.                 }
  163.             }
  164.         }
  165.     }
  166.  
  167.     public class CustLabelConverter : IValueConverter
  168.     {
  169.         int i = 0 ,j = 0, k = 4;  //k representa la cantidad de elementos en la coleccion
  170.         // Customers. Como la referencio aqui???
  171.         StringBuilder Line1 = new StringBuilder();
  172.         StringBuilder Line2 = new StringBuilder();
  173.         StringBuilder Line3 = new StringBuilder();
  174.         StringBuilder theLabelText = new StringBuilder();
  175.         public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  176.         {
  177.             string cad;
  178.  
  179.             if (value == null || !(value is CustLabels || value is IList))
  180.             {
  181.                 if (theLabelText.ToString() == string.Empty)
  182.                     return Binding.DoNothing;
  183.                 else
  184.                     return theLabelText.ToString();
  185.             }
  186.             if (i < 3)
  187.             {
  188.                 //IList cant = value as IList;
  189.                 CustLabels val = value as CustLabels;
  190.  
  191.                 k = val.Cant;
  192.                 cad = (val.Name.ToString().Length > 50 ? val.Name.ToString().Substring(0, 50) : val.Name.ToString().PadRight(50));
  193.                 Line1.AppendFormat(cad + "\t");
  194.                 cad = (val.Address.ToString().Length > 50 ? val.Address.ToString().Substring(0, 50) : val.Address.ToString().PadRight(50));
  195.                 Line2.AppendFormat(cad + "\t");
  196.                 cad = val.City.ToString().Trim() + ", " + val.State.ToString().Trim() + ", " + val.ZipCode.ToString().Trim(); 
  197.                 cad = (cad.Length > 50 ? cad.ToString().Substring(0, 50) : cad.ToString().PadRight(50));
  198.                 Line3.AppendFormat(cad + "\t");
  199.                 i++;
  200.                 j++;
  201.                 if (i == 3)
  202.                 {
  203.                     i = 0;
  204.                     theLabelText.AppendFormat("{0}" + Line1.ToString(), Environment.NewLine);
  205.                     theLabelText.AppendFormat("{0}" + Line2.ToString(), Environment.NewLine);
  206.                     theLabelText.AppendFormat("{0}" + Line3.ToString(), Environment.NewLine);
  207.                     theLabelText.AppendLine();
  208.                     Line1.Remove(0, Line1.Length);
  209.                     Line2.Remove(0, Line2.Length);
  210.                     Line3.Remove(0, Line3.Length);
  211.                     return Binding.DoNothing; // NtheLabelText.ToString();
  212.                 }
  213.                 else
  214.                 {
  215.                     if (j == k)
  216.                     {
  217.                         theLabelText.AppendFormat("{0}" + Line1.ToString(), Environment.NewLine);
  218.                         theLabelText.AppendFormat("{0}" + Line2.ToString(), Environment.NewLine);
  219.                         theLabelText.AppendFormat("{0}" + Line3.ToString(), Environment.NewLine);
  220.                         Line1.Remove(0, Line1.Length);
  221.                         Line2.Remove(0, Line2.Length);
  222.                         Line3.Remove(0, Line3.Length);
  223.                         return theLabelText.ToString();
  224.                     }
  225.                     else
  226.                         return Binding.DoNothing;
  227.                 }
  228.             }
  229.             else
  230.             {
  231.                 i = 0;
  232.                 return theLabelText.ToString();
  233.             }
  234.         }
  235.  
  236.         public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  237.         {
  238.             return Binding.DoNothing;
  239.         }
  240.     }
  241. }
  242.  
Aug 1 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: David | last post by:
I have a windows form listbox in one class and I need to reference it in another class, eg. I need to add an item to the listbox. What is the correct way to setup the listbox and then set a...
0
by: Sebastian Hiller | last post by:
Hello, i'm new to .Net (i'm using VB as language and i'm working in the code-behind mode) and i can't solve the following problem: I have a WebForm and want to Add a UserControl...
2
by: Ian Gore | last post by:
Hi, I'm relatively new to VB.NET so I'd be grateful if someone could point out what I don't understand here... 1) Creating a strongly typed collection by inheriting CollectionBase. This is...
5
by: Jeff Cobelli | last post by:
I am trying to include two classes as members of another class in a webservice. The definitions look basically like this: Public Class clsLender Public ID As String Public Name As String End...
6
by: Jack | last post by:
Hello everyone, Just a general question. Is the Collection Object a dinosaur from VB6 with another more useful object preferred for VB.NET? Or, has its stature remained unchanged with .NET? ...
6
by: Scott M. Lyon | last post by:
As I mentioned in my other post, I'm attempting to, using COM Interop so I can update existing VB6 code to (for several specific functions) return a Hashtable from a .NET library. I've had...
5
by: Bryan | last post by:
I have a class 'TagType' with an ilist member 'Props' that holds a collection of another class called 'Prop'. I let the user create TagTypes and save multiple properties (Props) in them. I am...
5
by: Rich | last post by:
In VB6 I used to be able to add an object like a textbox and see the dropdown properties of that textbox from the collection object: Dim col as collection set col = new collection col.Add(txt1)...
1
by: Chris | last post by:
I have a class library which sits in a dll and adds controls to a aspx page which calls it. I have been passing it the page object. Is there any way I can reference the page from within the class...
16
by: Mike | last post by:
Hi, I have a form with some controls, and a different class that needs to modify some control properties at run time. Hoy can I reference the from so I have access to its controls and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.