Hey, is there a magical way to shorten variables in Visual C# as in PHP? For example in place of

$user_nickname, $user_email, $user_rank.
They could use
$user["nickname"], $user["email"];, $user["rank"].

Now if you ask me, I think the second row looks much more organized. Is such thing possible in C#?
I know about enum function, but so far all examples I've met were only able to put things into list, like {0, 1, 2, 3, 4}, what I would need is {"hi" => "hullo", "bye" => "says crocodile", "dingus" => "shmingus"} and then being able to recall it with something would look something like the row from above (the lower one).

Recommended Answers

All 7 Replies

You could always create a class or struct and then you could use notation like:
user.name, user.email.

You could use a dictionary and enter key/value pairs.
Or, if you REALLY wanted it to look and operate like your example you could create a datatable or datarow with the columns named appropriately. Then you get exactly
user["name"], user["email"], etc if that is a requirement.

@hericles Thanks for answering, I tried to create things like you said the "user.name", "user.email". But it didn't really work.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication5 {
    class Program {
        static void Main(string[] args) {
            string ux.name = "RikTelner";
            Console.Write(ux.name);
        }
    }
}

Before I even tried to compile that, Visual Studio red-underlined ux.name in both cases. I assume I'm an idiot, and that I try to set variable that with a name of file "ux.name.dll".

Could you help me with it? I'm not really advanced, I'm not as big newbie though, as I was when I first posted here about C# (which was about 4 days ago).

The compiler has not the faintest idea what ux.name means, hence your error in the code.
Try something like this:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

namespace ConsoleApplication5 {

    public class Myux_class
    {
        public Myux_class() //default constructor
        {

        }

        string name;
        string email;
        int age;
        //....
    }

    class Program {
        static void Main(string[] args) {
            Myux_class ux = new Myux_class();
            string ux.name = "RikTelner";
            Console.Write(ux.name);
        }
    }
}
commented: Great answer, thank you! +2

Hey. I just copied your script and Visual prompted me two errors with ux.name. Now, If I had to do anything, I don't know, so far I analyze the code you provided, you publicly and openly define variable type, assign "RikTelner" to it and then recall it.

The two errors I get.

string ux.name = "RikTelner";
Part ux.name, says,
; expected

Console.Write(ux.name);
Part name, says,
'ConsoleApplication5.Myux_class.name' is inaccessible due to its protection level

Firstly I thought that you may have misplaced }, you just opened Myux_Class() and closed it right after, I tried to fix it, but after a second it says that name, email and age are never used, meaning that you placed them correctly and set them back.

Also, "inaccessible due to its protection level", I tried changing
static void Main(string[] args) { to
public static void Main(string[] args) {
So maybe, somehow it would work, but, nope.
I don't know, everything I try to adjust seems to be called upon damnation, I don't even... sigh

Blame it on me!
Line 16 to 18 should have the access modifier public in front.
Keep Main satic as it was.
On line 25 remove the word string.
Success!

commented: Ding ding ding ding! +0

Yea, works exactly right. Everything's fine, just one question. We, actually, You, made class Myux_class(), once it is defined, it is set, but is it possible to restart these settings once more? For example if some file had changed and the username had to change. (just an example).

I actually would use properties like this:

public class Myux_class
    {
        public Myux_class() //default constructor
        {

        }

        public string name{ get; set; }
        public string email { get; set; }
        public int age { get; set; }
        //....
    }

But in fact is doesn't matter much in this case. But as you saw I first declared the fields without the word public and you got errors, because a field without an access modifier is considered private.
Don't know exactly what you mean, you can always create a new instance of a class or even an array of this class if you want, or just change the value of the field.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.