newb advice on classes

M

mp

Hi all,

starting an app with an input form with 3 textboxes
length, width, depth (representing dimensions of a block of stone)
and a command button "calculate"

calculate will take the 3 entries and fill in variables representing sizes
of other objects
the objects are sides/ends/bottoms of a box whose interior dimensions would
contain the original values length,width,depth

my first iteration was just put variables in the form / do calculations /
process results
on second thought, it would be more OO to make a class representing the
original variables perhaps
and another class representing the resultant parts - or even one class per
part??? (side / end / bottom / etc)

below are the vars using just the form
how should i change this to make multiple classes - each holding it's
respective values?
do i create a new .cs file for each object or just define classes within the
form .cs?
any other advice on class structures(patterns?) in a simple scenario like
this?
I've seen multiple classes defined in one .cs file but coming from vb6 it's
foreign to me and not sure how that would be structured.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

//original variables
private double m_dStoneWidth =0;
private double m_dStoneLength =0;
private double m_dStoneDepth =0;

//resultant variables to be calculated

private double m_dBaseLinerWidth = 0;
private double m_dBaseLinerLength = 0;
private double m_dBaseShellWidth = 0;
private double m_dBaseShellLength = 0;

private double m_dSideLinerWidth = 0;
private double m_dSideLinerLength = 0;
private double m_dSideShellWidth = 0;
private double m_dSideShellLength = 0;

private double m_dEndLinerWidth = 0;
private double m_dEndLinerLength = 0;
private double m_dEndShellWidth = 0;
private double m_dEndShellLength = 0;


thanks for any info or sites that might help me get started.

Mark
 
M

mp

Thanks for the link,

Mr. Arnold said:

so based on that if i create an abstract class 'Moldpart'
then i could have classes like Side End Base that derive from MoldPart
and based on the example
public abstract class MoldPart

{

private string myId;

private double myWidth;

private double myLength;


//if this is the constructor

public MoldPart(string s, double Width, double Length, double Thickness)

{



so then i could have Side , End, Base be derived from MoldPart

public class Side : MoldPart

{

private string myId;

private double myWidth;

private double myLength;

private double myThickness;



public Side(string sId, double dWidth, double dLength, double dThickness) :
base(Id, Width, Length, Thickness)

{

myId = sId;

myWidth = dWidth;

myLength = dLength;

myThickness = dThickness;

}

but not sure how to finish that - the variables in the line ": base(Id,
Width, Length, Thickness)" are underlined i n red

i get the error:

Error 2 An object reference is required for the non-static field, method, or
property 'MoldPart.Id.get'

etc

not sure what that means
 
M

Mr. Arnold

mp said:
Thanks for the link,



so based on that if i create an abstract class 'Moldpart'
then i could have classes like Side End Base that derive from MoldPart
and based on the example
public abstract class MoldPart

{

private string myId;

private double myWidth;

private double myLength;


//if this is the constructor

public MoldPart(string s, double Width, double Length, double Thickness)

{



so then i could have Side , End, Base be derived from MoldPart

public class Side : MoldPart

{

private string myId;

private double myWidth;

private double myLength;

private double myThickness;



public Side(string sId, double dWidth, double dLength, double dThickness) :
base(Id, Width, Length, Thickness)

{

myId = sId;

myWidth = dWidth;

myLength = dLength;

myThickness = dThickness;

}

but not sure how to finish that - the variables in the line ": base(Id,
Width, Length, Thickness)" are underlined i n red

i get the error:

Error 2 An object reference is required for the non-static field, method, or
property 'MoldPart.Id.get'

etc

not sure what that means

It means that the object/class must be instantiated new in order to set
reference.

Myobject obj = new Myobject();

I also suspect that you might be able to use the 'Static' on the
property definition.


If a class has 'Static' methods in it, the class doesn't have to
instantiated new in order access a static method in a class.
 
M

mp

It means that the object/class must be instantiated new in order to set
reference.

Myobject obj = new Myobject();

I also suspect that you might be able to use the 'Static' on the property
definition.


If a class has 'Static' methods in it, the class doesn't have to
instantiated new in order access a static method in a class.

thanks again, and sorry for being dense. I'm still trying to see how to
modify my code to 'match' the example

the example in that situs you gave is thus:
public abstract class Shape
{
private string myId;

public Shape(string s)
{
Id = s; // calling the set accessor of the Id property
}

then in the derived class
public class Rectangle : Shape
{
private int myWidth;
private int myHeight;

public Rectangle(int width, int height, string id) : base(id)
{
myWidth = width;
myHeight = height;
}


so the part i'm not "getting" is...where would New go in that example or in
mine?
in the example:
public Rectangle(int width, int height, string id) : base(id)
in my attempt:
public Side(string sId, double dWidth, double dLength, double dThickness)
:base(Id, Width, Length, Thickness)

so i see the ":" means here's the base class this one is derived from - or
something to that effect....
where do I put New in that signature?

apologies for the beginner questions:
thanks
mark
 
M

Mr. Arnold

mp said:
thanks again, and sorry for being dense. I'm still trying to see how to
modify my code to 'match' the example

the example in that situs you gave is thus:
public abstract class Shape
{
private string myId;

public Shape(string s)
{
Id = s; // calling the set accessor of the Id property
}

then in the derived class
public class Rectangle : Shape
{
private int myWidth;
private int myHeight;

public Rectangle(int width, int height, string id) : base(id)
{
myWidth = width;
myHeight = height;
}


so the part i'm not "getting" is...where would New go in that example or in
mine?
in the example:
public Rectangle(int width, int height, string id) : base(id)
in my attempt:
public Side(string sId, double dWidth, double dLength, double dThickness)
:base(Id, Width, Length, Thickness)

so i see the ":" means here's the base class this one is derived from - or
something to that effect....
where do I put New in that signature?

apologies for the beginner questions:
thanks
mark

Rectangle rt = new Rectangle(); // an instance has been created.

<http://www.c-sharpcorner.com/Upload...11122005014357AM/AbstractClassesNMethods.aspx>
 
M

mp

Mr. Arnold said:
Rectangle rt = new Rectangle(); // an instance has been created.

<http://www.c-sharpcorner.com/Upload...11122005014357AM/AbstractClassesNMethods.aspx>
Thanks again
I guess I'm just being dense.
I know how to New an object in client code, once the object exists as a
defintion
what i'm not seeing is what to do with my example to allow it to build.
I can't New it yet as it won't even compile with that signature
it's the signature i'm not understanding how to fix.
on that site, the examples have no constructor in the base class - which
makes sense if they're not instantiable
in the help file there is a constructor so don't know if that's a mistake?
 
M

Mr. Arnold

mp said:
Thanks again
I guess I'm just being dense.
I know how to New an object in client code, once the object exists as a
defintion
what i'm not seeing is what to do with my example to allow it to build.
I can't New it yet as it won't even compile with that signature
it's the signature i'm not understanding how to fix.
on that site, the examples have no constructor in the base class - which
makes sense if they're not instantiable
in the help file there is a constructor so don't know if that's a mistake?

I guess you're still stuck at the compile error message.

an object reference is required for the non-static field

It's happened to other people so maybe you'll find your solution if you
start reading.

<http://www.google.com/#hl=en&source...d,+method,+or+pr&gs_rfai=&fp=467c3568f2eec009>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top