Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having problem to inherit Typed Datarow to be implemented in my coding. my question is, what is the right method to inherit a Typed Datarow from a class and then initialize it in another class?

eg:
C#
Public class ClassA : DataSet1.DataRow1
{
    public ClassA(DataRowBuilder rb) : base (rb)
    {
        //TODO
    }
}



but how am i suppose to get this constructor works?

i got error at :

C#
Public class ClassB
{
    private void DoWork()
    {
        ClassA newRow = new ClassA(); <--- Error ("Cannot resolve construtor")
    }
}
Posted
Updated 14-Nov-11 21:53pm
v2
Comments
lukeer 15-Nov-11 3:57am    
To improve your question's readability, wrap code blocks in <pre lang="c#">code<pre> tags.
Jimmy Beh 16-Nov-11 0:08am    
thanks..im a newbie to this forum here

There are two problems in the code you posted.

1. Classes cannot inherit from instances.
You wrote
C#
Public class ClassA : DataSet1.DataRow1

This makes the class ClassA inherit from DataSet1.DataRow1. From the usual naming conventions, this looks like DataRow1 is an object of type DataRow.
So make it
C#
Public class ClassA : DataRow
instead.

2. Using a non-existent constructor doesn't work
You are trying to create an instance of ClassA via
C#
new ClassA();
You use a constructor that doesn't take any parameters. But in your given code, the only constructor for ClassA takes one parameter of type DataRowBuilder.

If you don't provide any constructors, the compiler silently creates one without parameters. That's why you can use it in simple classes without writing it beforehand.
This shortcut is disabled as soon as you provide any constructors. So you're left with two options:
a) Write another constructor that works without parameters or
b) In ClassB.DoWork() use the existing ClassA constructor like
C#
new ClassA(someDataRowBuilderThatIsValidInThisScope);
 
Share this answer
 
Comments
Jimmy Beh 22-Nov-11 22:21pm    
your right, but your case is for normal object DataRow. What i meant in the problem is that I'm trying to inherit from a strongly Typed DataRow. As a result, i have no choice but to inherit in such a way.
Moreover, DataRow has a default DataRowBuilder as the parameter. The question is how do I create the constructor of this class if i were to inherit from a strongly typed DataRow where DataRowBuilder's value are not meant to be used. There is no way to pass any value through this constructor then. How is it suppose to be initialized then?
You need to have a default constructor with no parameters in classA.
Since you have defined a constructor with a parameter (DataRowBuilder) the original constructor is unavailable.

Or else, you can pass a new DataRowBuilder object when you create an instance of classA.
 
Share this answer
 
Comments
Jimmy Beh 15-Nov-11 4:13am    
It needs a parameter which is "DataRowBuilder" to define the constructor from the research made. that is why i declared in such a way by having "DataRowBuilder" as the constructor's parameter. but somehow i need to initialize "ClassA" to use the constructor in "ClassB". and it failed, any other method to make this work?
By the looks of it, you are trying to inherit a class from a specific instance of a DataSet, and a specific instance of it's rows (which it dowesn't have anyway - a set has tables, which have rows)

You can't do that: inheritance does not work from a specific instance, it works from generic types.

For example, you can say that a Ford Focus is a class inherited from the Ford class, which inherits from the Car class:

C#
public class Car{}
public class Ford : Car {}
public class Focus: Ford {}
public class Mondeo : Ford {}

But you can't say that a Ford Focus is a class inherited from your aunt Ethyl's Honda accord.

You need to go back to basics and look again at your course notes - you do not understand classes, and instances, much less inheritance yet!
 
Share this answer
 
Comments
Jimmy Beh 15-Nov-11 4:29am    
so from what you said, u meant that the class could not be initialized is due to the inheritance problem instead of wrong method in initializing it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900