Properties Question  
Author Message
Tdar





PostPosted: Visual C# General, Properties Question Top

I have a C# Class but I am not sure how to do something, I am also working with ClassDiagram's so if it can be done there instead of code let me know how.
this relates to shipping orders

Class:
ShipBoxes

Properties:
//First Package:
PieceNum
PackageWeight
PackageCOD
PackageDVal
//second package
PieceNum2
PackageWeight2
PackageCOD2
PackageDVal2

Is there a way to only have one set of properties then just add a second set of the
same Properties Instead of adding a "2" or "3" etc Soi can have unlimited number of packages if needed

I hope this makes sence.

Tdar






Visual C#10  
 
 
cablehead





PostPosted: Visual C# General, Properties Question Top

Just create another instance of your class:

Shipboxes a = new Shipboxes();

a.PieceNum = 1;

Shipboxes b = new Shipboxes();

b.PieceNum = 2;



 
 
Douglas H. Troy





PostPosted: Visual C# General, Properties Question Top

I don't know how (or if you can) actually 'hook' into the InfoPath context menu, but how about just handling the right-click on your control, and displaying your own context menu, with your property, and any others you need exposed ...

Just a thought ...



 
 
DellS





PostPosted: Visual C# General, Properties Question Top

If your ShipBoxes needs to contain a varying number of packages, here's what I would do:

1. Create a "Package" struct with all of the fields that you need to identify a package. It might look like this:

public struct Package
{
public int PieceNum;
public float Weight;
public bool COD;
public float DVal;
}

2. If your ShipBoxes has other properties in addition to the list of packages, go ahead and create it as a class. If you're in VS2005, your pakages property will be a generic list, declared like this: List<Package> Packages;. In VS2003, you'll have to use an ArrayList instead and cast each Package back and forth as an object.

3. If ShipBoxes contains ONLY packages, I wouldn't create a class, I just use either the generic list or the ArrayList instead.

To access each package, your code will look something like this:

Building the ShipBox list
2003
ArrayList ShipBox = new ArrayList();
Package pkg = new Package();
pkg.PieceNum = 1; ... - load package properties into pkg.
ShipBox.Add((Object) pkg);

2005
List<Package> ShipBox = new List<Package>();
Package pkg = new Package();
pkg.PieceNum = 1; ...-load package properties into pkg.
ShipBox.Add(pkg);

Walking through the ShipBox list - both versions of VS
foreach (Package pkg in ShipBox)
{
//do whatever you need to do with it.
}

-Dell



 
 
dreadjr





PostPosted: Visual C# General, Properties Question Top

Yeah above would be the best. But have a property named Packages in the ShipBox class

class ShipBox
{
public ShipBox()
{
packages = new List<Package>();
}
private List<Package> packages;
public List<Package> Packages
{
get { return packages; } set { packages = value; }
}
}




 
 
Tdar





PostPosted: Visual C# General, Properties Question Top

ok so something like this.. Just that I get errors with this code in the get and set area.

public class AddPackage
{

public struct Package
{
public int PieceNum;
public float weight;
public bool COD;
public float DVal;
}
public list shipbox()
{
List<Package> box = new List<Package>();
}
private List<Package> box;
public List<Package> box;
{
get { return box; }
set { box = value;}
}
}

Thanks,

Tdar


 
 
Reinout Waelput





PostPosted: Visual C# General, Properties Question Top

Make sure the private and the property don't have the same name and spelling. Either one of them should start with a capital :

private List<Package> box;
public List<Package> Box;
{
get { return box; }
set { box = value;}
}
}



 
 
Tdar





PostPosted: Visual C# General, Properties Question Top

Hi I did what you said I will get vs 2005 showing errors like:
-Invalid token '{' in class, struct, or interface member delcaration x 2
-Invalid token ';' in class struct or interface member delcaration X 1
-A namespace does not directly contain members such as field or methods x 1

public class AddPackage
{

public struct Package
{
public int PieceNum;
public float weight;
public bool COD;
public float DVal;
}
public list shipbox()
{
List<Package> Box = new List<Package>();
}
private List<Package> _Box;
public List<Package> Box;
{ //<-- error
get { return _Box;/*<-- error*/ }
set/*<-- error*/ { _Box = value;}
}
}/*<-- error*/


 
 
Nimrand





PostPosted: Visual C# General, Properties Question Top

You have some semicolons where there should be none:

public class AddPackage
{

public struct Package
{
public int PieceNum;
public float weight;
public bool COD;
public float DVal;
}
public list shipbox() //Return type looks wrong. Did you mean void or List<Package>
{
List<Package> Box = new List<Package>();
}
private List<Package> _Box;
public List<Package> Box //no Semicolon!
{
get { return _Box; }
set { _Box = value;}
}
}


 
 
Tdar





PostPosted: Visual C# General, Properties Question Top

I got it thanks all,

public class AddPackage
{
private Package _Box;

public Package Box
{
get { return _Box; }
set { _Box = value; }
}

public struct Package
{
public int PieceNum;
public float weight;
public bool COD;
public float DVal;
}
}