newb class def question

M

mp

I can't get the syntax right
I'm trying to make a base class MoldPart and sub classes, MoldSide, MoldEnd,
MoldBase etc

in the base class i have
public abstract class MoldPart

{

private string myId;

private double myWidth;

private double myLength;

private double myThickness;


public MoldPart(string sID, double dWidth, double dLength, double
dThickness)

{

Id = sID;

Width = dWidth;

Length = dLength;

Thickness = dThickness;

}

and implementations for width, length etc

in the sub class i can't get the syntax right to derive from base

what goes after the colon?
when I put "base" there I get an error
Error 1 An object reference is required for the non-static field, method, or
property 'MoldPart.Id.get' \

when i put "new base" there I get an error
Error 1 Keyword 'this' or 'base' expected

when I put "this" there i get an error:
Error 2 An object reference is required for the non-static field, method, or
property 'MoldPart.Width.get'

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) :
this (Id, Width, Length, Thickness)

{

myId = sId;

myWidth = dWidth;

myLength = dLength;

myThickness = dThickness;

}
 
A

Arne Vajhøj

public abstract class MoldPart

{

private string myId;

private double myWidth;

private double myLength;

private double myThickness;


public MoldPart(string sID, double dWidth, double dLength, double
dThickness)

{

Id = sID;

Width = dWidth;

Length = dLength;

Thickness = dThickness;

}

and implementations for width, length etc

in the sub class i can't get the syntax right to derive from base

what goes after the colon?
when I put "base" there I get an error
Error 1 An object reference is required for the non-static field, method, or
property 'MoldPart.Id.get' \

when i put "new base" there I get an error
Error 1 Keyword 'this' or 'base' expected

when I put "this" there i get an error:
Error 2 An object reference is required for the non-static field, method, or
property 'MoldPart.Width.get'

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) :
this (Id, Width, Length, Thickness)

{

myId = sId;

myWidth = dWidth;

myLength = dLength;

myThickness = dThickness;

}

It is not quite clear to me what you want.

But try look at this example:

using System;

namespace E
{
public abstract class MoldPart
{
public string PartNo { get; set; }
public double Width { get; set; }
public double Length { get; set; }
public double Thickness { get; set; }
public MoldPart(string partno, double width, double length,
double thickness)
{
this.PartNo = partno;
this.Width = width;
this.Length = length;
this.Thickness = thickness;
}
}
public class MoldSide : MoldPart
{
public bool LongSide { get; set; }
public MoldSide(string partno, double width, double length,
double thickness, bool longside) : base(partno, width, length, thickness)
{
this.LongSide = longside;
}
}
public class Program
{
public static void Main(string[] args)
{
MoldPart o = new MoldSide("#123", 2.0, 12.0, 0.01, true);
Console.WriteLine(o.PartNo + " : " + o.Width + " x " +
o.Length);
Console.ReadKey();
}
}
}

Arne
 
M

mp

Thanks I'll study your example
mark

Arne Vajhøj said:
public abstract class MoldPart

{

private string myId;

private double myWidth;

private double myLength;

private double myThickness;


public MoldPart(string sID, double dWidth, double dLength, double
dThickness)

{

Id = sID;

Width = dWidth;

Length = dLength;

Thickness = dThickness;

}

and implementations for width, length etc

in the sub class i can't get the syntax right to derive from base

what goes after the colon?
when I put "base" there I get an error
Error 1 An object reference is required for the non-static field, method,
or
property 'MoldPart.Id.get' \

when i put "new base" there I get an error
Error 1 Keyword 'this' or 'base' expected

when I put "this" there i get an error:
Error 2 An object reference is required for the non-static field, method,
or
property 'MoldPart.Width.get'

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)
:
this (Id, Width, Length, Thickness)

{

myId = sId;

myWidth = dWidth;

myLength = dLength;

myThickness = dThickness;

}

It is not quite clear to me what you want.

But try look at this example:

using System;

namespace E
{
public abstract class MoldPart
{
public string PartNo { get; set; }
public double Width { get; set; }
public double Length { get; set; }
public double Thickness { get; set; }
public MoldPart(string partno, double width, double length, double
thickness)
{
this.PartNo = partno;
this.Width = width;
this.Length = length;
this.Thickness = thickness;
}
}
public class MoldSide : MoldPart
{
public bool LongSide { get; set; }
public MoldSide(string partno, double width, double length, double
thickness, bool longside) : base(partno, width, length, thickness)
{
this.LongSide = longside;
}
}
public class Program
{
public static void Main(string[] args)
{
MoldPart o = new MoldSide("#123", 2.0, 12.0, 0.01, true);
Console.WriteLine(o.PartNo + " : " + o.Width + " x " +
o.Length);
Console.ReadKey();
}
}
}

Arne
 
M

mp

Thanks Peter,
I'll study this and try to figure out what I'm trying to do
:)
Mark

Peter Duniho said:
mp said:
[...]
what goes after the colon?
when I put "base" there I get an error
Error 1 An object reference is required for the non-static field, method,
or property 'MoldPart.Id.get' \

The "base" keyword would be the correct one, assuming I've interpreted
your aim correctly. However, passing the property values of "Id",
"Width", "Length", and "Thickness" as parameters to the base constructor
is not likely to be correct.

Rather, it would make more sense to simply pass the argument values from
the sub-class's constructor. E.g.:

public Side(string sId, double dWidth, double dLength, double
dThickness)
: this (sId, dWidth, dLength, dThickness)

Of course, there is also the question as to why you would provide a base
class that includes an implementation for those properties, and yet then
have the sub-class (apparently.unfortunately, without a complete class
definition, I'm having to make some assumptions here) re-implement those
properties.

It seems like, of the code for the "Side" class that you posted, private
fields and constructor method _body_ are all superfluous. You should need
only, given the code posted, to have an empty constructor body, where the
constructor declaration simply passes the constructor arguments down to
the base implementation (as I show above).

Pete
 

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