-
February 7th, 2010, 10:49 AM
#1
Need help with ineritance tree, new to C#
I am new to C# and am trying to make a .cs file for my ASP.net website to have a few objects to handel file and folder calls. The problem I am running into is with inheritance trees. I have created the base objects and it is inherited by object2, which works fine but when I create object 3 and have it inherit from object two I can not pull the varibles from the base object or object 2. I have attached my current code to the end of this post for your review, can anyone tell me where I am going wrong? I am new to C# and the whole OPP thing. Also if you have any advice on bettering my code I would like to hear them too.
Code:
using System;
using System.IO;
using System.Collections.Generic;
using System.Web;
/// <summary>
/// This file contains the classes for checking for and creating both Files and Folders.
/// </summary>
abstract class BaseHandels
{
private string m_BaseFolderPath = @"D:\FFInfoAdmin\";
private string m_FolderName;
private string m_FileName;
public string BaseFolderPath
{
get { return m_BaseFolderPath; }
}
public string FolderName
{
get { return m_FolderName; }
set { m_FolderName = value; }
}
public string FileName
{
get { return m_FileName; }
set { m_FileName = value; }
}
}
class CheckForFolder:BaseHandels
{
Int32 FolderCheck()
{
if (FolderName == "")
{ return 2; }
else
{
string FolderPath = BaseFolderPath + FolderName;
return (Directory.Exists(FolderPath)) ? 1 : 0;
}
}
}
class CheckForFile:BaseHandels
{
Int32 FileCheck()
{
if (FolderName == "" || FileName == "")
{ return 2; }
else
{
string FilePath = BaseFolderPath + FolderName + @"\" + FileName;
return (File.Exists(FilePath)) ? 1 : 0;
}
}
}
class CreateFolder:CheckForFolder
{
}
-
February 7th, 2010, 11:18 AM
#2
Re: Need help with ineritance tree, new to C#
Int32 FileCheck() and Int32 FolderCheck() are both private, set them to protected and you should be able to access them from the CreateFolder class.
BaseFolderPath, FolderName and FileName should also be accessable from the CreateFolder. But as the top three members of the base class are private you wont be able to access them from CreateFolder, CheckForFolder or CheckForFile.
If you want something to be accessable only for the one class use private, if you wan inherited classes to be able access them use protected and if everybody should be able to access them, use public.
-
February 7th, 2010, 12:37 PM
#3
Re: Need help with ineritance tree, new to C#
I changed everything from private to protected and add the protected keyword before the methods but when trying to call anything from the two classes in the CreateFolder class I still don't have aceess.
-
February 7th, 2010, 01:28 PM
#4
Re: Need help with ineritance tree, new to C#
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
February 7th, 2010, 01:50 PM
#5
Re: Need help with ineritance tree, new to C#
As requested.
Code:
using System;
using System.IO;
using System.Collections.Generic;
using System.Web;
/// <summary>
/// This file contains the classes for checking for and creating both Files and Folders.
/// </summary>
abstract class BaseHandels
{
protected string m_BaseFolderPath = @"D:\FFInfoAdmin\";
protected string m_FolderName;
protected string m_FileName;
public string BaseFolderPath
{
get { return m_BaseFolderPath; }
}
public string FolderName
{
get { return m_FolderName; }
set { m_FolderName = value; }
}
public string FileName
{
get { return m_FileName; }
set { m_FileName = value; }
}
}
class CheckForFolder:BaseHandels
{
protected Int32 FolderCheck()
{
if (FolderName == "")
{ return 2; }
else
{
string FolderPath = BaseFolderPath + FolderName;
return (Directory.Exists(FolderPath)) ? 1 : 0;
}
}
}
class CheckForFile:BaseHandels
{
protected Int32 FileCheck()
{
if (FolderName == "" || FileName == "")
{ return 2; }
else
{
string FilePath = BaseFolderPath + FolderName + @"\" + FileName;
return (File.Exists(FilePath)) ? 1 : 0;
}
}
}
class CreateFolder:CheckForFolder
{
}
-
February 7th, 2010, 02:03 PM
#6
Re: Need help with ineritance tree, new to C#
you need to have some public methods and/or properties in your derived class if you want it to be usable
I see you have difcullty with access modifiers. you should read something about them: C# Access Modifiers
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
February 7th, 2010, 02:59 PM
#7
Re: Need help with ineritance tree, new to C#
So I have to create a second set of properties in second class to gain access to the properites in the first class? I have read that link you have given me before and I thought I understood it which is where my original code came from. The docs on MSDN are not that clear to me though so I think I still don't know. When I took a VB.net class and we did classes like this we did private varibles with public properties. Though we never did inheritance trees in that class.
Last edited by Eagle_f90; February 7th, 2010 at 03:01 PM.
-
February 7th, 2010, 03:19 PM
#8
Re: Need help with ineritance tree, new to C#
Originally Posted by Eagle_f90
So I have to create a second set of properties in second class to gain access to the properites in the first class?
not necessarily, it depends on what you're going to do, look here:
Code:
abstract class BaseHandels
{
// protected means that only the base and a derived class can access it
protected string m_BaseFolderPath = @"D:\FFInfoAdmin\";
public string BaseFolderPath
{
get { return m_BaseFolderPath; } // base class accessing m_BaseFolderPath
// you can even have different access modifier for the setter (it's a good practice if only the derived class is allowed to change it)
// it's inaccessible from outside
protected set { m_BaseFolderPath = value; }
}
}
class CheckForFolder : BaseHandels
{
// this must be public if you want to use it outside
public Int32 FolderCheck()
{
// here you have two ways to get the path
string folderPath1 = BaseFolderPath; // via your property
string folderPath2 = base.m_BaseFolderPath; // or direclty
BaseFolderPath = @"c:\new\path"; // and finally you can change the value via the protected setter
return -1; // unimportant, only so that it compiles
}
}
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
February 7th, 2010, 05:12 PM
#9
Re: Need help with ineritance tree, new to C#
This is why I have avoided OOP till now, just to difficult to understand. Looking over your example I am really not seeing what your trying to say. So here are my non-undersandings one by one:
1. I setup the first 3 stings (m_XXX) as private becaues I did not want them directly modified by anything. Was this wrong?
2. I set public properties for each private stong in BaseHandels so when I call the objects I can pass the varibles and not affect other objects. Did I do this wrong?
3. I still do not see why when I am in the 3rd branch of the inheratince tree I can not gain access to ANYTHING from BaseHandels or CheckForFile.
4. The two methods for file/folder checks are set to Protected so the 3rd tree would have access to them but they could not be called outside of the 3rd tree. Is this not done right?
-
February 8th, 2010, 12:09 AM
#10
Re: Need help with ineritance tree, new to C#
Originally Posted by Eagle_f90
This is why I have avoided OOP till now, just to difficult to understand.
it's very easy. all you have to know about them is that actually you could have everything public and it will also work. the access modifieres are only a help for the programmer so that you don't modify anything accidentally. this way you don't have to still remember what you can and what not, if you do something wrong then the compiler will remember it to you. it makes programming a little bit easier.
Originally Posted by Eagle_f90
1. I setup the first 3 stings (m_XXX) as private becaues I did not want them directly modified by anything. Was this wrong?
that's perfeclty ok
Originally Posted by Eagle_f90
2. I set public properties for each private stong in BaseHandels so when I call the objects I can pass the varibles and not affect other objects. Did I do this wrong?
this I don't understand but probably you mean to change the private fields only via properties? if so, then it's ok
Originally Posted by Eagle_f90
3. I still do not see why when I am in the 3rd branch of the inheratince tree I can not gain access to ANYTHING from BaseHandels or CheckForFile.
show us please your Level3 code because I have no problems accessing anything
Originally Posted by Eagle_f90
4. The two methods for file/folder checks are set to Protected so the 3rd tree would have access to them but they could not be called outside of the 3rd tree. Is this not done right?
that's ok too. I you want this design then that's perfect.
Last edited by memeloo; February 8th, 2010 at 12:12 AM.
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
February 8th, 2010, 06:34 AM
#11
Re: Need help with ineritance tree, new to C#
The code I was using for the 3rd tree was
class CreateFolder:CheckForFolder
{
}
but when ever I typed any varbiles in VS08Pro's intillisense would not show any varibles from the base or 2nd tree. I have actualy rethought what I needed to do with this objects and relized I am making it too complicated. I have broken the class down to a base and create object right now, with modify and delete to yet be done but the objects will never go more then 2 trees deep so this is no longer a problem. Thanks everyone for the help and understanding.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|