Results 1 to 8 of 8

Thread: variable declaration....where should I declare them?

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    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!!

  2. #2
    Hyperactive Member
    Join Date
    Feb 2002
    Posts
    261

    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!

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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.

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  5. #5

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    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!!

  6. #6
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    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.

  7. #7

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    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!!

  8. #8
    Lively Member hbandarra's Avatar
    Join Date
    Aug 2002
    Location
    Lisbon, PT
    Posts
    66

    Red face

    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
  •  



Click Here to Expand Forum to Full Width