469,275 Members | 1,818 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

How can i have multiple base classes.

hi all,

I would like to be able to create an umbrella class for all my main global
sections but I would still like to keep them all in separate file something
like the below but I keep getting an error saying you are not allowed
Multiple base classes.
/// <summary>

/// This is the umbrella Object for loading all the Global classes at once.

/// It should only ever be used for this task.

/// </summary>

public class myGlobal : GlobalVariables , Settings , ErrorHandler ,StopWatch

{
}

Does any one know a way of doing this.

Thanks

ink


Feb 27 '07 #1
15 27294
"iKiLL" <iK***@NotMyEmail.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
hi all,

I would like to be able to create an umbrella class for all my main global sections but I
would still like to keep them all in separate file something like the below but I keep
getting an error saying you are not allowed Multiple base classes.
/// <summary>

/// This is the umbrella Object for loading all the Global classes at once.

/// It should only ever be used for this task.

/// </summary>

public class myGlobal : GlobalVariables , Settings , ErrorHandler ,StopWatch

{
}

Does any one know a way of doing this.

Thanks

ink

..NET does not support Multiple Inheritance, so the answer is you can't derive from multiple
base classes.

Willy.

Feb 27 '07 #2
iKiLL wrote:
public class myGlobal : GlobalVariables , Settings , ErrorHandler
,StopWatch
{
}
..NET/C# does not support multiple inheritance. Anders Hejlsberg doesn't like
it :-)
Does any one know a way of doing this.
You should use "using" directives for the classes, you want to reference.

Ebbe
Feb 27 '07 #3
Thanks for your comments both of you,

Ebbe what do you mean by ("using" directives for the classes) is this a
simple work around.
i have found a complicated work around using Interfaces.

Thnaks,
ink


"Ebbe Kristensen" <eb**@ekologic.dkwrote in message
news:eL**************@TK2MSFTNGP05.phx.gbl...
iKiLL wrote:
>public class myGlobal : GlobalVariables , Settings , ErrorHandler
,StopWatch
{
}

.NET/C# does not support multiple inheritance. Anders Hejlsberg doesn't
like it :-)
>Does any one know a way of doing this.

You should use "using" directives for the classes, you want to reference.

Ebbe

Feb 27 '07 #4
"iKiLL" <iK***@NotMyEmail.comwrote in
news:#q**************@TK2MSFTNGP02.phx.gbl:
I would like to be able to create an umbrella class for all my main
global sections but I would still like to keep them all in separate
file something like the below but I keep getting an error saying you
are not allowed Multiple base classes.

/// <summary>

/// This is the umbrella Object for loading all the Global classes at
once.

/// It should only ever be used for this task.

/// </summary>

public class myGlobal : GlobalVariables , Settings , ErrorHandler
,StopWatch

{
}
Does any one know a way of doing this.
Can you have instance variables for your "global" classes in your
"umbrella" class?

Eg.
public class MyGlobal
{
private GlobalVariables globalVariables;
private Settings settings;
private ErrorHandler errorHandler;
...
}

And of course you set the instances as appropriate, and expose them as
appropriate...

/Peter
Feb 27 '07 #5
iKiLL wrote:
Ebbe what do you mean by ("using" directives for the classes) is this
a simple work around.
i have found a complicated work around using Interfaces.
I am tempted to ask why you are asking that question? Surely you do know
what the "#using" directive does, don't you?

For example, if you insist on using global variables (you shouldn't but
that's another discussion), you collect them in a class in a .cs file and
get access to them in other files through a "#using" directive.

Ebbe
Feb 27 '07 #6
iKiLL <iK***@NotMyEmail.comwrote:
I would like to be able to create an umbrella class for all my main global
sections but I would still like to keep them all in separate file something
like the below but I keep getting an error saying you are not allowed
Multiple base classes.
Indeed you're not. However, I would question your design anyway. Does
your class really represent something which *is* a StopWatch and *is* a
Settings, and *is* an ErrorHandler? Or does it just *have* or use those
things? It sounds to me much more likely that composition is more
appropriate than inheritance here. (I would also worry about any class
called GlobalVariables, btw.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 27 '07 #7


hi all,

i am have been developing in VB6 for over 6 years.

I am having a hard time coming to grips with the development environment of
OOP.

The hole concept of not being able to access stuff that i need when i need
it with out loading up a entire class is really bugging me.

So fare in fact it is the most annoying part of this whole learning curve.

i am just trying to make development less time consuming with out effecting
the over program.

i really am open to suggestions but so fare all i have had from anyone is
[Why don't you just create the class when you need it.]

Below are a few things that i would like in my Global variables

public static string APPPATH =
System.IO.Path.GetDirectoryName(System.Reflection. Assembly.GetExecutingAssembly().GetName().CodeBase );

public string APP_PATH = APPPATH;

public string LOCAL_DB_PATH = APPPATH + @"\DB\";

public string ERROR_LOG_PATH = APPPATH + @"\Log\";

public string DIALOG_TITLE = "Mitie Cleaning";

public Boolean LOG_ERROR = false;

Now this means that ever time i want to access one of my paths i have to
Write the code to create the class before i can us my Variable and it is the
same with everything else.

if someone has a better suggestion then [just do it the long way like
everyone else] i would love to hear it.

thanks

ink


"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
iKiLL <iK***@NotMyEmail.comwrote:
>I would like to be able to create an umbrella class for all my main
global
sections but I would still like to keep them all in separate file
something
like the below but I keep getting an error saying you are not allowed
Multiple base classes.

Indeed you're not. However, I would question your design anyway. Does
your class really represent something which *is* a StopWatch and *is* a
Settings, and *is* an ErrorHandler? Or does it just *have* or use those
things? It sounds to me much more likely that composition is more
appropriate than inheritance here. (I would also worry about any class
called GlobalVariables, btw.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Feb 27 '07 #8
You might want to try

public class MySettings
{
private static MySettings m_MySettings = null;
private string m_Path;

private static MySettings GetInstance()
{
if(m_MySettings = null)
{
m_MySettings = new MySettings();
m_MySettings.m_Path =
System.IO.Path.GetDirectoryNameSystem.Reflection.A ssembly.GetExecutingAssembly().GetName().CodeBase) ;
}
return m_MySettings;
}

public static string GetLocalDbPath()
{
MySettings mySettings = MySettings.GetInstance();
return m_MySettings.m_Path + @"\DB\";
}

public static string GetErrorLogPath()
{
MySettings mySettings = MySettings.GetInstance();
return m_MySettings.m_Path + @"\Log\";
}
}

in your source code you can use

string errorFile = MySettings.GetErrorLogPath() + "error.log";

The contents of MySettings apply to your entire application. Does this
answer your question?

Hans.
"iKiLL" wrote:
>

hi all,

i am have been developing in VB6 for over 6 years.

I am having a hard time coming to grips with the development environment of
OOP.

The hole concept of not being able to access stuff that i need when i need
it with out loading up a entire class is really bugging me.

So fare in fact it is the most annoying part of this whole learning curve.

i am just trying to make development less time consuming with out effecting
the over program.

i really am open to suggestions but so fare all i have had from anyone is
[Why don't you just create the class when you need it.]

Below are a few things that i would like in my Global variables

public static string APPPATH =
System.IO.Path.GetDirectoryName(System.Reflection. Assembly.GetExecutingAssembly().GetName().CodeBase );

public string APP_PATH = APPPATH;

public string LOCAL_DB_PATH = APPPATH + @"\DB\";

public string ERROR_LOG_PATH = APPPATH + @"\Log\";

public string DIALOG_TITLE = "Mitie Cleaning";

public Boolean LOG_ERROR = false;

Now this means that ever time i want to access one of my paths i have to
Write the code to create the class before i can us my Variable and it is the
same with everything else.

if someone has a better suggestion then [just do it the long way like
everyone else] i would love to hear it.

thanks

ink


"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
iKiLL <iK***@NotMyEmail.comwrote:
I would like to be able to create an umbrella class for all my main
global
sections but I would still like to keep them all in separate file
something
like the below but I keep getting an error saying you are not allowed
Multiple base classes.
Indeed you're not. However, I would question your design anyway. Does
your class really represent something which *is* a StopWatch and *is* a
Settings, and *is* an ErrorHandler? Or does it just *have* or use those
things? It sounds to me much more likely that composition is more
appropriate than inheritance here. (I would also worry about any class
called GlobalVariables, btw.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Feb 27 '07 #9
Thanks Hans,

This looks like exactly what i am after.

But i am sorry i am not sure i understand how it is working.

Is this a Singleton Design Pattern. i recognize the GetInstance() from
something i was reading earlier.

How is it that this is working with out having to write all the create
object code, And at what point is this loaded into memory and destroyed from
memory?

Thanks
ink

"Hans" <Ha**@discussions.microsoft.comwrote in message
news:B2**********************************@microsof t.com...
You might want to try

public class MySettings
{
private static MySettings m_MySettings = null;
private string m_Path;

private static MySettings GetInstance()
{
if(m_MySettings = null)
{
m_MySettings = new MySettings();
m_MySettings.m_Path =
System.IO.Path.GetDirectoryNameSystem.Reflection.A ssembly.GetExecutingAssembly().GetName().CodeBase) ;
}
return m_MySettings;
}

public static string GetLocalDbPath()
{
MySettings mySettings = MySettings.GetInstance();
return m_MySettings.m_Path + @"\DB\";
}

public static string GetErrorLogPath()
{
MySettings mySettings = MySettings.GetInstance();
return m_MySettings.m_Path + @"\Log\";
}
}

in your source code you can use

string errorFile = MySettings.GetErrorLogPath() + "error.log";

The contents of MySettings apply to your entire application. Does this
answer your question?

Hans.
"iKiLL" wrote:
>>

hi all,

i am have been developing in VB6 for over 6 years.

I am having a hard time coming to grips with the development environment
of
OOP.

The hole concept of not being able to access stuff that i need when i
need
it with out loading up a entire class is really bugging me.

So fare in fact it is the most annoying part of this whole learning
curve.

i am just trying to make development less time consuming with out
effecting
the over program.

i really am open to suggestions but so fare all i have had from anyone is
[Why don't you just create the class when you need it.]

Below are a few things that i would like in my Global variables

public static string APPPATH =
System.IO.Path.GetDirectoryName(System.Reflection .Assembly.GetExecutingAssembly().GetName().CodeBas e);

public string APP_PATH = APPPATH;

public string LOCAL_DB_PATH = APPPATH + @"\DB\";

public string ERROR_LOG_PATH = APPPATH + @"\Log\";

public string DIALOG_TITLE = "Mitie Cleaning";

public Boolean LOG_ERROR = false;

Now this means that ever time i want to access one of my paths i have to
Write the code to create the class before i can us my Variable and it is
the
same with everything else.

if someone has a better suggestion then [just do it the long way like
everyone else] i would love to hear it.

thanks

ink


"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft. com...
iKiLL <iK***@NotMyEmail.comwrote:
I would like to be able to create an umbrella class for all my main
global
sections but I would still like to keep them all in separate file
something
like the below but I keep getting an error saying you are not allowed
Multiple base classes.

Indeed you're not. However, I would question your design anyway. Does
your class really represent something which *is* a StopWatch and *is* a
Settings, and *is* an ErrorHandler? Or does it just *have* or use those
things? It sounds to me much more likely that composition is more
appropriate than inheritance here. (I would also worry about any class
called GlobalVariables, btw.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too



Feb 27 '07 #10
iKiLL <iK***@NotMyEmail.comwrote:
i am have been developing in VB6 for over 6 years.

I am having a hard time coming to grips with the development environment of
OOP.

The hole concept of not being able to access stuff that i need when i need
it with out loading up a entire class is really bugging me.

So fare in fact it is the most annoying part of this whole learning curve.

i am just trying to make development less time consuming with out effecting
the over program.
That's not really the best way of going - if you try to shoehorn an OO
environment into your non-OO mindset, you're going to be fighting it
every step of the way.
i really am open to suggestions but so fare all i have had from anyone is
[Why don't you just create the class when you need it.]

Below are a few things that i would like in my Global variables

public static string APPPATH =
System.IO.Path.GetDirectoryName(System.Reflection. Assembly.GetExecutingAssembly().GetName().CodeBase );

public string APP_PATH = APPPATH;

public string LOCAL_DB_PATH = APPPATH + @"\DB\";

public string ERROR_LOG_PATH = APPPATH + @"\Log\";

public string DIALOG_TITLE = "Mitie Cleaning";

public Boolean LOG_ERROR = false;
Firstly, look into the .NET naming conventions - these should be things
like ApplicationPath (although look at Application.ExecutablePath for
an easier way of doing this), LocalDatabasePath, ErrorLogPath,
DialogTitle and LogError.
Now this means that ever time i want to access one of my paths i have to
Write the code to create the class before i can us my Variable and it is the
same with everything else.
No you don't. Either you could use the Singleton pattern, or just make
the above into static variables (or preferably properties).

It's well worth reading a book or tutorial on OO before going too much
further. I'm not saying that to be unkind - I just know how easy it is
to carry practices from one environment into another where they're not
appropriate.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 27 '07 #11


Ok i think i have figured it out

The object creates it self the first time any of its properties are called
and then tests the internal object each time to make sure it only loads
itself up once into memory.

And then I assume that it will remain in memory till the application shuts
down.

But how is the Properties exposed the very first time[string errorFile =
MySettings.GetErrorLogPath() + "error.log";], if the object is not in memory
yet.

Is this some sort of rule that is just allowed. or am i missing something.

Thanks,

ink



"iKiLL" <iK***@NotMyEmail.comwrote in message
news:OU**************@TK2MSFTNGP05.phx.gbl...
Thanks Hans,

This looks like exactly what i am after.

But i am sorry i am not sure i understand how it is working.

Is this a Singleton Design Pattern. i recognize the GetInstance() from
something i was reading earlier.

How is it that this is working with out having to write all the create
object code, And at what point is this loaded into memory and destroyed
from memory?

Thanks
ink

"Hans" <Ha**@discussions.microsoft.comwrote in message
news:B2**********************************@microsof t.com...
>You might want to try

public class MySettings
{
private static MySettings m_MySettings = null;
private string m_Path;

private static MySettings GetInstance()
{
if(m_MySettings = null)
{
m_MySettings = new MySettings();
m_MySettings.m_Path =
System.IO.Path.GetDirectoryNameSystem.Reflection. Assembly.GetExecutingAssembly().GetName().CodeBase );
}
return m_MySettings;
}

public static string GetLocalDbPath()
{
MySettings mySettings = MySettings.GetInstance();
return m_MySettings.m_Path + @"\DB\";
}

public static string GetErrorLogPath()
{
MySettings mySettings = MySettings.GetInstance();
return m_MySettings.m_Path + @"\Log\";
}
}

in your source code you can use

string errorFile = MySettings.GetErrorLogPath() + "error.log";

The contents of MySettings apply to your entire application. Does this
answer your question?

Hans.
"iKiLL" wrote:
>>>

hi all,

i am have been developing in VB6 for over 6 years.

I am having a hard time coming to grips with the development environment
of
OOP.

The hole concept of not being able to access stuff that i need when i
need
it with out loading up a entire class is really bugging me.

So fare in fact it is the most annoying part of this whole learning
curve.

i am just trying to make development less time consuming with out
effecting
the over program.

i really am open to suggestions but so fare all i have had from anyone
is
[Why don't you just create the class when you need it.]

Below are a few things that i would like in my Global variables

public static string APPPATH =
System.IO.Path.GetDirectoryName(System.Reflectio n.Assembly.GetExecutingAssembly().GetName().CodeBa se);

public string APP_PATH = APPPATH;

public string LOCAL_DB_PATH = APPPATH + @"\DB\";

public string ERROR_LOG_PATH = APPPATH + @"\Log\";

public string DIALOG_TITLE = "Mitie Cleaning";

public Boolean LOG_ERROR = false;

Now this means that ever time i want to access one of my paths i have to
Write the code to create the class before i can us my Variable and it is
the
same with everything else.

if someone has a better suggestion then [just do it the long way like
everyone else] i would love to hear it.

thanks

ink


"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft .com...
iKiLL <iK***@NotMyEmail.comwrote:
I would like to be able to create an umbrella class for all my main
global
sections but I would still like to keep them all in separate file
something
like the below but I keep getting an error saying you are not allowed
Multiple base classes.

Indeed you're not. However, I would question your design anyway. Does
your class really represent something which *is* a StopWatch and *is*
a
Settings, and *is* an ErrorHandler? Or does it just *have* or use
those
things? It sounds to me much more likely that composition is more
appropriate than inheritance here. (I would also worry about any class
called GlobalVariables, btw.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Feb 27 '07 #12
iKiLL wrote:
But i am sorry i am not sure i understand how it is working.

Is this a Singleton Design Pattern. i recognize the GetInstance() from
something i was reading earlier.
It is indeed. A singleton works as follows:

first, the class contains a class variable, i.e. a variable tied to the
class instead of to an instance of the class. This is the "private static
MySettings m_MySettings = null;" at the start of the class.

The only way to get the access to an instance of this class is by calling
the GetInstance() method. This method checks to see if an instance has been
created. If not, it creates one and stores a reference to it in the
'm_MySettings' variable and initialises the other variables in the object.
Whether or not a new instance is created, a reference to it is returned to
the caller, who can then access the public interfaces to it - and only
those. In this case there are a couple of GetXxxx methods whereas the actual
values are not accessible. This design ensures that you cannot accidentally
change the settings.

Using a singleton ensures that you always refer to the same object. Without
it you might end up with two different object of the same class and a bunch
of hard-to-find errors.
How is it that this is working with out having to write all the create
object code,
Hmm, seems that Hans forgot a:

private void MySettings() {};
And at what point is this loaded into memory and destroyed from memory?
It is loaded on first access to it. Unload is something you don't need to
think about, .NET does it for you when the object is no longer needed. This
is what they call garbage collection.

Ebbe
Feb 27 '07 #13
iKiLL wrote:
But how is the Properties exposed the very first time[string errorFile =
MySettings.GetErrorLogPath() + "error.log";], if the object is not in memory
yet.

Is this some sort of rule that is just allowed. or am i missing something.
They are static. That means that they are accessed through the class,
not through an instance of the class.

--
Göran Andersson
_____
http://www.guffa.com
Feb 27 '07 #14
Am Tue, 27 Feb 2007 11:09:45 -0000 schrieb Jon Skeet [C# MVP]:
>
Indeed you're not. However, I would question your design anyway. Does
your class really represent something which *is* a StopWatch and *is* a
Settings, and *is* an ErrorHandler? Or does it just *have* or use those
things? It sounds to me much more likely that composition is more
appropriate than inheritance here. (I would also worry about any class
called GlobalVariables, btw.)
I agree here, but pls look at the following:
I have derivations of TextBox, Combobox, Checkbox, Label, TreeView and some
more. They all implement the same additional stuff - connection to a member
of a business object. For each of the controls, this is about 100 lines of
code, of which 80-95% are the same for each control. Due to the lack of MI,
I can't factor it out.

It is simply annoying to have to write things like

//-----------------------------------------------------------------
// boBindName
//
[
Bindable (true)
, Category ("QFC")
, Description ("Feld/Property, an das gebunden werden soll")
]
public string boBindName
{
get { return mBoBindName; }
set { mBoBindName = value; }
}

over and over again and identical in all controls. This is stupid, and
exactly this is one of the things MI was invented for.

My 2 cents..
Paule

Feb 28 '07 #15
On Wed, 28 Feb 2007 08:05:47 +0800, Paul Werkowitz
<ne********@primaprogramm.dewrote:
[...]
It is simply annoying to have to write things like

//-----------------------------------------------------------------
// boBindName
//
[
Bindable (true)
, Category ("QFC")
, Description ("Feld/Property, an das gebunden werden soll")
]
public string boBindName
{
get { return mBoBindName; }
set { mBoBindName = value; }
}

over and over again and identical in all controls. This is stupid, and
exactly this is one of the things MI was invented for.
I agree that having multiple inheritance would help address that.
However, there are other solutions that don't involve copying and pasting
the interesting code over and over.

For one, you could instead implement a helper class that all of your
derived classes include and expose as a property. Then you only have to
add the member (field) to the derived classes, along with a simple
property to expose the member. The longer code just needs to be written
once. This is, of course, a lot more maintainable as well; if you have to
change the shared functionality, you can change it just once rather than
having to revisit several copies of a 100 line block of code.

A similar solution would be to declare an interface. Then rather than
exposing your added functionality as a property, it's simply an interface
that the derived classes expose. This works more like multiple
inheritance, without many of the problems of multiple inheritance.

That said, I have also found that often times when I find that the
language seems to be impeding my progress and preventing me from doing
something I want to do, then whatever I am trying to do turns out to be a
poor design decision in the first place. There's usually an alternate
method that is completely different from the approach I start out with,
and which works better in the given language. In many cases, it turns out
to be more efficient and more maintainable as well.

In addition, my experiences with multiple inheritance in C++ have led me
to feel that multiple inheritance has more drawbacks than it does
advantages. Code I've dealt with that has multiple inheritance invariably
is harder to work with, buggier, and often creates strange ambiguities
that are difficult (or at the very least, awkward) to resolve.

It may be that you are dealing with a rare case in which you truly need
multiple inheritance. However, I suspect that it's more likely that if
you simply approach the problem from a slightly different vantage, you may
find an alternate solution that works just fine and which fits better into
the .NET paradigm, and which does not require that you copy and paste your
code over and over. My experience has been that copied and pasted code is
one of the very worst transgressions a programmer can do. I've yet to run
into a situation where interesting code (that is, anything more
complicated that a declaration or reference or something like that)
actually does have to be copied and pasted.

Pete
Mar 1 '07 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Stuart Golodetz | last post: by
6 posts views Thread by Paul | last post: by
20 posts views Thread by km | last post: by
22 posts views Thread by Matthew Louden | last post: by
5 posts views Thread by Scott | last post: by
2 posts views Thread by Heinz Ketchup | last post: by
3 posts views Thread by Jess | last post: by
47 posts views Thread by Larry Smith | last post: by
13 posts views Thread by stephenpas | last post: by
2 posts views Thread by Immortal Nephi | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.