0

(Apologies, this is basically my first C# program, and I'm translating C++)

I defined a "user" struct like this:

struct user
{
  string usrnm;
  string pw;
}

It is said here that one can create a pseudo typedef in C#

namespace _1._0
{
  class Program
  {
    struct user
    {
      string usrnm;
      string pw;
    }
    using (userArr = Dictionary<int, user>);
  }
}

But this doesn't work; it throws me the error saying:

Invalid token 'using' in class, struct or interface member declaration

and

The type or namespace name 'userArr' could not be found (are you missing a using directive or assembly reference?)

So why doesn't this work, and how can I get it to work?

Community
  • 1
  • 1
Lee Yi
  • 517
  • 6
  • 17

3 Answers3

2

The full example should be:

using userArr = System.Collections.Generic.Dictionary<int, _1._0.Program.user>;

namespace _1._0
{ 
    // You could place it here, nearly equivalent:
    // using userArr = System.Collections.Generic.Dictionary<int, Program.user>;

    public class Program
    {
        public struct user
        {
            string usrnm;
            string pw;
        }
    }
}

and then inside that file you can:

userArr myDictionary = new userArr();

You use the using like using System;, not like using (something) {}. That one is another type of using :), so you can place it outside a namespace declaration or directly inside a namespace declaration. Not inside a class/struct/method body. Note that, as written by Jeppe, you have to use the full namespace for Dictionary<>.

Note that this using will "work" only inside the file where you "used" it. Clearly you can reapply it to other source files.

But note that I concur with CodeCaster. The only time I use the using ... = is when I have classes with the same name in multiple namespaces and I need to be able to distinguish between them (for example I have MyNamespace1.MySubnamespace1.MyClass and MyNamespace2.MySubnamespace2.MyClass... I could every time write the full name or I could add two using M1 = MyNamespace1.MySubnamespace1;using M2 = MyNamespace2.MySubnamespace2; and then M1.MyClass and M2.MyClass)

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • In the `using` alias directive in the top, can one really leave out the namespace for `System.Collections.Generic.Dictionary<,>`? – Jeppe Stig Nielsen Mar 27 '15 at 11:37
  • @JeppeStigNielsen I was not including "system" namespaces. But then, as written, the example could compile (with the addition of that namespace) but it wouldn't do anything. – xanatos Mar 27 '15 at 11:38
  • Even with an earlier `using` directive, the `using` you show will not work. You need the full name of `System.Collections.Generic.Dictionary<,>`. **Edit:** If you move the `using` alias inside the `namespace` and keep the `using System.Collections.Generic;` outside, it appears to work with only `using userArr = Dictionary;`. – Jeppe Stig Nielsen Mar 27 '15 at 11:40
  • 1
    @JeppeStigNielsen Fascinating... I didnt know... If I think about it for a moment, it's clear why... You can't do `using System; using Collections;`... The `using` don't use other `using` – xanatos Mar 27 '15 at 11:43
1

Leave the C++ methodologies at C++. You don't need any of this.

Just use a class with properties and use the proper syntax to instantiate the dictionary.

namespace _1._0
{
    public class User
    {
      public string Username { get; set; }
      public string Password { get; set; }      
    }

  class Program
  {
    static void Main(string[] args)
    {
        var userDictionary = new Dictionary<int, User>();
    }
  }
}

I don't know why you want to use an alias, but if it's to prevent typing long type names multiple times, check out var as used above.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • 1
    I'll add "and remember that you can often use the `var` keyword when you don't want to write the same long things too many times" :-) (as you've done) – xanatos Mar 27 '15 at 11:24
0

I figured it out. Coming from a C++ background, where everything needs to be declared beforehand, I assumed I would have to do the same with C#, but I don't.

Placing the using statement before the struct user declaration works fine.

(Sidenote: I removed all such "typedefs" from my code")

Lee Yi
  • 517
  • 6
  • 17