|
-
Apr 20th, 2010, 08:10 AM
#81
Thread Starter
Lively Member
Re: good old arrays
I havent forgetten you, im stranded in Europe due to the volcano...
I have been reading about records and arrays during my vacation - yes I know lucky me (NOT) and believe for my final task I need to:
- define a type that consists of mixed data types so Name would be string and Mark would be Integer (infact it could be byte)
- after creating the type, call it in the program so that when the ADD button is clicked the name is stored in the name list box and the mark in the mark list box...
So far I have this but it doesnt work and Im not sure why, perhaps you can guys can help?
Code:
Option Explicit
'~~~ Creating the type
Private Type Student
strName As String
intMark As Integer
End Type
Private Sub Form_Load()
'~~~ Checking. This should be done first !
If Val(txtStudentMark.Text) <= 0 Then
MsgBox "student mark cannot be less than zero. Please re-enter it"
Exit Sub
ElseIf Val(txtStudentMark.Text) > 100 Then
MsgBox "student mark cannot be greater than 100. Please re-enter it"
Exit Sub
End If
Element = Element + 1
ReDim Preserve strName(1 To Element)
ReDim Preserve intMark(1 To Element)
strname(student) = txtStudentName.Text
intMark(student) = txtStudentMark.Text
lstNames.AddItem txtStudentName.Text
lstMarks.AddItem txtStudentMark.Text
End Sub
thanks in advance - will check back shortly, depending on internet connection availabilty...
thank you so much
-
Apr 20th, 2010, 08:17 AM
#82
Re: good old arrays
 Originally Posted by xirokx
- define a type that consists of mixed data types so Name would be string and Mark would be Integer (infact it could be byte)
- after creating the type, call it in the program so that when the ADD button is clicked the name is stored in the name list box and the mark in the mark list box...
You have already done those things. 
Check all the previous posts. Start reading from the beginning.
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Apr 20th, 2010, 08:27 AM
#83
Thread Starter
Lively Member
Re: good old arrays
yes i have but i did not assign the student type to the array so I could add names and marks to my list boxes..??
thats the part Im confused on as you can see from my above attempt..
remember it needs to be a dynamic array
-
Apr 20th, 2010, 08:30 AM
#84
Re: good old arrays
 Originally Posted by xirokx
yes i have but i did not assign the student type to the array so I could add names and marks to my list boxes..??
thats the part Im confused on as you can see from my above attempt..
remember it needs to be a dynamic array

Could you explain it ?
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Apr 20th, 2010, 08:33 AM
#85
Re: good old arrays
lstNames.AddItem Students(Element).StudentName
-
Apr 20th, 2010, 08:49 AM
#86
Re: good old arrays
 Originally Posted by MartinLiss
lstNames.AddItem Students(Element).StudentName
You did it again...
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Apr 20th, 2010, 09:04 AM
#87
Thread Starter
Lively Member
Re: good old arrays
shouldnt that be:
lstNames.AddItem Students(Element).strname
??
-
Apr 20th, 2010, 09:11 AM
#88
Re: good old arrays
You really ought to take the advice you were given and look all the way back in this thread, for example post #2.
-
Apr 20th, 2010, 09:16 AM
#89
Thread Starter
Lively Member
Re: good old arrays
here is my code, I receive an error message "variable not defined"
Option Explicit
Dim Element As Integer
'~~~ Creating the type
Private Type Student
strName As String
intMark As Integer
End Type
Private Sub cmdAddRecord_Click()
'~~~ Checking. This should be done first !
If Val(txtStudentMark.Text) <= 0 Then
MsgBox "student mark cannot be less than zero. Please re-enter it"
Exit Sub
ElseIf Val(txtStudentMark.Text) > 100 Then
MsgBox "student mark cannot be greater than 100. Please re-enter it"
Exit Sub
End If
Element = Element + 1
ReDim Preserve strName(1 To Element) <---------------error msg "variable not defined"
ReDim Preserve intMark(1 To Element)
strName(Student) = txtStudentName.Text
intMark(Student) = txtStudentMark.Text
lstNames.AddItem Student(Element).strName
lstMarks.AddItem Student(Element).intMark
End Sub
I thought it was though above? Have I declared the datatypes correctly?
-
Apr 21st, 2010, 04:58 AM
#90
Thread Starter
Lively Member
Re: good old arrays
i had a look at post #2 now I have an internet connection again but I dont see how that can help me because I am not using a loop...also post #2 does not say anything about private types which I have used in my code..
therefore I remain unsure of how to resolve my code so that it works i.e. resolving the error msg 'variable not defined' especially when I have used a private type and am using the 'student' type..
please put me on the right track or point me in the right direction
thanks all
-
Apr 21st, 2010, 05:56 AM
#91
Hyperactive Member
Re: good old arrays
It looks like you have declared the type Student but have not declared an instance of the type ie
Dim XXX as Student
Also when accessing you need to do
XXX.strName(Student) = txtStudentName.Text
XXX.intMark(Student) = txtStudentMark.Text
I may have also missed the point completely but you really should use [ code ][ /Code] tags and indent your code.
Slower than a crippled Vista
More buggy than a fresh XP install
Look! Down the road, some 50 miles behind the drunken snail.
It's Ubuntu!
-
Apr 21st, 2010, 08:39 AM
#92
Thread Starter
Lively Member
Re: good old arrays
Code:
Option Explicit
Dim Element As Integer
Dim Students As Student
'~~~ Creating the type
Private Type Student
strName As String
intMark As Integer
End Type
Private Sub cmdAddRecord_Click()
'~~~ Checking. This should be done first !
If Val(txtStudentMark.Text) <= 0 Then
MsgBox "student mark cannot be less than zero. Please re-enter it"
Exit Sub
ElseIf Val(txtStudentMark.Text) > 100 Then
MsgBox "student mark cannot be greater than 100. Please re-enter it"
Exit Sub
End If
Element = Element + 1
ReDim Preserve Students.strName(1 To Element)
ReDim Preserve Students.intMark(1 To Element)
Students.strName(Student) = txtStudentName.Text
Students.intMark(Student) = txtStudentMark.Text
lstNames.AddItem Student(Element).strName
lstMarks.AddItem Student(Element).intMark
End Sub
Private Sub cmdClearRecord_Click()
txtStudentName.Text = ""
txtStudentMark.Text = ""
txtStudentName.SetFocus
End Sub
this gives me the following error mesage:
"expected array"
it points me towards the "redim" line as if to say there is an error on that line...
what am i doing wrong..
please help and show me the way
thanks
-
Apr 21st, 2010, 01:01 PM
#93
Thread Starter
Lively Member
Re: good old arrays
somethings up with the array and the calling off I believe - but what?
I'm one baffled IROK...
-
Apr 21st, 2010, 03:49 PM
#94
Re: good old arrays
Your TYPE doesn't include arrays, but you are trying to use them as arrays
See changes in blue, the parentheses
Code:
Private Type Student
strName() As String
intMark() As Integer
End Type
After above change, you will probably get an error on these lines
Code:
Students.strName(Student) = txtStudentName.Text
Students.intMark(Student) = txtStudentMark.Text
lstNames.AddItem Student(Element).strName
lstMarks.AddItem Student(Element).intMark
That is because I believe Student should be Students, you need to include the array index, i.e.
Code:
Students.strName(Element) = txtStudentName.Text
Students.intMark(Element) = txtStudentMark.Text
lstNames.AddItem Students.strName(Element)
lstMarks.AddItem Students.intMark(Element)
Last edited by LaVolpe; Apr 21st, 2010 at 03:56 PM.
-
Apr 21st, 2010, 07:49 PM
#95
Re: good old arrays
Try this:
Code:
Option Explicit
Private Type Student
strName As String
intMark As Integer
End Type
Dim Students() As Student
Private Sub cmdAddRecord_Click()
Dim iMark As Long
Dim sName As String
Dim i As Long
sName = Trim(txtStudentName.Text)
If sName = "" Then
MsgBox "student name cannot be blank. Please re-enter it"
Exit Sub
End If
iMark = Val(txtStudentMark.Text)
If iMark < 0 Then
MsgBox "student mark cannot be less than zero. Please re-enter it"
Exit Sub
ElseIf iMark > 100 Then
MsgBox "student mark cannot be greater than 100. Please re-enter it"
Exit Sub
End If
If (Not Students) = -1 Then '-- Students() array was not initialized
i = 1
Else
i = UBound(Students) + 1
End If
ReDim Preserve Students(1 To i)
Students(i).strName = sName
Students(i).intMark = iMark
lstNames.AddItem sName
lstMarks.AddItem iMark
End Sub
-
Apr 22nd, 2010, 03:25 AM
#96
Re: good old arrays
 Originally Posted by anhn
Try this:
Code:
Option Explicit
Private Type Student
strName As String
intMark As Integer
End Type
Dim Students() As Student
Private Sub cmdAddRecord_Click()
Dim iMark As Long
Dim sName As String
Dim i As Long
sName = Trim(txtStudentName.Text)
If sName = "" Then
MsgBox "student name cannot be blank. Please re-enter it"
Exit Sub
End If
iMark = Val(txtStudentMark.Text)
If iMark < 0 Then
MsgBox "student mark cannot be less than zero. Please re-enter it"
Exit Sub
ElseIf iMark > 100 Then
MsgBox "student mark cannot be greater than 100. Please re-enter it"
Exit Sub
End If
If (Not Students) = -1 Then '-- Students() array was not initialized
i = 1
Else
i = UBound(Students) + 1
End If
ReDim Preserve Students(1 To i)
Students(i).strName = sName
Students(i).intMark = iMark
lstNames.AddItem sName
lstMarks.AddItem iMark
End Sub
I had already suggested and posted the code for using dynamic array in post #21
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Apr 22nd, 2010, 04:46 AM
#97
Re: good old arrays
 Originally Posted by akhileshbc
I had already suggested and posted the code for using dynamic array in post #21 
I have no time to read 94 posts.
The main thing is the declare of Students. That must be an array of type Student.
-
Apr 22nd, 2010, 04:56 AM
#98
Re: good old arrays
 Originally Posted by anhn
I have no time to read 94 posts.
The main thing is the declare of Students. That must be an array of type Student.
I wasn't complaining you You have done a great job... 
I was trying to provide a statement for this:
 Originally Posted by akhileshbc
You have already done those things. 
Check all the previous posts. Start reading from the beginning.
...
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
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
|