471,317 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Writing stuff

Hi all,

I would consider myself a fairly intermediate level programmer in C#, having
come from an ASP background, though there are still big gaps in my
knowledge.

One such gap is writing stuff such as...

HttpContext.Current.Application.Contents etc. etc. etc.

Now, there are two parts to this.
1. Say I want to write something like...
MyApp
In my app, I have a class, ThisClass
This would give me something like
MyApp.ThisClass

Next, as the example httpcontext above, how do I go further, such as
MyApp.ThisClass.NextLevel.DeeperLevel etc.

Also, what do you call this? (All the dots, and the intellisense picking up
sub items.)

2. In the HttpContext item above, when I get to Contents and type a dot, I
get Contents, then another dot I get Contents again (seemingly endless.)
How can I do that? What is this called?

Anyone have any URLs that area fairly straightforward to read and use?

There are other areas that I am lacking on, such as inheritance,
multithreading, etc. but they will save for another time.

The reason I am asking is that I want to construct a menu system, similar to
the way Microsoft CMS builds it... This looks like....

CmsHttpContext.Current.Channel.Url
CmsHttpContext.Current.Channel.Name
CmsHttpContext.Current.Channel.Parent.Parent.Paren t.Url

etc. etc. (note how the last example has parent.parent.parent (ad
infinitum).

Thanks for any help...

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
Feb 16 '06 #1
5 1341
this is called Namespaces.

Namespaces are mainly used for organizing, it also allows you to use
two classes that are named the same, in the same piece of code:

the "using System.IO;" at the top of every file "imports" that
namespace. what that means is if you want to access a class in that
namespace, you dont have to type the full namespace path:

i.e.:

without "using System.IO;":
System.IO.File myFile = whatever;

with "using System.IO;":
File myFile = whatever;

get the drift?

heres an article on msdn that helped me out originally:
http://msdn2.microsoft.com/en-us/lib.../dfb3cx8s.aspx

Feb 16 '06 #2
Hi David

In .Net each . represents the relationship between an object and its members.

Take
HttpContext.Current.Application.Contents

The HttpContext class has a static Current property which is a HttpContext reference.

The HttpContext class also has an Application property which is a HttpApplicationState reference.

The HttpApplicationState has a Contents property which is a HttpApplicationState reference.

HttpContext.Current.Application.Contents is just a shorter way of writing

HttpContext cur = HttpContext.Current;
HttpApplicationState appstate = cur.Application;
HttpApplicationState cont = appstate.Contents;

Similarly you can do fun stuff with strings.

string A = "one";
string B = "two";
string C = (A + B).Substring(B.IndexOf(B[0]) + A.Length, (A + B).Length - (A.Length + B.Length - B.IndexOf(B[1]))).Insert(1, "hr").PadRight(5, A[2]);

The . doesn't care about where in the line it is, as all it knows is the object immediatly to the left and right.

To go further in MyApp.ThisClass, simply add a public member to ThisClass

class ThisClass
{
private ThisClass myparent = null;
private string myurl = "http://url";

public ThisClass Parent
{
get{ return myparent; }
}

public string Url
{
get{ return myurl; }
}

public ThisClass()
{
myparent = this;
}
}

The above will let you write

ThisClass myclass = new Thisclass();
myClass.Parent.Parent.Parent.Parent.Url

Usually Parent would point to something else than itself though.

I would read up on inheritance before much else as .Net is full of it. For instance, everything inherits from System.Object (The Object class in the System namespace), which means that no matter what object you can trust it has the GetType and ToString methods.

--
Happy coding!
Morten Wennevik [C# MVP]
Feb 16 '06 #3
SP

"David" <da*****************@revilloc.REMOVETHIS.com> wrote in message
news:Oj**************@tk2msftngp13.phx.gbl...
Hi all,

I would consider myself a fairly intermediate level programmer in C#,
having come from an ASP background, though there are still big gaps in my
knowledge.

One such gap is writing stuff such as...

HttpContext.Current.Application.Contents etc. etc. etc.

Now, there are two parts to this.
1. Say I want to write something like...
MyApp
In my app, I have a class, ThisClass
This would give me something like
MyApp.ThisClass

Next, as the example httpcontext above, how do I go further, such as
MyApp.ThisClass.NextLevel.DeeperLevel etc.

Also, what do you call this? (All the dots, and the intellisense picking
up sub items.)

2. In the HttpContext item above, when I get to Contents and type a dot, I
get Contents, then another dot I get Contents again (seemingly endless.)
How can I do that? What is this called?

Anyone have any URLs that area fairly straightforward to read and use?

There are other areas that I am lacking on, such as inheritance,
multithreading, etc. but they will save for another time.

The reason I am asking is that I want to construct a menu system, similar
to the way Microsoft CMS builds it... This looks like....

CmsHttpContext.Current.Channel.Url
CmsHttpContext.Current.Channel.Name
CmsHttpContext.Current.Channel.Parent.Parent.Paren t.Url

etc. etc. (note how the last example has parent.parent.parent (ad
infinitum).

Thanks for any help...

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available


That is known as the Composite pattern. Your objects contain objects that
contain objects and so on. For example if class A has a property B of type B
and class B has a property C of type C and class C has a property D of type
D then you can get to D from your instance of A with MyA.B.C.D = 5;

HTH

SP
Feb 16 '06 #4
Heres some links to read up on wikipedia.org

Inheritance:
http://en.wikipedia.org/wiki/Inherit...ter_science%29
Abstract Classes (the base class invloving inheritance) :
http://en.wikipedia.org/wiki/Abstrac...ncrete_classes
Polymorphism:
http://en.wikipedia.org/wiki/Polymor...ter_science%29
Compisition/Aggregation (Object relationships):
http://en.wikipedia.org/wiki/Aggrega...programming%29
Delegates : http://en.wikipedia.org/wiki/Delegat...programming%29

Hope that helps!!

sean

Feb 17 '06 #5
Thank you DKode, Morten and SP for your help. I didn't quite follow SPs
example though.

This example I am replying to seems to demonstrate exactly what I am asking
about, though as yet, I am still a little unsure. I am sure though that
there will be a switch in the very near future where everything becomes so
apparent.

I will definately read the URLs that have been posted here. The project I
have got on is a large project (I am no stranger to large projects, just
look at the first URL in my signature) and I want to ensure that it will be
easy for the next programmer to just use the objects I have created.

I think my next step will be learning about IEnumerable and multithreading
(a particular app I have needs threading). I will ask about those when the
time comes if I need to.

Once again, thanks for your help.

Best regards,
Dave Colliver.
http://www.LiverpoolFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:op.s426wibkklbvpo@stone...
Hi David

In .Net each . represents the relationship between an object and its
members.

Take
HttpContext.Current.Application.Contents

The HttpContext class has a static Current property which is a HttpContext
reference.

The HttpContext class also has an Application property which is a
HttpApplicationState reference.

The HttpApplicationState has a Contents property which is a
HttpApplicationState reference.

HttpContext.Current.Application.Contents is just a shorter way of writing

HttpContext cur = HttpContext.Current;
HttpApplicationState appstate = cur.Application;
HttpApplicationState cont = appstate.Contents;

Similarly you can do fun stuff with strings.

string A = "one";
string B = "two";
string C = (A + B).Substring(B.IndexOf(B[0]) + A.Length, (A + B).Length -
(A.Length + B.Length - B.IndexOf(B[1]))).Insert(1, "hr").PadRight(5,
A[2]);

The . doesn't care about where in the line it is, as all it knows is the
object immediatly to the left and right.

To go further in MyApp.ThisClass, simply add a public member to ThisClass

class ThisClass
{
private ThisClass myparent = null;
private string myurl = "http://url";

public ThisClass Parent
{
get{ return myparent; }
}

public string Url
{
get{ return myurl; }
}

public ThisClass()
{
myparent = this;
}
}

The above will let you write

ThisClass myclass = new Thisclass();
myClass.Parent.Parent.Parent.Parent.Url

Usually Parent would point to something else than itself though.

I would read up on inheritance before much else as .Net is full of it.
For instance, everything inherits from System.Object (The Object class in
the System namespace), which means that no matter what object you can
trust it has the GetType and ToString methods.

--
Happy coding!
Morten Wennevik [C# MVP]

Feb 17 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

48 posts views Thread by Joseph | last post: by
40 posts views Thread by post400 | last post: by
385 posts views Thread by Xah Lee | last post: by
17 posts views Thread by Eric Lindsay | last post: by
10 posts views Thread by Jason Curl | last post: by
13 posts views Thread by jay.dow | last post: by
59 posts views Thread by riva | last post: by
reply views Thread by rosydwin | last post: by

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.