469,609 Members | 1,190 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

Beginner - basic inheritance problem -- public/protected

I am new to oop so please bear with me:

Could someone please explain to me why the "protected void
runCategory() method" cannot access the local ddlCategory object that
is easily accessed in the "createNav() method"? I thought that because
they are in the same class, that the ddlCategory object would be
accessible to all methods in this class.

Here is the code snippet:

public partial class Main: System.Web.UI.Page
{
public void Page_Load(object sender, EventArgs e)
{
createNav();
}
public void createNav();
{
//code for navigation for web pages -- two dropdownlists,
ddlCategory and ddlSculpture
}
protected void runCategory();
{
//definition for ddlCategory selection
}
protected void runSculpture();
{
//definition for ddlSculpture selection
{
Thanks!

May 3 '06 #1
13 2709
Hi,

What error are you getting?

Post the exact message from the compiler and also the piece of code where it
occur.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Ranginald" <da*******@gmail.com> wrote in message
news:11*********************@e56g2000cwe.googlegro ups.com...
I am new to oop so please bear with me:

Could someone please explain to me why the "protected void
runCategory() method" cannot access the local ddlCategory object that
is easily accessed in the "createNav() method"? I thought that because
they are in the same class, that the ddlCategory object would be
accessible to all methods in this class.

Here is the code snippet:

public partial class Main: System.Web.UI.Page
{
public void Page_Load(object sender, EventArgs e)
{
createNav();
}
public void createNav();
{
//code for navigation for web pages -- two dropdownlists,
ddlCategory and ddlSculpture
}
protected void runCategory();
{
//definition for ddlCategory selection
}
protected void runSculpture();
{
//definition for ddlSculpture selection
{
Thanks!

May 3 '06 #2
"Ranginald" <da*******@gmail.com> a écrit dans le message de news:
11*********************@e56g2000cwe.googlegroups.c om...

| public void createNav();
| {
| //code for navigation for web pages -- two dropdownlists,
| ddlCategory and ddlSculpture
| }

My guess is that you are declaring ddlCategory and ddlSculpture in the
createNav() method instead of private fields in the body of the class.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
May 3 '06 #3
"Ranginald" <da*******@gmail.com> a écrit dans le message de news:
11*********************@e56g2000cwe.googlegroups.c om...

| public void createNav();
| {
| //code for navigation for web pages -- two dropdownlists,
| ddlCategory and ddlSculpture
| }

My guess is that you are declaring ddlCategory and ddlSculpture in the
createNav() method instead of private fields in the body of the class.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
May 3 '06 #4
I have two web pages. default.aspx and sculpture.aspx. My goal is to
have to dropdownlist controls templated for use on all pages of the
site, and for individual pages like sculpture.aspx to have these
controls, as well as their own controls. I have a template file in
dwmx, template.dwt that has all the locked page code (e.g these ddl's
and some other stuff). This template file, and every derived file has a
codebehind="default.aspx.cs" and inherits="Ranginald"/

When the user clicks on one of the ddls', either ddlCategory or
ddlSculpture, code is executed to do some navigation. Up to this
point, everything is great.
--
The next step is to run additional things on the sculpture.aspx page.
These are repeaters and I need to put the code somewhere. I want to use
codefiles. I did a ton of reading and digging and discovered that I
could keep it simple (my goal for now) and have one codefile,
default.aspx.cs and just have inherits=" " change to reference a new
class in the same codefile.

Along these lines I was told to have this second class, pageSculpture
inherit the templated dropdownlist controls from the Ranginald class,
and then just do whatever I need additionally in this second class.
This way the Ranginald class navigation is "left alone" and is on each
page, and each new page would have an inherited class that runs the
Ranginald class navigation, but also implements its own methods.

Once I add the second class, the pageSculpture class, that's when it
all goes haywire. I am able to run the navigation on the default.aspx
page and the scuplture.aspx page as long as both have
inherit="ranginald". This is fine as a baseline, but I clearly can't
have page-specific methods loading on the Page_Load event for all of
the pages in the site.

Here is the code. It is based on on reading and digging and advice:
----------
public partial class Ranginald:System.Web.Ui.Page
{
protected void Page_Load(object sender, EventArgs e)
{
create nav();
}
protected void createNav();
{
If (!IsPostBack)
{
String strConnection = *********
String strSQL = "stoCat";
SqlConnection objConnection = new
SqlConnection(strConnection);
SqlCommand objCommand = new SqlCommand(strSQL,
objConnection);
objCommand.CommandType = CommandType.StoredProcedure;
objConnection.Open();
ddlCategory.DataSource = objCommand.ExecuteReader();
ddlCategory.DataTextField = "catDesc";
ddlCategory.DataValueField = "catID";
ddlCategory.DataBind();
objConnection.Close();

String strSQL2 = "stoSculpture";
SqlConnection objConnection2 = new
SqlConnection(strConnection);
SqlCommand objCommand2 = new SqlCommand(strSQL2,
objConnection2);
objCommand2.CommandType = CommandType.StoredProcedure;
objConnection2.Open();
ddlSculpture.DataSource = objCommand2.ExecuteReader();
ddlSculpture.DataTextField = "Name";
ddlSculpture.DataValueField = "SculpID";
ddlSculpture.DataBind();
objConnection2.Close();

ddlCategory.Items.Insert(0, "Please Select");
ddlSculpture.Items.Insert(0, "Please Select");
}

protected void runCategory(object sender, EventArgs e)
{
string category;
category = ddlCategory.SelectedValue;
int index;
index = ddlCategory.SelectedIndex;
if (index != 0)
{
response.Redirect("./sculpture.aspx?catID=" +
category);
}
protected void runScultpure(object sender, EvenArgs e)
{
//code is exactly the same as runCategory but sculpture
instead of category
}

That's the end of the first class. It all works fine.
Now I add this second class to try to get the sculpture.aspx page to
inherit this code into pageSculpture class.

protected partial class pageSculpture: Ranginald(object sender,
EventArgs e)
{
private void Page_Load(object sender, EventArgs e)
{
Ranginald rangy = new Ranginald();
rangy.createNav();

//would put new sculpture.aspx specific code here but I want to
get the basics
working first before I add more code
}

I load default.aspx and both dropdownlists appear and are populated.
When I click on on one of the the ddlCategory items, I get this spefiic
error:

===
Compilation Error

Compiler Error Message: CS0103: The name 'ddlCategory' does not exist
in the current context

Source Error:

Line 53: //DropDownList ddlCategory = new
DropDownList();
Line 54:
Line 55: ddlCategory.DataSource =
objCommand.ExecuteReader();
Line 56: ddlCategory.DataTextField = "catDesc";
Line 57: ddlCategory.DataValueField = "catID";

Source File: c:\Documents and Settings\webbackups\....\default.aspx.cs
Line: 55
Thank you so much for your help. I've been looking at this for days!
-David
:

May 3 '06 #5
Consider Protected almost as if it was Private.
Private = not accessible outside the current class.
Protected = As private, it's not accessible outside the current class with 1
exception: derived classes.
"Ranginald" <da*******@gmail.com> a écrit dans le message de news:
11*********************@e56g2000cwe.googlegroups.c om...
I am new to oop so please bear with me:

Could someone please explain to me why the "protected void
runCategory() method" cannot access the local ddlCategory object that
is easily accessed in the "createNav() method"? I thought that because
they are in the same class, that the ddlCategory object would be
accessible to all methods in this class.

Here is the code snippet:

public partial class Main: System.Web.UI.Page
{
public void Page_Load(object sender, EventArgs e)
{
createNav();
}
public void createNav();
{
//code for navigation for web pages -- two dropdownlists,
ddlCategory and ddlSculpture
}
protected void runCategory();
{
//definition for ddlCategory selection
}
protected void runSculpture();
{
//definition for ddlSculpture selection
{
Thanks!

May 3 '06 #6
but shouldn't ddlCategory be acessible to the pageSculpture class
because the pageSculpture class is derived from the Ranginald class?

May 3 '06 #7
Ranginald wrote:
I have two web pages. default.aspx and sculpture.aspx. My goal is to
have to dropdownlist controls templated for use on all pages of the
site, and for individual pages like sculpture.aspx to have these
controls, as well as their own controls. I have a template file in
dwmx, template.dwt that has all the locked page code (e.g these ddl's
and some other stuff). This template file, and every derived file has a
codebehind="default.aspx.cs" and inherits="Ranginald"/

When the user clicks on one of the ddls', either ddlCategory or
ddlSculpture, code is executed to do some navigation. Up to this
point, everything is great.


That cannot be the complete code as nowhere in what you posted is the
declaration for ddlCategory.
--
Tom Porterfield

May 3 '06 #8
I thought that I did declare the ddlCategory because

1. In the .aspx page I have
<asp:DropDownList....ID="ddlCategory.......>

and

2. In the .cs page I have ddlCategory.parameter1,
ddlCategory.parameter2, etc.

3. I find that if I did a dropdownlist ddlCategory = new
dropdownlist();, then I loose all the data binding for the initial
page.

May 3 '06 #9
I thought that I did declare the ddlCategory because

1. In the .aspx page I have
<asp:DropDownList....ID="ddlCategory.......>

and

2. In the .cs page I have ddlCategory.parameter1,
ddlCategory.parameter2, etc.

3. I find that if I did a dropdownlist ddlCategory = new
dropdownlist();, then I loose all the data binding for the initial
page. And if I try to put it anywhere else in the code it tells me that
the class already has the ddlCategory defined.

May 3 '06 #10
On 3 May 2006 14:30:51 -0700, Ranginald wrote:
I thought that I did declare the ddlCategory because

1. In the .aspx page I have
<asp:DropDownList....ID="ddlCategory.......>

and

2. In the .cs page I have ddlCategory.parameter1,
ddlCategory.parameter2, etc.

3. I find that if I did a dropdownlist ddlCategory = new
dropdownlist();, then I loose all the data binding for the initial
page. And if I try to put it anywhere else in the code it tells me that
the class already has the ddlCategory defined.


You have shown a partial class. Look at the other file, it likely contains
the declaration for ddlCategory. Is it scoped is private, protected,
internal or public? Based on what you describe, my guess is private.
Simply change it to protected and everything should work OK.
--
Tom Porterfield
May 4 '06 #11
I must confess that I only did it as a partial class because visual
studio defaulted it that way. I thought that if I wanted to have what
I listed above in asp.net that I needed to use partial classes.

Do I need to use partial classes?

And forgive the inexperience but what declaration would you be looking
for?

Thanks.

May 4 '06 #12
Hmmm...I just read that I need to use partial classes with asp.net
because that's how codefile pages are handled.

Should I change any of the scoping above?

May 4 '06 #13
does it matter where in the class body I put these declarations.
And by declarations do you mean:

dropdownlist ddlCategory = new dropdownlist()

Also,
if I make them private, do I have to do the get/set thing to make them
accessible?

Thanks

May 4 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Christian Meier | last post: by
5 posts views Thread by Jeff Greenberg | last post: by
2 posts views Thread by MJ | last post: by
6 posts views Thread by Lionel | last post: by
1 post views Thread by Jason Hickey | last post: by
7 posts views Thread by jason | last post: by
2 posts views Thread by Heinz Ketchup | last post: by
11 posts views Thread by John | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.