how to reference a collection object in another class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tatinccr
    New Member
    • Jul 2010
    • 3

    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:
    Code:
    namespace CashRegister.Reports
    {
         public partial class CustLevelReportPrint : Window
        {
          public CustLevelReportPrint(string zipfrom, string zipto, int catId,
             string custNumber, bool sortbyzip, bool sortbyname, bool sortbycat)
                   {
                     InitializeComponent();
                     Customers = CustLabels.LoadDatas(zipfrom, zipto, catId,     <--- HERE I FILL MY COLLECTION
                                          custNumber, sortbyzip, sortbyname, sortbycat);
                     this.DataContext = this;
                   }
     
             public List<CustLabels> Customers { get; set; }   <--- HERE I DECLARE THE COLLECTION
    }
     
    public class CustLabelConverter : IValueConverter
    {
          int i ,j = 0;
          StringBuilder Line1 = new StringBuilder();
          StringBuilder Line2 = new StringBuilder();
          StringBuilder Line3 = new StringBuilder();
          StringBuilder theLabelText = new StringBuilder();
          public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
         {     <--- HERE I NEED TO REFERENCE THE COLLECTION
    E.G  i = Customers.Count;
            string cad;
    
            if (value == null || !(value is CustLabels))
              {
                 if (theLabelText.ToString() == string.Empty)
                    return Binding.DoNothing;
                 else
                    return theLabelText.ToString();
             }
             if (i < 3)
             {
                 CustLabels val = value as CustLabels;
    
                 cad = (val.Name.ToString().Length > 50 ? val.Name.ToString().Substring(0, 50) : val.Name.ToString();
                 Line1.AppendFormat(cad + "\t");
                 cad = (val.Address.ToString().Length > 50 ? val.Address.ToString().Substring(0, 50) : val.Address.ToString());
                 Line2.AppendFormat(cad + "\t");
                 cad = val.City.ToString().Trim() + ", " + val.State.ToString().Trim() + ", " + val.ZipCode.ToString().Trim(); 
                 cad = (cad.Length > 50 ? cad.ToString().Substring(0, 50) : cad.ToString().PadRight(50));
                 Line3.AppendFormat(cad + "\t");
                 i++;
                 if (i == 3)
                 {
                    i = 0;
                    theLabelText.AppendFormat("{0}" + Line1.ToString(), Environment.NewLine);
                    theLabelText.AppendFormat("{0}" + Line2.ToString(), Environment.NewLine);
                    theLabelText.AppendFormat("{0}" + Line3.ToString(), Environment.NewLine);
                    Line1.Remove(0, Line1.Length);
                    return theLabelText.ToString();
                }
                else
                   return Binding.DoNothing;
               }
            else
             {
               i = 0;
               return theLabelText.ToString();
             }
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
           return Binding.DoNothing;
    }
    }
  • Sfreak
    New Member
    • Mar 2010
    • 64

    #2
    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.


    Code:
    public class Address
    {
        public string Street {get; set;}
        public string City   {get; set;}
    }
    public class Customer
    {
        public string        Name    {get; set;}
        public int           Age     {get; set;}
        public List<Address> Address {get; set;}
    }
    The usual operation is:
    
    public List<Address> AddressList()
    {
       List<Address> lst = new List<Address>();
       Address  address  = new Address();
       address.Street    = "Paranagua";
       address.City      = "Londrina";
       lst.Add(address);
    
       Address  address2  = new Address();
       address2.Street    = "Brazil";
       address2.City      = "Londrina";
       lst.Add(address2);
    
       return lst;
    }
    
    public void CreateCustomer()
    {
       Customer customer = new Customer();
       customer.Name     = "Fernando Mello";
       customer.Age      = 29;
       customer.Address  = AddressList();
    }
    I think you are trying to cast a third object into Address class and then bind into "customer.Addre ss" list right?

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

    Code:
    public class DTOCustomer
    {
       public string Name   {get; set;}
       public string Age    {get; set;}
       public string Street {get; set;}
       public string City   {get; set;}
       // and some other attributes
    }
    public Address AddressConverter(DTOCustomer dto)
    {
       Address address = new Address();
       address.Street  = dto.Street;
       address.City    = dto.City;
       return address;
    }
    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

    Comment

    • tatinccr
      New Member
      • Jul 2010
      • 3

      #3
      Originally posted by Sfreak
      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.


      Code:
      public class Address
      {
          public string Street {get; set;}
          public string City   {get; set;}
      }
      public class Customer
      {
          public string        Name    {get; set;}
          public int           Age     {get; set;}
          public List<Address> Address {get; set;}
      }
      The usual operation is:
      
      public List<Address> AddressList()
      {
         List<Address> lst = new List<Address>();
         Address  address  = new Address();
         address.Street    = "Paranagua";
         address.City      = "Londrina";
         lst.Add(address);
      
         Address  address2  = new Address();
         address2.Street    = "Brazil";
         address2.City      = "Londrina";
         lst.Add(address2);
      
         return lst;
      }
      
      public void CreateCustomer()
      {
         Customer customer = new Customer();
         customer.Name     = "Fernando Mello";
         customer.Age      = 29;
         customer.Address  = AddressList();
      }
      I think you are trying to cast a third object into Address class and then bind into "customer.Addre ss" list right?

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

      Code:
      public class DTOCustomer
      {
         public string Name   {get; set;}
         public string Age    {get; set;}
         public string Street {get; set;}
         public string City   {get; set;}
         // and some other attributes
      }
      public Address AddressConverter(DTOCustomer dto)
      {
         Address address = new Address();
         address.Street  = dto.Street;
         address.City    = dto.City;
         return address;
      }
      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
      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.
      Code:
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Windows;
      using System.Windows.Controls;
      using System.Windows.Data;
      using System.Windows.Documents;
      using System.Windows.Input;
      using System.Windows.Media;
      using System.Windows.Media.Imaging;
      using System.Windows.Shapes;
      using System.Data;
      using System.Data.SqlClient;
      using System.ComponentModel;
      using System.Globalization;
      using System.Collections;
      
      namespace CashRegister.Reports
      {
          /// <summary>
          /// Interaction logic for CustLevelReportPrint.xaml
          /// </summary>
          public partial class CustLabelReportPrint : Window
          {
              public CustLabelReportPrint()
              { }
      
              public CustLabelReportPrint(int zipfrom, int zipto, int catId,
                  string sortby)
              {
                  InitializeComponent();
                  Customers = CustLabels.LoadDatas(zipfrom, zipto, catId,
                      sortby);
                  this.DataContext = this;
              }
      
              public List<CustLabels> Customers { get; set; }
      
          }
      
          public partial class CustLabels : INotifyPropertyChanged
          {
              public event PropertyChangedEventHandler PropertyChanged;
      
              private string _name;
              public string Name
              {
                  get { return _name; }
                  set
                  {
                      _name = value;
                      if (PropertyChanged != null)
                      {
                          PropertyChanged(this, new PropertyChangedEventArgs("Name"));
                      }
                  }
              }
      
              private string _address;
              public string Address
              {
                  get { return _address; }
                  set
                  {
                      _address = value;
                      if (PropertyChanged != null)
                      {
                          PropertyChanged(this, new PropertyChangedEventArgs("Address"));
                      }
                  }
              }
      
              private string _city;
              public string City
              {
                  get { return _city; }
                  set
                  {
                      _city = value;
                      if (PropertyChanged != null)
                      {
                          PropertyChanged(this, new PropertyChangedEventArgs("City"));
                      }
                  }
              }
      
              private string _state;
              public string State
              {
                  get { return _state; }
                  set
                  {
                      _state = value;
                      if (PropertyChanged != null)
                      {
                          PropertyChanged(this, new PropertyChangedEventArgs("State"));
                      }
                  }
              }
      
              private string _zipcode;
              public string ZipCode
              {
                  get { return _zipcode; }
                  set
                  {
                      _zipcode = value;
                      if (PropertyChanged != null)
                      {
                          PropertyChanged(this, new PropertyChangedEventArgs("ZipCode"));
                      }
                  }
              }
      
             public CustLabels(string name, string address, string city, string state,
                   string zipcode)
              {
                  Name = name;
                  Address = address;
                  City = city;
                  State = state;
                  ZipCode = zipcode;
              }
      
              public static List<CustLabels> LoadDatas(int zipfrom, int zipto, int category,
                  string sortby)
              {
                  List<CustLabels> list = new List<CustLabels>();
                  string conn = Common.Common.GetConnectionString();
                  using (SqlConnection con = new SqlConnection(conn))
                  {
                      try
                      {
                          SqlCommand cmd = new SqlCommand("spCustomerLabelReport", con);
                          cmd.CommandType = CommandType.StoredProcedure;
                          //Parameters to Store Procedure
                          cmd.Parameters.Add("@Param_ZipCodeFrom", SqlDbType.Int);
                          cmd.Parameters["@Param_ZipCodeFrom"].Value = zipfrom;
                          cmd.Parameters.Add("@Param_ZipCodeTo", SqlDbType.Int);
                          cmd.Parameters["@Param_ZipCodeTo"].Value = zipto;
                          cmd.Parameters.Add("@Param_Category", SqlDbType.SmallInt);
                          cmd.Parameters["@Param_Category"].Value = category;
                          cmd.Parameters.Add("@Param_SortBy", SqlDbType.VarChar);
                          cmd.Parameters["@Param_SortBy"].Value = sortby;
                          
                          con.Open();
                          SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                          while (dr.Read())
                          {
                              CustLabels custreporting = new CustLabels(dr["name"].ToString().Trim(),
                                  dr["address"].ToString().Trim(), dr["city"].ToString().Trim(),
                                  dr["state"].ToString().Trim(), dr["zip"].ToString().Trim());
                              list.Add(custreporting);
                          }
                          
                          return list;
                      }
                      catch (SqlException ex)
                      {
                          throw (ex);
                      }
                  }
              }
          }
      
          public class CustLabelConverter : IValueConverter
          {
              int i = 0 ,j = 0, k = 4;  //k representa la cantidad de elementos en la coleccion
              // Customers. Como la referencio aqui???
              StringBuilder Line1 = new StringBuilder();
              StringBuilder Line2 = new StringBuilder();
              StringBuilder Line3 = new StringBuilder();
              StringBuilder theLabelText = new StringBuilder();
              public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
              {
                  string cad;
                  
                  if (value == null || !(value is CustLabels || value is IList))
                  {
                      if (theLabelText.ToString() == string.Empty)
                          return Binding.DoNothing;
                      else
                          return theLabelText.ToString();
                  }
                  if (i < 3)
                  {
                      //IList cant = value as IList;
                      CustLabels val = value as CustLabels;
      
                      k = val.Cant;
                      cad = (val.Name.ToString().Length > 50 ? val.Name.ToString().Substring(0, 50) : val.Name.ToString().PadRight(50));
                      Line1.AppendFormat(cad + "\t");
                      cad = (val.Address.ToString().Length > 50 ? val.Address.ToString().Substring(0, 50) : val.Address.ToString().PadRight(50));
                      Line2.AppendFormat(cad + "\t");
                      cad = val.City.ToString().Trim() + ", " + val.State.ToString().Trim() + ", " + val.ZipCode.ToString().Trim(); 
                      cad = (cad.Length > 50 ? cad.ToString().Substring(0, 50) : cad.ToString().PadRight(50));
                      Line3.AppendFormat(cad + "\t");
                      i++;
                      j++;
                      if (i == 3)
                      {
                          i = 0;
                          theLabelText.AppendFormat("{0}" + Line1.ToString(), Environment.NewLine);
                          theLabelText.AppendFormat("{0}" + Line2.ToString(), Environment.NewLine);
                          theLabelText.AppendFormat("{0}" + Line3.ToString(), Environment.NewLine);
                          theLabelText.AppendLine();
                          Line1.Remove(0, Line1.Length);
                          Line2.Remove(0, Line2.Length);
                          Line3.Remove(0, Line3.Length);
                          return Binding.DoNothing; // NtheLabelText.ToString();
                      }
                      else
                      {
                          if (j == k)
                          {
                              theLabelText.AppendFormat("{0}" + Line1.ToString(), Environment.NewLine);
                              theLabelText.AppendFormat("{0}" + Line2.ToString(), Environment.NewLine);
                              theLabelText.AppendFormat("{0}" + Line3.ToString(), Environment.NewLine);
                              Line1.Remove(0, Line1.Length);
                              Line2.Remove(0, Line2.Length);
                              Line3.Remove(0, Line3.Length);
                              return theLabelText.ToString();
                          }
                          else
                              return Binding.DoNothing;
                      }
                  }
                  else
                  {
                      i = 0;
                      return theLabelText.ToString();
                  }
              }
      
              public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
              {
                  return Binding.DoNothing;
              }
          }
      }

      Comment

      Working...