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: -
-
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;
-
}
-
}
-
2 1357
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. - 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.Address" list right?
Ok here we go. I suppose you have a DTO class (Data Transfer Object). - 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
@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. -
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;
-
}
-
}
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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?
...
|
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...
|
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...
|
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)...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |