#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmers Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use it's exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# help with error

Posts: 99Member
hey im sorta new to vb and i am trying to create this thing that creates a small test type thing. i finished but it is giving this error. Compile Error: Constant Expression Required

Heres the part that has the error, sry its kinda long.
[code]
Public Function CreateTest(TotalQs As Integer, TotalOperands As Integer, _
SetOperand, OperandLength As Integer, ProblemType As String)

Dim XNum
Dim ProbType(3) As String
Dim TempNum As Long, PType() As String, ProbTypeFinal As Integer
Dim UsePType(1 To TotalQs) As Integer
Dim ProbNums(1 To TotalQs, 1 To TotalOperands) As String, UsedSetOperand As Boolean
Dim Problems(1 To TotalQs) As String

' creates the highest number an operand can be

XNum = 1
For i = 1 To OperandLength
XNum = XNum & "0"
Next
XNum = XNum - 1

' finds which problem types are used and what problems will use which type

For i = 0 To 3
ProbType(i) = ""
Next
If InStr("+", ProblemType) Then ProbType(0) = "+"
If InStr("-", ProblemType) Then ProbType(1) = "-"
If InStr("*", ProblemType) Then ProbType(2) = "*"
If InStr("/", ProblemType) Then ProbType(3) = "/"

TempNum = 0
For i = 0 To 3
If ProbType(i) <> "" Then
PType(TempNum) = ProbType(i)
TempNum = TempNum + 1
End If
Next
ProbTypeFinal = TempNum - 1

For i = 1 To TotalQs
UsePType(i) = Random(0, ProbTypeFinal)
Next

' this creates the numbers for each problem

TempNum = 1
For i = 1 To TotalQs
UseSetOperand = False
For Num = 1 To TotalOperands

If UseSetOperand = False And SetOperand <> "" Then
ProbNums(i, Num) = SetOperand
End If

If UseSetOperand = True Or SetOperand = "" Then
If TempNum <> TotalOperands Then
ProbNums(i, Num) = Random(0, XNum) & PType(UsePType(i))
Else
ProbNums(i, Num) = Random(0, XNum)
End If
End If
UseSetOperand = True
Next
Next

' this puts the numbers together

For i = 1 To TotalQs
For Num = 1 To TotalOperands

Problems(i) = Problems(i) & ProbNums(i, Num)

Next
Next

Text1.Text = Problems(1)

End Function
[/code]

it is highlighting the TotalQs variable right in the begining.

· ·

• Posts: 12Member
: hey im sorta new to vb and i am trying to create this thing that creates a small test type thing. i finished but it is giving this error. Compile Error: Constant Expression Required
:
: Heres the part that has the error, sry its kinda long.
: [code]
: Public Function CreateTest(TotalQs As Integer, TotalOperands As Integer, _
: SetOperand, OperandLength As Integer, ProblemType As String)
:
:
: Dim XNum
: Dim ProbType(3) As String
: Dim TempNum As Long, PType() As String, ProbTypeFinal As Integer
: Dim UsePType(1 To TotalQs) As Integer
: Dim ProbNums(1 To TotalQs, 1 To TotalOperands) As String, UsedSetOperand As Boolean
: Dim Problems(1 To TotalQs) As String

This is just a guess but if you're getting a Constant expression required error means that the variable in question has Nothing assigned to it, that is to say that TotalQs is empty or set to Zero.

Without assigning a value to your variable when you use it in declaring an array as you have:

Dim UsePType(1 To TotalQs) As Integer

You are essentially declaring an array with a size of 1 to 0. If VB is highlighting TotalQs as you say then bring your mouse over the highlited area and it should popup with its value, and I am willing to bet a buck that it is empty or set to zero.

So what you need to do is pass a value to TotalQs when your function is called. I gather by the variable name and what you are trying to accomplish that TotalQs represents the number of questions in the test. You need to somehow count the number of questions and pass that value to TotalQs. That should fix that problem; however, in reading your code I have spotted some more problems that will inevitably popup when you get passed the first problem.

1. Your For Next loops like this one
: For i = 0 To 3
: ProbType(i) = ""
: Next
Next should have the variable pass to it like this
: For i = 0 To 3
: ProbType(i) = ""
: Next i

2. The If Then statements directly below the above loop are missing the End If statement. This will cause an if without end if error.

3. When nesting For Next and/or If Then statements you need to indent each nested statements both for readability and to avoid compiler hangups. Consider your nested statements here:

: TempNum = 1
: For i = 1 To TotalQs
: UseSetOperand = False
: For Num = 1 To TotalOperands
:
: If UseSetOperand = False And SetOperand <> "" Then
: ProbNums(i, Num) = SetOperand
: End If
:
: If UseSetOperand = True Or SetOperand = "" Then
: If TempNum <> TotalOperands Then
: ProbNums(i, Num) = Random(0, XNum) & PType(UsePType(i))
: Else
: ProbNums(i, Num) = Random(0, XNum)
: End If
: End If
: UseSetOperand = True
: Next
: Next
I have found that when you nest statements as you have the compiler spits out errors, but if you indent as below then the errors usually go away.
Try:
: TempNum = 1
: For i = 1 To TotalQs
: UseSetOperand = False
: For Num = 1 To TotalOperands
:
: If UseSetOperand = False And SetOperand <> "" Then
: ProbNums(i, Num) = SetOperand
: End If
:
: If UseSetOperand = True Or SetOperand = "" Then
: If TempNum <> TotalOperands Then
: ProbNums(i, Num) = Random(0, XNum) & PType(UsePType(i))
: Else
: ProbNums(i, Num) = Random(0, XNum)
: End If
: End If
: UseSetOperand = True
: Next Num
: Next i

Do the same thing with these nested for next loops
: For i = 1 To TotalQs
: For Num = 1 To TotalOperands
:
: Problems(i) = Problems(i) & ProbNums(i, Num)
:
: Next
: Next

Finally are you trying to generate a random number here:

ProbNums(i, Num) = Random(0, XNum)

I didn't see any declaration for an array called Random and I believe that Random() is the C++ method for random number generation. VB random number generation goes like this:

Randomize (Timer)
MyVariable = Int(Rnd * 6)'Generates random number from 0 to 6

If zero is not wanted as a result then do it like this:

Randomize (Timer)
MyVariable = Int(Rnd * 5) + 1'Generates random number from 1 to 6

Well that's all I could find, hope this helps.
· ·
• Posts: 99Member
I am passing a value to it wen i am using the function but it still was giving me that error. is it because im passing it using textbox text?

: : hey im sorta new to vb and i am trying to create this thing that creates a small test type thing. i finished but it is giving this error. Compile Error: Constant Expression Required
: :
: : Heres the part that has the error, sry its kinda long.
: : [code]
: : Public Function CreateTest(TotalQs As Integer, TotalOperands As Integer, _
: : SetOperand, OperandLength As Integer, ProblemType As String)
: :
: :
: : Dim XNum
: : Dim ProbType(3) As String
: : Dim TempNum As Long, PType() As String, ProbTypeFinal As Integer
: : Dim UsePType(1 To TotalQs) As Integer
: : Dim ProbNums(1 To TotalQs, 1 To TotalOperands) As String, UsedSetOperand As Boolean
: : Dim Problems(1 To TotalQs) As String
:
:
: This is just a guess but if you're getting a Constant expression required error means that the variable in question has Nothing assigned to it, that is to say that TotalQs is empty or set to Zero.
:
: Without assigning a value to your variable when you use it in declaring an array as you have:
:
: Dim UsePType(1 To TotalQs) As Integer
:
: You are essentially declaring an array with a size of 1 to 0. If VB is highlighting TotalQs as you say then bring your mouse over the highlited area and it should popup with its value, and I am willing to bet a buck that it is empty or set to zero.
:
: So what you need to do is pass a value to TotalQs when your function is called. I gather by the variable name and what you are trying to accomplish that TotalQs represents the number of questions in the test. You need to somehow count the number of questions and pass that value to TotalQs. That should fix that problem; however, in reading your code I have spotted some more problems that will inevitably popup when you get passed the first problem.
:
: 1. Your For Next loops like this one
: : For i = 0 To 3
: : ProbType(i) = ""
: : Next
: Next should have the variable pass to it like this
: : For i = 0 To 3
: : ProbType(i) = ""
: : Next i
:
: 2. The If Then statements directly below the above loop are missing the End If statement. This will cause an if without end if error.
:
: 3. When nesting For Next and/or If Then statements you need to indent each nested statements both for readability and to avoid compiler hangups. Consider your nested statements here:
:
: : TempNum = 1
: : For i = 1 To TotalQs
: : UseSetOperand = False
: : For Num = 1 To TotalOperands
: :
: : If UseSetOperand = False And SetOperand <> "" Then
: : ProbNums(i, Num) = SetOperand
: : End If
: :
: : If UseSetOperand = True Or SetOperand = "" Then
: : If TempNum <> TotalOperands Then
: : ProbNums(i, Num) = Random(0, XNum) & PType(UsePType(i))
: : Else
: : ProbNums(i, Num) = Random(0, XNum)
: : End If
: : End If
: : UseSetOperand = True
: : Next
: : Next
: I have found that when you nest statements as you have the compiler spits out errors, but if you indent as below then the errors usually go away.
: Try:
: : TempNum = 1
: : For i = 1 To TotalQs
: : UseSetOperand = False
: : For Num = 1 To TotalOperands
: :
: : If UseSetOperand = False And SetOperand <> "" Then
: : ProbNums(i, Num) = SetOperand
: : End If
: :
: : If UseSetOperand = True Or SetOperand = "" Then
: : If TempNum <> TotalOperands Then
: : ProbNums(i, Num) = Random(0, XNum) & PType(UsePType(i))
: : Else
: : ProbNums(i, Num) = Random(0, XNum)
: : End If
: : End If
: : UseSetOperand = True
: : Next Num
: : Next i
:
: Do the same thing with these nested for next loops
: : For i = 1 To TotalQs
: : For Num = 1 To TotalOperands
: :
: : Problems(i) = Problems(i) & ProbNums(i, Num)
: :
: : Next
: : Next
:
: Finally are you trying to generate a random number here:
:
: ProbNums(i, Num) = Random(0, XNum)
:
: I didn't see any declaration for an array called Random and I believe that Random() is the C++ method for random number generation. VB random number generation goes like this:
:
: Randomize (Timer)
: MyVariable = Int(Rnd * 6)'Generates random number from 0 to 6
:
: If zero is not wanted as a result then do it like this:
:
: Randomize (Timer)
: MyVariable = Int(Rnd * 5) + 1'Generates random number from 1 to 6
:
: Well that's all I could find, hope this helps.
:

· ·
• Posts: 617Member
: I am passing a value to it wen i am using the function but it still was giving me that error. is it because im passing it using textbox text?
:
:
: : : hey im sorta new to vb and i am trying to create this thing that creates a small test type thing. i finished but it is giving this error. Compile Error: Constant Expression Required
: : :
: : : Heres the part that has the error, sry its kinda long.
: : : [code]
: : : Public Function CreateTest(TotalQs As Integer, TotalOperands As Integer, _
: : : SetOperand, OperandLength As Integer, ProblemType As String)
: : :
: : :
: : : Dim XNum
: : : Dim ProbType(3) As String
: : : Dim TempNum As Long, PType() As String, ProbTypeFinal As Integer
: : : Dim UsePType(1 To TotalQs) As Integer
: : : Dim ProbNums(1 To TotalQs, 1 To TotalOperands) As String, UsedSetOperand As Boolean
: : : Dim Problems(1 To TotalQs) As String
: :
: :
: : This is just a guess but if you're getting a Constant expression required error means that the variable in question has Nothing assigned to it, that is to say that TotalQs is empty or set to Zero.
: :
: : Without assigning a value to your variable when you use it in declaring an array as you have:
: :
: : Dim UsePType(1 To TotalQs) As Integer
: :
: : You are essentially declaring an array with a size of 1 to 0. If VB is highlighting TotalQs as you say then bring your mouse over the highlited area and it should popup with its value, and I am willing to bet a buck that it is empty or set to zero.
: :
: : So what you need to do is pass a value to TotalQs when your function is called. I gather by the variable name and what you are trying to accomplish that TotalQs represents the number of questions in the test. You need to somehow count the number of questions and pass that value to TotalQs. That should fix that problem; however, in reading your code I have spotted some more problems that will inevitably popup when you get passed the first problem.
: :
: : 1. Your For Next loops like this one
: : : For i = 0 To 3
: : : ProbType(i) = ""
: : : Next
: : Next should have the variable pass to it like this
: : : For i = 0 To 3
: : : ProbType(i) = ""
: : : Next i
: :
: : 2. The If Then statements directly below the above loop are missing the End If statement. This will cause an if without end if error.
: :
: : 3. When nesting For Next and/or If Then statements you need to indent each nested statements both for readability and to avoid compiler hangups. Consider your nested statements here:
: :
: : : TempNum = 1
: : : For i = 1 To TotalQs
: : : UseSetOperand = False
: : : For Num = 1 To TotalOperands
: : :
: : : If UseSetOperand = False And SetOperand <> "" Then
: : : ProbNums(i, Num) = SetOperand
: : : End If
: : :
: : : If UseSetOperand = True Or SetOperand = "" Then
: : : If TempNum <> TotalOperands Then
: : : ProbNums(i, Num) = Random(0, XNum) & PType(UsePType(i))
: : : Else
: : : ProbNums(i, Num) = Random(0, XNum)
: : : End If
: : : End If
: : : UseSetOperand = True
: : : Next
: : : Next
: : I have found that when you nest statements as you have the compiler spits out errors, but if you indent as below then the errors usually go away.
: : Try:
: : : TempNum = 1
: : : For i = 1 To TotalQs
: : : UseSetOperand = False
: : : For Num = 1 To TotalOperands
: : :
: : : If UseSetOperand = False And SetOperand <> "" Then
: : : ProbNums(i, Num) = SetOperand
: : : End If
: : :
: : : If UseSetOperand = True Or SetOperand = "" Then
: : : If TempNum <> TotalOperands Then
: : : ProbNums(i, Num) = Random(0, XNum) & PType(UsePType(i))
: : : Else
: : : ProbNums(i, Num) = Random(0, XNum)
: : : End If
: : : End If
: : : UseSetOperand = True
: : : Next Num
: : : Next i
: :
: : Do the same thing with these nested for next loops
: : : For i = 1 To TotalQs
: : : For Num = 1 To TotalOperands
: : :
: : : Problems(i) = Problems(i) & ProbNums(i, Num)
: : :
: : : Next
: : : Next
: :
: : Finally are you trying to generate a random number here:
: :
: : ProbNums(i, Num) = Random(0, XNum)
: :
: : I didn't see any declaration for an array called Random and I believe that Random() is the C++ method for random number generation. VB random number generation goes like this:
: :
: : Randomize (Timer)
: : MyVariable = Int(Rnd * 6)'Generates random number from 0 to 6
: :
: : If zero is not wanted as a result then do it like this:
: :
: : Randomize (Timer)
: : MyVariable = Int(Rnd * 5) + 1'Generates random number from 1 to 6
: :
: : Well that's all I could find, hope this helps.
: :
:
:
[/code]

you could try casting the value from the text box, as your function is expecting a integer - and you are passing it a string.

try passing it:
Val(mytextbox.text)

this will pass the numeric value in the text box - make sure you have error handling to prevent someone from entering a leter or symbol.
[blue]
C:Dos
C:Dos Run
Run Dos Run
[/blue]

· ·
• Posts: 2,444Member
: hey im sorta new to vb and i am trying to create this thing that creates a small test type thing. i finished but it is giving this error. Compile Error: Constant Expression Required
:
: Heres the part that has the error, sry its kinda long.
: [code]
: Public Function CreateTest(TotalQs As Integer, TotalOperands As Integer, _
: SetOperand, OperandLength As Integer, ProblemType As String)
:
:
: Dim XNum
: Dim ProbType(3) As String
: Dim TempNum As Long, PType() As String, ProbTypeFinal As Integer
: Dim UsePType(1 To TotalQs) As Integer
: Dim ProbNums(1 To TotalQs, 1 To TotalOperands) As String, UsedSetOperand As Boolean
: Dim Problems(1 To TotalQs) As String
:
: ' creates the highest number an operand can be
:
: XNum = 1
: For i = 1 To OperandLength
: XNum = XNum & "0"
: Next
: XNum = XNum - 1
:
: ' finds which problem types are used and what problems will use which type
:
:
: For i = 0 To 3
: ProbType(i) = ""
: Next
: If InStr("+", ProblemType) Then ProbType(0) = "+"
: If InStr("-", ProblemType) Then ProbType(1) = "-"
: If InStr("*", ProblemType) Then ProbType(2) = "*"
: If InStr("/", ProblemType) Then ProbType(3) = "/"
:
: TempNum = 0
: For i = 0 To 3
: If ProbType(i) <> "" Then
: PType(TempNum) = ProbType(i)
: TempNum = TempNum + 1
: End If
: Next
: ProbTypeFinal = TempNum - 1
:
:
: For i = 1 To TotalQs
: UsePType(i) = Random(0, ProbTypeFinal)
: Next
:
:
:
:
: ' this creates the numbers for each problem
:
:
: TempNum = 1
: For i = 1 To TotalQs
: UseSetOperand = False
: For Num = 1 To TotalOperands
:
: If UseSetOperand = False And SetOperand <> "" Then
: ProbNums(i, Num) = SetOperand
: End If
:
: If UseSetOperand = True Or SetOperand = "" Then
: If TempNum <> TotalOperands Then
: ProbNums(i, Num) = Random(0, XNum) & PType(UsePType(i))
: Else
: ProbNums(i, Num) = Random(0, XNum)
: End If
: End If
: UseSetOperand = True
: Next
: Next
:
: ' this puts the numbers together
:
:
: For i = 1 To TotalQs
: For Num = 1 To TotalOperands
:
: Problems(i) = Problems(i) & ProbNums(i, Num)
:
: Next
: Next
:
: Text1.Text = Problems(1)
:
: End Function
: [/code]
:
: it is highlighting the TotalQs variable right in the begining.
:
:

You are using a variable to dimension arrays staticly... you can't do that!

[code]
Dim MyArray() As String
ReDim MyArray(TotalQs)
[/code]
And then one thing: This will declare TotalQs + 1 items in the array since the array will not be dimensioned from 0 To TotalQs!
Fix:
[code]
ReDim MyArray(TotalQs - 1) 'for 0 to TotalQs - 1
'Alternative: ReDim MyArray(0 To TotalQs - 1) 'This is the same
'Now to declare from 1 to TotalQs
ReDim MyArray(1 To TotalQs)
[/code]

[size=5][red]WARNING:[/red][/size]
And then someone said something about the Random function... He was almost right but made a little mistake:
RandomNumber = Int(Rnd * 6) '0 to 5
RandomNumber = Int(Rnd * 6) + 1 '1 to 6
RandomNumber = Int(Rnd * 5) '0 to 4

Instead of the first returning 0 to 6...
Why? Good you ask: Rnd returns a number from 0 to 1, includiging 0, exluding 1. So something close to 5.99999999999 would be the closest you could get to six. Int() will return 5 then... Just a side note which is quite important!
Greets...
Richard

· ·
• Posts: 99Member
hey thanks, well i made a random function but i just didnt include it i just showed the error. thanks for the help

: : hey im sorta new to vb and i am trying to create this thing that creates a small test type thing. i finished but it is giving this error. Compile Error: Constant Expression Required
: :
: : Heres the part that has the error, sry its kinda long.
: : [code]
: : Public Function CreateTest(TotalQs As Integer, TotalOperands As Integer, _
: : SetOperand, OperandLength As Integer, ProblemType As String)
: :
: :
: : Dim XNum
: : Dim ProbType(3) As String
: : Dim TempNum As Long, PType() As String, ProbTypeFinal As Integer
: : Dim UsePType(1 To TotalQs) As Integer
: : Dim ProbNums(1 To TotalQs, 1 To TotalOperands) As String, UsedSetOperand As Boolean
: : Dim Problems(1 To TotalQs) As String
: :
: : ' creates the highest number an operand can be
: :
: : XNum = 1
: : For i = 1 To OperandLength
: : XNum = XNum & "0"
: : Next
: : XNum = XNum - 1
: :
: : ' finds which problem types are used and what problems will use which type
: :
: :
: : For i = 0 To 3
: : ProbType(i) = ""
: : Next
: : If InStr("+", ProblemType) Then ProbType(0) = "+"
: : If InStr("-", ProblemType) Then ProbType(1) = "-"
: : If InStr("*", ProblemType) Then ProbType(2) = "*"
: : If InStr("/", ProblemType) Then ProbType(3) = "/"
: :
: : TempNum = 0
: : For i = 0 To 3
: : If ProbType(i) <> "" Then
: : PType(TempNum) = ProbType(i)
: : TempNum = TempNum + 1
: : End If
: : Next
: : ProbTypeFinal = TempNum - 1
: :
: :
: : For i = 1 To TotalQs
: : UsePType(i) = Random(0, ProbTypeFinal)
: : Next
: :
: :
: :
: :
: : ' this creates the numbers for each problem
: :
: :
: : TempNum = 1
: : For i = 1 To TotalQs
: : UseSetOperand = False
: : For Num = 1 To TotalOperands
: :
: : If UseSetOperand = False And SetOperand <> "" Then
: : ProbNums(i, Num) = SetOperand
: : End If
: :
: : If UseSetOperand = True Or SetOperand = "" Then
: : If TempNum <> TotalOperands Then
: : ProbNums(i, Num) = Random(0, XNum) & PType(UsePType(i))
: : Else
: : ProbNums(i, Num) = Random(0, XNum)
: : End If
: : End If
: : UseSetOperand = True
: : Next
: : Next
: :
: : ' this puts the numbers together
: :
: :
: : For i = 1 To TotalQs
: : For Num = 1 To TotalOperands
: :
: : Problems(i) = Problems(i) & ProbNums(i, Num)
: :
: : Next
: : Next
: :
: : Text1.Text = Problems(1)
: :
: : End Function
: : [/code]
: :
: : it is highlighting the TotalQs variable right in the begining.
: :
: :
:
: You are using a variable to dimension arrays staticly... you can't do that!
:
: Do it like this instead:
: [code]
: Dim MyArray() As String
: ReDim MyArray(TotalQs)
: [/code]
: And then one thing: This will declare TotalQs + 1 items in the array since the array will not be dimensioned from 0 To TotalQs!
: Fix:
: [code]
: ReDim MyArray(TotalQs - 1) 'for 0 to TotalQs - 1
: 'Alternative: ReDim MyArray(0 To TotalQs - 1) 'This is the same
: 'Now to declare from 1 to TotalQs
: ReDim MyArray(1 To TotalQs)
: [/code]
:
: [size=5][red]WARNING:[/red][/size]
: And then someone said something about the Random function... He was almost right but made a little mistake:
: RandomNumber = Int(Rnd * 6) '0 to 5
: RandomNumber = Int(Rnd * 6) + 1 '1 to 6
: RandomNumber = Int(Rnd * 5) '0 to 4
:
: Instead of the first returning 0 to 6...
: Why? Good you ask: Rnd returns a number from 0 to 1, includiging 0, exluding 1. So something close to 5.99999999999 would be the closest you could get to six. Int() will return 5 then... Just a side note which is quite important!
: Greets...
: Richard
:
:

· ·