|
-
Jan 18th, 2007, 06:32 AM
#1
Thread Starter
Frenzied Member
[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.
-
Jan 18th, 2007, 07:57 AM
#2
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.
-
Jan 18th, 2007, 10:22 AM
#3
Thread Starter
Frenzied Member
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!
-
Jan 18th, 2007, 10:31 AM
#4
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
-
Jan 18th, 2007, 06:13 PM
#5
Re: [2005] Redim ??
VB Code:
Private ReadOnly arrayLength As Integer
Public Sub New()
'...
Dim myCommand As New SqlCommand
myCommand.CommandText = "SELECT COUNT(*) FROM MyTable"
'...
Me.arrayLength = CInt(myCommand.ExecuteScalar())
'...
End Sub
As I said, a ReadOnly variable can be initialised when it's declared or set in a constructor, but nowhere else.
-
Jan 19th, 2007, 05:36 AM
#6
Thread Starter
Frenzied Member
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!
-
Jan 19th, 2007, 05:44 AM
#7
Re: [2005] Redim ??
 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.
-
Jan 19th, 2007, 06:20 AM
#8
Thread Starter
Frenzied Member
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.
-
Jan 19th, 2007, 06:28 AM
#9
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:
'Declare the variable but do not create the array.
Private myArray As Object()
VB Code:
'Create an array with elementCount elements.
Me.myArray = New Object(elementCount) {}
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
|