Results 1 to 9 of 9

Thread: [2005] [Resolved] Redim ??

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    [2005] [Resolved] Redim ??

    Hi All,

    I have defined a public constant called iNoDepartments and set it to 13. I use this constant throughout my app in setting arrays and for loops. My question is I want to change it so that the number can be variable. Basically I want to check the Departments table at the start of my app to see how many records there are and then set my constant to that number thus setting my arrays / for loops correctly. Does anyone know if this is possible and how?

    Thank you all for any advice,

    Jiggy!
    Last edited by Jigabyte; Jan 19th, 2007 at 05:37 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Redim ??

    The whole point of a constant is that it is constant, hence the name. If you want to change the value then it's a variable, not a constant. If you only want to set the value once then you should declare a ReadOnly variable. ReadOnly variables can be set to a value on the line on which they're declared or in a constructor but nowhere else. Also, ReadOnly variables can be set using a function or property value, while constants can only be set using literals or other constants.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: [2005] Redim ??

    Hi, thanks for that. What I mean is I want to now have a variable that I can set when my app runs. How do you set a variable array? What I mean is my array depends on how many rows are going to be in my table.

    Cheers,

    Jiggy!

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2005] Redim ??

    Run a SQL count statement on the department table and set the variable to that result.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Redim ??

    VB Code:
    1. Private ReadOnly arrayLength As Integer
    2.  
    3. Public Sub New()
    4.     '...
    5.  
    6.     Dim myCommand As New SqlCommand
    7.  
    8.     myCommand.CommandText = "SELECT COUNT(*) FROM MyTable"
    9.  
    10.     '...
    11.  
    12.     Me.arrayLength = CInt(myCommand.ExecuteScalar())
    13.  
    14.     '...
    15. End Sub
    As I said, a ReadOnly variable can be initialised when it's declared or set in a constructor, but nowhere else.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: [2005] Redim ??

    Hi,

    Thanks for that, I can still use the redim in VS2005 so I define an array of 1 and then redim it once I have the record count.

    Thanks again all,

    Jiggy!

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Redim ??

    Quote Originally Posted by Jigabyte
    Hi,

    Thanks for that, I can still use the redim in VS2005 so I define an array of 1 and then redim it once I have the record count.

    Thanks again all,

    Jiggy!
    Non, no, no. Why create an array at all if you aren't going to use it? Just declare the variable without specifying an upper bound an no array will be created. You can then either use ReDim or, preferably, invoke an array constructor at a later time when you know how many elements you need.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,806

    Re: [2005] [Resolved] Redim ??

    We have deep confusion here! I need to use an array, that was the whole point of this post. I need to store a list of information but the array I want to store it in depends on the number of records I am getting the info from so I will only know in runtime not design time.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] [Resolved] Redim ??

    You said that you create an array with one element, then you use ReDim later to create another array. What's the first array with one element for? You don't need it so don't create it. Simply declare the variable and don't create the array until you need it.
    VB Code:
    1. 'Declare the variable but do not create the array.
    2. Private myArray As Object()
    VB Code:
    1. 'Create an array with elementCount elements.
    2. Me.myArray = New Object(elementCount) {}
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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