constructors/static methods and inheritance query

  • Thread starter Thread starter Aaron Watters
  • Start date Start date
A

Aaron Watters

A C# question about constructors/static methods and inheritance:
Please help me make my code simpler!

For fun and as an exercise I wrote somewhat classical B-tree
implementation
in C# which I later ported to java and Python for comparison.

[ See http://bplusdotnet.sourceforge.net/ for full details and code
]

The basic tree implementation has a number of subclasses which are
very
similar except for the types of certain data elements and the return
types of certain static methods. The only way I could think of to
implement these involved a lot of cutting and pasting. For example,
from hBplusTree.cs

public class hBplusTree : BplusTree
{
hBplusTreeBytes xtree;
public hBplusTree(hBplusTreeBytes tree) : base(tree)
{
this.xtree = tree;
}
.....
public static new hBplusTree Initialize(string treefileName,
string blockfileName, int PrefixLength, int CultureId,
int nodesize, int buffersize)
{
hBplusTreeBytes tree = hBplusTreeBytes.Initialize(treefileName,
blockfileName, PrefixLength, CultureId, nodesize, buffersize);
return new hBplusTree(tree);
}
public static new hBplusTree Initialize(string treefileName, string
blockfileName,
int PrefixLength, int CultureId)
{
hBplusTreeBytes tree = hBplusTreeBytes.Initialize(treefileName,
blockfileName, PrefixLength, CultureId);
return new hBplusTree(tree);
}
.....
public static new hBplusTree ReOpen(System.IO.Stream treefile,
System.IO.Stream blockfile)
{
hBplusTreeBytes tree = hBplusTreeBytes.ReOpen(treefile, blockfile);
return new hBplusTree(tree);
}
public static new hBplusTree ReOpen(string treefileName, string
blockfileName)
{
hBplusTreeBytes tree = hBplusTreeBytes.ReOpen(treefileName,
blockfileName);
return new hBplusTree(tree);
}
public static new hBplusTree ReadOnly(string treefileName, string
blockfileName)
{
hBplusTreeBytes tree = hBplusTreeBytes.ReadOnly(treefileName,
blockfileName);
return new hBplusTree(tree);
}
.....

and from xBplusTree.cs

public class xBplusTree : BplusTree
{
xBplusTreeBytes xtree;
public xBplusTree(xBplusTreeBytes tree) : base(tree)
{
this.xtree = tree;
}
.....
public static new xBplusTree Initialize(string treefileName,
string blockfileName, int PrefixLength, int CultureId,
int nodesize, int buffersize)
{
xBplusTreeBytes tree = xBplusTreeBytes.Initialize(treefileName,
blockfileName, PrefixLength, CultureId,
nodesize, buffersize);
return new xBplusTree(tree);
}
public static new xBplusTree Initialize(string treefileName,
string blockfileName, int PrefixLength, int CultureId)
{
xBplusTreeBytes tree = xBplusTreeBytes.Initialize(treefileName,
blockfileName, PrefixLength, CultureId);
return new xBplusTree(tree);
}
.....
public static new xBplusTree ReOpen(System.IO.Stream treefile,
System.IO.Stream blockfile)
{
xBplusTreeBytes tree = xBplusTreeBytes.ReOpen(treefile, blockfile);
return new xBplusTree(tree);
}
public static new xBplusTree ReOpen(string treefileName, string
blockfileName)
{
xBplusTreeBytes tree = xBplusTreeBytes.ReOpen(treefileName,
blockfileName);
return new xBplusTree(tree);
}
public static new xBplusTree ReadOnly(string treefileName,
string blockfileName)
{
xBplusTreeBytes tree = xBplusTreeBytes.ReadOnly(treefileName,
blockfileName);
return new xBplusTree(tree);
}
.....

(look familiar?)

Without going into great detail the Python implementation is much
simpler
(or at least shorter) than this due to Python's very relaxed type
system.

My question is: Is there a better way to do this? Please inform!
Thanks,
-- Aaron Watters

===
Let's not elect him in 2004 either.
 
Aaron,

I am kind of curious why so much of these members are static. I mean,
most of these members act on an instance, and it defeats the whole purpose
of OO to have it in this manner.

If you need the methods to return specific sub types, then you will have
to create a specialized instance for each type. This sucks, I know, but
it's the only way to do it in .NET 1.1 and before.

However, in 2.0, you will be able to define this as a generic type, and
have it perform operations and return that type from methods, when you
declare an instance of that type.

Also, as a side note, you have a tremendous number of naming violations
(type names, member names, parameter names, mostly casing errors) on your
publically exposed members. I would recommend adhering to the naming
convention for the .NET framework. It just makes it easier all around for
everyone IMO.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Aaron Watters said:
A C# question about constructors/static methods and inheritance:
Please help me make my code simpler!

For fun and as an exercise I wrote somewhat classical B-tree
implementation
in C# which I later ported to java and Python for comparison.

[ See http://bplusdotnet.sourceforge.net/ for full details and code
]

The basic tree implementation has a number of subclasses which are
very
similar except for the types of certain data elements and the return
types of certain static methods. The only way I could think of to
implement these involved a lot of cutting and pasting. For example,
from hBplusTree.cs

public class hBplusTree : BplusTree
{
hBplusTreeBytes xtree;
public hBplusTree(hBplusTreeBytes tree) : base(tree)
{
this.xtree = tree;
}
....
public static new hBplusTree Initialize(string treefileName,
string blockfileName, int PrefixLength, int CultureId,
int nodesize, int buffersize)
{
hBplusTreeBytes tree = hBplusTreeBytes.Initialize(treefileName,
blockfileName, PrefixLength, CultureId, nodesize, buffersize);
return new hBplusTree(tree);
}
public static new hBplusTree Initialize(string treefileName, string
blockfileName,
int PrefixLength, int CultureId)
{
hBplusTreeBytes tree = hBplusTreeBytes.Initialize(treefileName,
blockfileName, PrefixLength, CultureId);
return new hBplusTree(tree);
}
....
public static new hBplusTree ReOpen(System.IO.Stream treefile,
System.IO.Stream blockfile)
{
hBplusTreeBytes tree = hBplusTreeBytes.ReOpen(treefile, blockfile);
return new hBplusTree(tree);
}
public static new hBplusTree ReOpen(string treefileName, string
blockfileName)
{
hBplusTreeBytes tree = hBplusTreeBytes.ReOpen(treefileName,
blockfileName);
return new hBplusTree(tree);
}
public static new hBplusTree ReadOnly(string treefileName, string
blockfileName)
{
hBplusTreeBytes tree = hBplusTreeBytes.ReadOnly(treefileName,
blockfileName);
return new hBplusTree(tree);
}
....

and from xBplusTree.cs

public class xBplusTree : BplusTree
{
xBplusTreeBytes xtree;
public xBplusTree(xBplusTreeBytes tree) : base(tree)
{
this.xtree = tree;
}
....
public static new xBplusTree Initialize(string treefileName,
string blockfileName, int PrefixLength, int CultureId,
int nodesize, int buffersize)
{
xBplusTreeBytes tree = xBplusTreeBytes.Initialize(treefileName,
blockfileName, PrefixLength, CultureId,
nodesize, buffersize);
return new xBplusTree(tree);
}
public static new xBplusTree Initialize(string treefileName,
string blockfileName, int PrefixLength, int CultureId)
{
xBplusTreeBytes tree = xBplusTreeBytes.Initialize(treefileName,
blockfileName, PrefixLength, CultureId);
return new xBplusTree(tree);
}
....
public static new xBplusTree ReOpen(System.IO.Stream treefile,
System.IO.Stream blockfile)
{
xBplusTreeBytes tree = xBplusTreeBytes.ReOpen(treefile, blockfile);
return new xBplusTree(tree);
}
public static new xBplusTree ReOpen(string treefileName, string
blockfileName)
{
xBplusTreeBytes tree = xBplusTreeBytes.ReOpen(treefileName,
blockfileName);
return new xBplusTree(tree);
}
public static new xBplusTree ReadOnly(string treefileName,
string blockfileName)
{
xBplusTreeBytes tree = xBplusTreeBytes.ReadOnly(treefileName,
blockfileName);
return new xBplusTree(tree);
}
....

(look familiar?)

Without going into great detail the Python implementation is much
simpler
(or at least shorter) than this due to Python's very relaxed type
system.

My question is: Is there a better way to do this? Please inform!
Thanks,
-- Aaron Watters

===
Let's not elect him in 2004 either.
 
Nicholas Paldino said:
Aaron,

I am kind of curious why so much of these members are static. I mean,
most of these members act on an instance, and it defeats the whole purpose
of OO to have it in this manner.

Hardly. I'm just giving different names to different types of
constructors rather than overloading the standard constructor
20 different ways.
If you need the methods to return specific sub types, then you will have
to create a specialized instance for each type. This sucks, I know, but
it's the only way to do it in .NET 1.1 and before.

However, in 2.0, you will be able to define this as a generic type, and
have it perform operations and return that type from methods, when you
declare an instance of that type.

So I can't improve the code yet... bummer.
Also, as a side note, you have a tremendous number of naming violations
....

yes yes. Thanks for the response! -- Aaron Watters

===
Han: So whaddya think kid -- a princess and a guy like me...?
Luke: No.
 
Back
Top