|
-
Oct 24th, 2002, 05:15 PM
#1
variable declaration....where should I declare them?
I've been declaring all of my variables on the top part of my class. I'm just wondering if it's a bad practice to declare some of the variables in the middle of the class?
Some of the variables in my class are only used for a few functions. Is it bad to group all the functions and those variables in a region? or should I declare all the variables in the class in one place? (I know it doesnt make a difference in functionality of the app, but I'm just wondering if it's considered a "bad habit" or not )
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Oct 24th, 2002, 05:58 PM
#2
Hyperactive Member
Re: variable declaration....where should I declare them?
Originally posted by MrPolite
I've been declaring all of my variables on the top part of my class. I'm just wondering if it's a bad practice to declare some of the variables in the middle of the class?
Some of the variables in my class are only used for a few functions. Is it bad to group all the functions and those variables in a region? or should I declare all the variables in the class in one place? (I know it doesnt make a difference in functionality of the app, but I'm just wondering if it's considered a "bad habit" or not )
I dont think it really matters, just as long as your code is efficient and easy to read!
-
Oct 24th, 2002, 06:01 PM
#3
Sleep mode
so you know that but you don't know this
if you want to collect your variables in specific structure :
[COLOR=royalblue]
Code:
#Region "My Variables by Pirate , :-"
#End Region
The #Region directive lets you specify a block of code that you can expand or collapse when using the outlining feature of the Visual Studio Code Editor. #Region statements support block semantics (such as #If...#End If), meaning that the start and end must be in the same code block.
from MSDN
Pirate
Last edited by Pirate; Oct 24th, 2002 at 06:06 PM.
-
Oct 24th, 2002, 07:43 PM
#4
If the variable is only used in a function then it should probably be declared in the function, for scope reasons. If its needed for the whole class then it should go at the top of the class to make things easier to read.
-
Oct 24th, 2002, 10:21 PM
#5
Originally posted by Edneeis
If the variable is only used in a function then it should probably be declared in the function, for scope reasons. If its needed for the whole class then it should go at the top of the class to make things easier to read.
well not for one function. It's shared by 3 functions. I don't know. It was just getting a little confusing, having 20 variables at top of the class
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Oct 24th, 2002, 10:30 PM
#6
PowerPoster
It doesn't matter. If it is easier for you, then do it that way. I would think the majority of people don't do it your way, but it isn't 'wrong' or anything.
-
Oct 24th, 2002, 10:43 PM
#7
thanks
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Oct 25th, 2002, 04:37 AM
#8
Lively Member
Declare your variables when you need them, using the smallest scope possible. For example:
Code:
Public Class DummyExample
Const range As Integer = 10
Private _mode As Integer '_mode will have class scope because is needed so.
Private _data As ArrayList '_data will have class scope because is needed so.
Sub New()
mode = 1
_data = New ArrayList()
ApplyMode()
End Sub
Private Sub ApplyMode()
Dim filter As Integer 'index will be needed here, so here I declare it. It will be within the function scope from now on. The fact that it is the first line of code in this function is merely coincidence.
If _mode > 0 Then
Dim temp As Integer 'temp will be needed here, so here I declare it. It will be within this then...else scope from now on.
temp = _mode * range
filter = temp
Else
filter = 1
End If
Dim index As Integer 'index will be needed here, so here I declare it. It will be within the function scope from now on.
For index = 0 To range
Dim a As Integer 'a will be needed here, so here I declare it. It will be within this _for loop_ scope from now on.
a = index * mode * filter
_data.Clear()
_data.Add(a)
Next
End Sub
Public Property mode()
Get
Return _mode
End Get
Set(ByVal Value)
_mode = Value
ApplyMode()
End Set
End Property
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|