Azurewrath

Hi all,

Recently I managed to use something like this:

My.Array ... methods

without conflicting/removing the default one:

Array ... methods


Is this normal Because I was thinking this kind of thing is not doable.




Thanks,
Aw


Re: Visual C# Language Class libraries

timvw

Although it's not entirely clear what you've been able to achieve.. And why you think it is odd...

My magic ball says that you've been experimenting with using statements like "using My = Azurewrath.Library;"...





Re: Visual C# Language Class libraries

Azurewrath

Thanks man. But your magic ball is lying Smile

I was doing something like this:

namespace lighttest
{
public partial class Form1 : Form
{
public Form1 ( )
{
InitializeComponent ( );
}

private void Form1_Load ( object sender, EventArgs e )
{
float [ ] weights = new float [ 5 ];
Array.Sort ( weights );
My.Array.Compact ( );
}
}
}

So as you can see I have 2 Array classes, which doesn't conflict. I always wanted to do this. Using it directly on instances would be cool too. I know you can do it but you can't filter them for specific types.




Thanks,
Aw




Re: Visual C# Language Class libraries

anubisascends

If I understand you, it sounds like you are wanting to use Generics. This will allow stonrgly typed arrays. For instance:

list<string> stringList = new list<string>()

using this will allow you to easily add sstrings, or any specific object or data type that you want, then you can filter, sort, etc.






Re: Visual C# Language Class libraries

Azurewrath

Thanks man.

Can I do this with generics, as I don't know generics:

A method for numeric arrays, like float, integer, etc. So if I have a numeric array, my method will showup, but not for others.




Thanks again,
Aw




Re: Visual C# Language Class libraries

ChunSheng Tang - MSFT

Hi, Aw

Two classes with the same name but in different namespaces is allowed and should be distinguished, so there will be no conflicts between Array and My.Array(i.e. your code is OK.).

Best Regards

Chunsheng Tang






Re: Visual C# Language Class libraries

Azurewrath

Thanks alot. But I am kinda confused.

With namespaces, can I do this:

using My;

Array.DefaultMethods

Array.MyMethods (coming from My.Array)

If not, then what's the point of namespaces as I use mine straight away, without using namespaces.




Thanks,
Aw




Re: Visual C# Language Class libraries

timvw


If not, then what's the point of namespaces as I use mine straight away, without using namespaces.


You're mistaken here... Even the little code that you posted was taking advantage of a "Using System" directive... Your eventhandler has arguments: object sender, EventArgs e (and EventArgs is short for System.EventArgs).

If you want to read more about why it's useful etc http://msdn2.microsoft.com/en-us/library/aa664764(VS.71).aspx is a good resource.







Re: Visual C# Language Class libraries

Azurewrath

Thanks man. For default types I understand, they use namespaces, but what I did was this:

Add a new class, and remove everything (namespace className, etc), except using statements.

Then write something like this:

class ArrayEx
{
public static void DoIt ( )
{

}
}

internal partial class Persona
{
public partial class Array : ArrayEx
{

}
}




Then went onto another file to say:

Persona.Array.DoIt()

Does this uses namespaces too I just thought this wasn't, after I removed namespace declaration VS made. Also I believe this also remove the necessity to write using namespaceName before I use my class library.

I will check the link you sent.




Thanks again,
Aw




Re: Visual C# Language Class libraries

timvw

I would strongly recommend to use namespaces though

A real-life example:

PHP does not support namespaces (so everything ends up in one global namespace, the same as what you seem to be doing.

Virtually everyone has implemented it's own Date class (even an important library provider like PEAR has it's own Date class)..

Serious problems arise when they decided to add a Date class to PHP (as now the 'custom' classes couldn't be used anymore... At best they'd result in ambigious code as the compiler can not know about which Date you're talking...)





Re: Visual C# Language Class libraries

Azurewrath

Thanks again. So you are saying this would be a problem for the keyword Persona, right Because Array is within that name so other Array's wouldn't conflict.

If someone else also used Persona, then the code wouldn't compile right So if I use a name space like:

using MyOwnClass;

then use Persona.Array (from MyOwnClass)

and then some other person will able to use the other Persona (and not mine)

I believe this will still be a conflict, right




Thanks again,
Aw




Re: Visual C# Language Class libraries

timvw

What better than experiment yourself

Create a console application, create two library projects, name them libA and libB (by default a Class1 is added, remove the namespace { } from it, so that they end up in the same "default" namespace.

- In your console application add references to both assemblies. (compile, and notice that it works up to here).
- Now add a line like Class1 c1 = null; in your main.. notice that the compiler complains that there are two classes with the name Class.

- If both Class1 classes existed in their own namespace you could clearly define exactly which Class1 you want to use. That is the power of namespaces. (Hope it's clear now Wink)






Re: Visual C# Language Class libraries

Azurewrath

Thanks again. I tried it. Now I am a little confused which one to choose. I understand the concept of namespaces, but they seem very similar to nested classes, right


Because when I tried the differences was these:

1. Namespaces allow me to use using keyword.
2. Namespace.ClassName only color codes ClassName, while nested Classes color codes both class names.


Similarities:

1. They both give you a unique identifier with top level name, (namespace name, and top class name).
2. Writing Namespace.ClassName or TopClass.ClassName does not conflict with default exisiting names, like ClassName.


So from what I can see, namespaces doesn't solve the conflict between 2 classes with the same name, unless at least of them are used with Namespace.ClassName form, and the other uses the using keyword.

So the only way for nested global classes to conflict with others with the same name would be to share the top level name, that's why you need to use a very unique name.


Do you think I miss something here. Smile




Thanks again,
Aw




Re: Visual C# Language Class libraries

Figo Fei - MSFT

Hi Aw,

If I understand you right, you are talking about same class name conflict with namespace name.

For namespace, we can use global keywords with namespace alias qualifier (::) to locate the namespace instead of the class with the same name.

For example:

Code Snippet
class System
{ }
static void Main()
{
global::System.Console.Write("whatever here");//call Console.Write
System sys = new System();//System class instance
}

Thanks






Re: Visual C# Language Class libraries

Azurewrath

Thanks man.

I see what you mean.

But what's the point of writing global::, if System.Console.Write does the same thing, IE providing the fully qualified name I just don't know Smile




Thanks,
Aw