Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to name instance variables in VB.NET since not case sensitive

Tags:

c#

.net

vb.net

Ok hopefully this does not get deleted because I really am interested in some input on my question but I realize it is a bit subjective. I really like in C# when defining a varaible of a type I can use the same name but just have the 1st letter lower case to differentiate it from its type; I see it done often:

MyType myType = new MyType();

Ok I can't do this in VB.NET because it is not a case sensitive language so I end up doing all sorts of stuff (keep improving) to create a name for the variable. I realize I can use an underscore "_" in front of the type, but I don't really like that, as that is also used to denote class level variables (or 'm' is used too). So I end up always trying something like:

Dim Typ As New MyType()

Ok it works, but I like the C# way better because it makes so much sense. Just make the 1st letter lowercase.

Any suggestions other than the underscore preceeding the variable name on this topic?

Thanks!

like image 855
atconway Avatar asked Dec 21 '25 06:12

atconway


2 Answers

Using the lower case version of the class name is actually a bad practice in actual code. It's good for use in code examples because they only show the basic function of a class. But you never use "integer" (or should not) as variable name because it describes what data is stored, not what it's used for.

Instead you should choose names that describe what the variable is used for.

Edit: It's common practice to use variable names that describe the class because the class often defines what the object is used for. Still you should choose a name specific for the situation, like if you are making a list of errors: instead of stringBuilder you should use allErrorsFound.

Edit example:

class Person
{
    Person friend = new Person();
    public void GetPencil()
    {
        bool pencilFound = 
            friend.AskQuestion(this, "Can I borrow your pencil?")
            .ToLower().Contains("yes");
    }

    public string AskQuestion(Person enquirer, string question)
    {
        // Analyze question implications.
        return "Decision made.";
    }
}
like image 110
MrFox Avatar answered Dec 22 '25 18:12

MrFox


Visual Basic Naming Conventions, per Microsoft. (Found via "naming conventions VB.NET" search.)

Of course, follow house rules and be consistent.

Happy coding.


I only code in C#, however, my rules is: If the instance variable is exposed as part of the type, then UpperCase. If it is private ("not exposed) then camelCase.

I use an underscore prefix only to denote something of interest. For instance, COM objects which are not owned (mostly with local variables) or for backing variables for properties. That is, the use of an underscores in my code is to highlight something and "being an instance variable" is not very noteworthy.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!