Beginner - basic inheritance problem -- public/protected

  • Thread starter Thread starter Ranginald
  • Start date Start date
R

Ranginald

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!
 
Hi,

What error are you getting?

Post the exact message from the compiler and also the piece of code where it
occur.
 
"Ranginald" <[email protected]> a écrit dans le message de (e-mail address removed)...

| 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
 
"Ranginald" <[email protected]> a écrit dans le message de (e-mail address removed)...

| 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
 
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


:
 
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.
 
but shouldn't ddlCategory be acessible to the pageSculpture class
because the pageSculpture class is derived from the Ranginald class?
 
Ranginald said:
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.
 
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.
 
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.
 
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.
 
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.
 
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?
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top