Writing stuff

  • Thread starter Thread starter David
  • Start date Start date
D

David

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.Parent.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
 
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/library(d=robot)/dfb3cx8s.aspx
 
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.
 
David said:
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.Parent.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
 
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 said:
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.
 

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

Similar Threads


Back
Top