overloading the ctor question

  • Thread starter Thread starter Eric Sabine
  • Start date Start date
E

Eric Sabine

Here should be an easy question. I want to overload the ctor of the class
with 2 versions, one takes 3 input parameters and the other takes 2, if I
call the "2" version I want to simply call the 3 version but pass in a
private variable for the missing parameter. But at compile time, I get
"An object reference is required for the nonstatic field, method, or
property ..."


public class TestExample
{
private string m_tablename;

// public properties
public string TableNameToCreate
{ set { m_tablename=value; } }


// the ctors
public xsd_retrieving(string ConnectionString, string SqlStatement):
this(ConnectionString, SqlStatement, m_tablename)
{
// There is to be no code here
}

public TestExample(string ConnectionString, string SqlStatement,
string TableName
{
......
}


} // TestExample
 
Eric,

Outside of the brackets, the m_tablename field doesn't have any context.
In any case, the field will always be null anyways. In this case, you want
the declaration to be:

public TestExample(string ConnectionString, string SqlStatement) :
this(ConnectionString, SqlStatement, null)

I assume you meant TestExample and not xsd_retrieving as your class
name. After all, "xsd_retrieving" is a violation of the .NET naming
guidelines for public types/members, and we wouldn't want that now, would
we? ;)

Hope this helps.
 
Here should be an easy question. I want to overload the ctor of the
class with 2 versions, one takes 3 input parameters and the other
takes 2, if I call the "2" version I want to simply call the 3 version
but pass in a private variable for the missing parameter. But at
compile time, I get "An object reference is required for the nonstatic
field, method, or property ..."

First, I think you mean 'base(...)' instead of 'this(...)'.

Second, The 'base(...)' construct is only used on constructor functions,
which xsd_retrieving is not. If you want xsd_retrieving to return an
object of type 'TestExample', as a constructor would, then inside
xsd_retrieving, create a 'TestExample' and return it (set the return type
appropriately.)

If you really intend for xsd_retrieving to be a constructor, then you need
to name it the same as the class. Inside, you would return a new
TestExample(a, b, c).

-mdb
 
Nicholas, as you and mdb correctly pointed out, my code had a typo and I
should have renamed the 2 ctors to be the same. Thanks for the null
suggestion. That is how I will handle it.

Now, for your "tongue-in-cheek" comment about my lame naming convention.
Yes, yes, yes, I know it's bad but it's just test code. I couldn't agree
with you more though. On the other hand, do you have links for some good
online reading about naming guidelines?

---
Eric


You teach a child to read and he or her will be able to pass a literacy
test.
-- President Dubya at Townsend Elementary School, touting his education
reform plans, Feb. 21, 2001



} // TestExample
 
mdb said:
First, I think you mean 'base(...)' instead of 'this(...)'.

No, I don't think so. He wants to call another constructor in the same
class, not the base class.
 
// the ctors
public TestExample(string ConnectionString, string SqlStatement):
this(ConnectionString, SqlStatement, m_tablename)
{
// There is to be no code here
}

The problem with this, which Nicholas hints at, is that constructors
construct; they *create* the object. So, you cannot pass m_tablename,
because, until that call returns, this.m_tablename doesn't exist.

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
 
yes that is exactly the problem my caffene deprived mind didn't realize at
the time. Thank god my company stocks a coffee flavor called "Dark Magic."
It tastes like dirt but does the trick :-)

thanks James.
 
Back
Top