Variable Naming Conventions To Illustrate Variable Type And Where Variables Are Declared
Aug 24, 2009
I am looking for a good resource on variable naming conventions to illustrate variable type and where variables are declared. So I will have public variables, Private variables, private or local variables. I also may want to declare variables with the same name in different class code (i.e. in the code behind different forms). I am assuming good coding would dicatate a prefix for declaration location.
View 4 Replies
ADVERTISEMENT
Oct 21, 2010
In the example below, what would you name the parameter given that it is used to initialize the property FromDate?For class constructor methods, I like to have the name of the constructor parameter variable match the name of the property which is being initialized. For example, the parameter "fromDate" is used to initialize the module level variable "_FromDate" with the statement _FromDate = fromDate. Likewise, I could have alternatively written Me.FromDate = fromDate.
Proponents of C#'s case sensitivity would probably say that using a leading lower cased letter for the param variable name, which I believe is MS convention, is an acceptable approach to distinguish it from the Property of the same name but different casing.
However, VB is not case sensitive, which I generally appreciate. In the following example, I am using a param name that matches the property name, 'fromDate," and VB refers to the local instance when there is ambiguity. However, many would probably argue that this "ambiguity" introduces the opportunity for the developer to get confused and not realize which variable is being used. For example, my intent below was to have TWO params passed in, "fromDate" and "toDate" but I accidentily ommited one and as a result, the VB.NET did not warn me of the mistake because it assumed that the statement _ToDate = ToDate was equivalent to _ToDate = Me.ToDate instead of informing me that the variable on the right side of the assignment statement was undeclared.
Public Class Period
Property FromDate As Date
Property ToDate As Date
[code]....
In my judgement, we should have a convention for prefixing all parameter variable with a prefix, but hasn't the use of prefixes been discouraged by Microsoft? For example:
Public Sub New(ByVal paramFromDate As Date, paramToDate As Date)
..or maybe it could be shortened to pFromDate, pToDate...
View 3 Replies
Jun 9, 2010
I am generally not one to engage in subjective arguments over matters like variable naming, code formatting, etc. So I have no intention of starting an argument here.I just came across this (old) blog post which recommends not prefixing member variable names:
[Code]...
I get it: member variables can be lower camelCase, and public properties/methods can be PascalCase. But VB.NET is case-insensitive, so you can't really give a private member the same name as a public property except with a lower case first letter.I've generally prefixed member variables with an underscore, but I've been told that's not idiomatic.
So really I'm just curious: how do you name your member variables in VB.NET? And is there a "standard" way?
I'm not asking because I believe there's a "right" way or because I particularly want to change my style, and certainly not because I have any desire to tell others they're "wrong." Like I said, I'm just curious.
View 7 Replies
Jan 10, 2011
Say that i have the following code that parse about 20k records from the DB.
Code #1
vb.net While reader.Read()
list.Add(If(Integer.TryParse(reader(0).ToString, 0), Integer.Parse(reader(0).ToString), 0))
End While
And then another code which does the very same thing but it seems to be a little cleaner.
Code #2
vb.net While reader.Read()
Dim storeowner As Integer = 0
Integer.TryParse(reader(0).ToString, storeowner)
list.Add(storeowner)
End While
This is what i am confused about; does the compiler creates a new variable automatically when i use the if statement without strictly declared variable? What approach is better in sense of performance?
View 2 Replies
Jan 4, 2011
I've been programming for many years in many languages and I've seen the variable declarations conventions change. With VS 2010 I've noticed you don't have to declare the variable type. I work alone so I can use any method I want. But, someday I may want to sell some of my code so I would like to keep my programming conventions current.
In VS 2010 which of these would be considered the best convention?
Dim srtFirstName = "Bob"
Dim strFirstName As String = "Bob"
Dim FirstName = "Bob"
Dim FirstName As String = "Bob"
View 5 Replies
Mar 25, 2011
What everyone uses for naming conventions?
View 3 Replies
Mar 6, 2012
In one function I have the variable: dim StrProjectNumber = UCase("23-EXP-16284")
However in another form I want to use that same variable in a SQL query, and when I try it says the variable is not declared. Do I have to make a global variable or is there a way around it? If so, how would I go about declaring a global variable?
View 3 Replies
Apr 14, 2010
I have this function:
Public Sub DoStuff(ByVal type as System.Type, ByVal value as Object)
End Sub
The 'value' argument is always an array of the same type as 'type'. How can I loop through the values of the array?
I'd like to be able to do something like this:
DoStuff(GetType(Integer), New Integer(){1,2,3})
Public Sub DoStuff(ByVal type as System.Type, ByVal value as Object)
//Strongly types arr as Integer()
[Code].....
View 3 Replies
Mar 25, 2011
I'm relatively new to .NET and am wondering how people handle naming their private variables and the public properties that access them. Like if you want to be able to just read it, but not write to it.
[Code]...
So far I've taken to putting a 'l' (for local) in front of the all the private variables so as to be able to use the full name for the property. Is there a better way around this, or do you just always have to have different names for private variable/public properties? If so, what sort of conventions do people use?
Its not a huge deal, its just a minor annoyance and I was wondering if I was missing something.
View 4 Replies
Jan 5, 2011
iv got visual basic 6.0 and im trying to change the naming conventions because i need to change them to 2008 standards the code is for a basic book shop.im looking ofr the code for the calcualte button on a till.you enter the title,quantity and price.then it updates for extended price 15% discount and the discounted price. There is also a running total of no of books sold and the total no of dicount given in € the code is
[Code]...
View 5 Replies
May 18, 2012
Public Class Form1
Dim x, c, number(0 To 19) As Integer
Dim s As Integer
[CODE]...
The variables in the brackets [example] do not actually have brackets in them in the original code. These are where the issues are. For both variables, it says, "The type for variable [variable] will not be inferred because it is bound to a field in an enclosing scope. Either change the name of [variable], or use the fully qualified name (for example, 'Me.[variable]' or 'MyBase.[variable]')." Now, I'm not entirely sure if this is a stupid question or not, as I'm used to VB '98 because that's what we use in my programming class at High School. let me know why this won't work.
-Note: The intention of this program is to continually loop the generation of numbers for this list until I tell it to stop. Button1 ends the program, Button2 generates the list one time only, Button3 is supposed to loop the generation of the list, and Button4 is supposed to end the loop, but not the program.
View 9 Replies
Jan 25, 2012
I have a class (let's call it foo) that has several public properties.
private _fee as object
private _fie as object
private _fo as object
[Code]....
So far this seems to work... I'm just worried that something might get screwed up scope wise.
What is the normal way someone would name the constructor variables in a class like this?
View 9 Replies
Oct 30, 2009
I am trying to create a number of objects depending on the width of a row of an array, is there a way I can change the name of the variable being Dim 'd as I don't know how wide the array will be.
I have been trying something along the lines of:
pseudocode
For x as integer = 0 to UBound(MyArray,2)
Dim Object & CStr(x) As Object Type
Next
View 3 Replies
Sep 17, 2011
I have the following code that I am using to parse out a test file. I am getting variable conversion error in Sub Main() when I assign file = Read(). The return value of Read() is a TextFieldParser type. How do I assign the proper variable type to "file" so I can write the output to a text file?
Function Read()
Using MyReader As New FileIO.TextFieldParser("C:UsersColinDesktopParse_Me.txt")
Dim currentRow As String
[Code].....
View 3 Replies
Oct 10, 2009
I'm aware that questions like this have been asked before and I doubt it's possible, but I just wanted to make 100% sure that it isn't.In VB.net or C# (either language, it doesn't matter), I want to cast a variable to a type represented by another Type variable. Here's an example of the kind of code that would be needed in C#:
Object castMe = new Foo();
Type castTo = typeof(Foo);
Foo beenCast = (castTo.GetRepresentedType())castMe;
[code].....
View 3 Replies
Dec 2, 2009
What's the C# equivalent of
<% dim name %>
so you can use it for web forms in PayPal API integration?
View 3 Replies
Feb 24, 2011
The following is a screenshot of the problem: What can I do?
View 3 Replies
Sep 22, 2009
I have an object that I know was originally declared was an enum, but I need tothe specificnum type. I'm aware of the GetType method and it appears to work correctly, but I can't figure out how to go from there to a direct type comparison For reference types, you can use Typeof(object1) Is Class1, but this doesn't seem to work for value types.
View 3 Replies
May 17, 2010
when I was using vs2008, code below was working without defining mystudent variable.It was recognizing type automaticly because of mystudents list of object.Has anyone know the reason why compiler returns error that "It is not declared"?
View 16 Replies
Jun 17, 2008
If I modify this code (written by Luis Esquivel) because I want to dimension a variable and cast it either as a polyline or a polyline2d inside a select statement...it doesn't recognize the variable outside the select statment. I can't dimension it outside (or rather) before the select statement because I don't yet know how I want to cast it.
' by Luis Esquivel on June 29 2007
' using the idea/algorithm by John F. Uhden,
' one of the masters that use to frequent the customization ng of AutoDesk
[Code]....
View 7 Replies
Sep 13, 2011
[code]...
Private Sub btnData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnData.Click[code]....
View 1 Replies
Jan 11, 2012
I'm using DAO (been asked not to use ADO.NET) to update an Access database. I'm currently thus far, however, VB2008 is telling me that the variable "daoengine" is not declared before it is used. What am I doing wrong in the following code?
Function update_db()
Dim daoengine As DAO.DBEngine
Dim dbs As DAO.Database
[Code]....
View 2 Replies
Aug 11, 2011
I have a simple IF THEN ELSE block which checks for the presence of a querystring and then checks if it is set. The idea is that if no querystring, the form is empty and people can enter a new form. If there is a querystring and it isn't blank, a db query for the form data occurs, fields are populated and a user can update the form.
so here is my code block.
Dim strQueryStingCheck As String
If Not (Request.QueryString("pid") Is Nothing) Then ' is there a querystring?
If Request.QueryString("pid").ToString <> "" Then ' does it have a value?
[code]....
I am getting the "Variable is not declared; it may be inaccessible due to its protection level" for strQueryStringCheck ONLY on the Else code block - that is when I set strQueryStringCheck to "e". I can't figure it out.I've looked at other posts, particularly this one. and it was helpful. I can make the error go away, but I want to understand why I am getting it in the first place. I declared it within the subroutine. And if I was doing something wrong, shouldn't it throw an error on BOTH blocks of the IF THEN ELSE block? It doesn't when I set strQueryStringCheck to "u". Why only in the ELSE block?
View 2 Replies
Feb 27, 2012
catch(ArgumentOutOfRangeException aooreE){}
catch(NullReferenceException nreE){}
I got the error state that The variable 'aooreE' is declared but never used.
View 6 Replies
Sep 15, 2011
In C#, I can declare an array variable like this: object[] Parameters;
And initialize it like this: Parameters = new object[20];
In VB, declaring and initializing an array is easy:
Dim Parameters(19) As Object
Dim Parameters As Object(19) ' Alternative Syntax
How would I initialize an array variable that has already been declared in VB.NET? Parameters = New Object(19) doesn't work.
For example, how would I translate the following to vb.net?
int value = 20;
object[] Parameters;
if (value > 10)
{
Parameters = new Object[20];
}
View 3 Replies
Nov 18, 2011
I have some web pages that include other pages, and I need to check if a variable (a string) has been declared in the page or not.
I was exploring try catch and finally, but im always getting a compiler error saying the variable doesnt exits.[code]...
View 3 Replies
Jul 26, 2010
I have a text file to read, at the first line will have a size of array, that means I cannot guess size of array, and I have to create a array in side a sub (Local Variable). Then, I want to use value of array in another sub, but I cannot use value of variable of another sub. What I have to do to make an array con be used in every sub/function in a class? Also, I've post an example file, code and error list.
[Code]...
View 1 Replies
May 24, 2012
I am trying to run this line of code: Dim OrderedFiles() As String = Directory.GetFiles(FilePath).OrderBy(x >= x.CreationTime)
I get an error on x saying x is not declared. I have my project set to Option Strict Off and Option Infer On. If I turn ON Option Strict then I get thousands of errors from the project(it is inherited) and I don't have the time to fix all of them, but x no longer gives me an error. I have googled until I want to throw my computer out the window.
I was hoping for a more elegant solution but here is what I came up with to solve this particular problem.
[Code]...
It is not particularly elegant but it does the job. I was hoping for a one liner LINQ solution and I just don't have the background in LINQ to know how to do the job, time to go buy a book.
View 2 Replies
Nov 17, 2010
I have 2 public variables that I declared in form1 of an application. I am trying to call that variable in form2 and then pass that variable in a sql query. [code]...
View 14 Replies
Nov 15, 2010
I have a public variable that I declared in form1 of an application. I am trying to call that variable in form2 and then pass that variable in a sql query. If I declare:
Public Class Form1
Public payPeriodStartDate, payPeriodEndDate As Date
How then to I declare that variable in form2 and how to I pass it to my sql query.
Here is the code I have for form1: [Code]
and when I debug this form, no data will appear. I'm sure that it's something wrong with how I'm either calling the variable in form2 or in the query. Can anyone offer any assistance on this?
View 9 Replies