473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

overloading the ctor question

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
Nov 16 '05 #1
7 1325
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.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Eric Sabine" <mopar41@mail_after_hot_not_before.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

Nov 16 '05 #2
mdb
"Eric Sabine" <mopar41@mail_after_hot_not_before.com> wrote in
news:#3**************@TK2MSFTNGP11.phx.gbl:
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
Nov 16 '05 #3
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

Nicholas Paldino [.NET/C# MVP] wrote:
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.
"Eric Sabine" <mopar41@mail_after_hot_not_before.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

Nov 16 '05 #4
Eric,

Glad you asked =)

http://msdn.microsoft.com/library/de...asp?frame=true

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Eric Sabine" <mopar41@mail_after_hot_not_before.com> wrote in message
news:eS*************@TK2MSFTNGP15.phx.gbl...
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

Nicholas Paldino [.NET/C# MVP] wrote:
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.
"Eric Sabine" <mopar41@mail_after_hot_not_before.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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


Nov 16 '05 #5
mdb <m_b_r_a_y@c_t_i_u_s_a__d0t__com> wrote:
"Eric Sabine" <mopar41@mail_after_hot_not_before.com> wrote in
news:#3**************@TK2MSFTNGP11.phx.gbl:
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(...)'.


No, I don't think so. He wants to call another constructor in the same
class, not the base class.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
// 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!)
"Eric Sabine" <mopar41@mail_after_hot_not_before.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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

Nov 16 '05 #7
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.

James Curran wrote:
// 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.
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

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Martin Häcker | last post by:
Hi there, I just tried to run this code and failed miserably - though I dunno why. Could any of you please enlighten me why this doesn't work? Thanks a bunch. --- snip --- import unittest...
39
by: scooter | last post by:
Given this class heirarchy class Base{}; class A : public Base {}; class B : public Base {};
2
by: Ook | last post by:
This is a few ways I've seen to overload operator+. I can understand that you would want to pass a reference to the function if you wanted to change some of the data elements of the class, but in...
15
by: Brian Haynes | last post by:
I am having a problem with overloading. An example: Public Class Base ... End Class Public Class Derived Inherits Base ... End Class
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.