Click here to Skip to main content
12,399,640 members (53,033 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB VB.NET
Here is a bit of coding for a project.... I keep getting this problem......Variable '' is used before it has been assigned a value...the variable 'onestudent' is underlined red...

Dim Filename As String
Dim NumberOfrecords As Integer
Dim MyFormat As String = "{0, -5}{1, -21}{2, -6}{3, -10}{4, -5}"
Structure StudentDetails
    Dim forename As String
    <vbfixedstring(18)> Public Description As String
    Dim surname As String
    Dim StId As String
    Dim AdvId As String
    Dim reason As String
    Dim dte As Date
End Structure

Private Sub frmmenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim onestudent As StudentDetails
    Filename = "Student.txt"
    FileOpen(1, Filename, OpenMode.Random, , , Len(onestudent))
    NumberOfrecords = LOF(1) / Len(onestudent)
    lblnumberofrecords.Text = NumberOfrecords
End Sub
Private Sub btnsubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsubmit.Click
    Dim onestudent As StudentDetails
    onestudent.StId = txtStId.Text
    onestudent.forename = txtforname.Text
    onestudent.surname = txtsurname.Text
    onestudent.AdvId = cboAdvId.Text
    onestudent.reason = RichTextBox1.Text
    onestudent.dte = DateTimePicker1.Text
    FileOpen(1, Filename, OpenMode.Random, , , Len(onestudent))
    FilePut(1, onestudent, NumberOfrecords)
    NumberOfrecords = NumberOfrecords + 1
    lblnumberofrecords.Text = NumberOfrecords
    txtAdStId.Text = ""
    txtforname.Text = ""
    txtsearch.Text = ""
    txtStId.Text = ""
    txtsurname.Text = ""
    RichTextBox1.Text = ""
End Sub</variablename>
Posted 14-Mar-13 9:19am
Updated 14-Mar-13 9:24am
ThePhantomUpvoter 14-Mar-13 15:25pm
Yes... the message is clear is it not? In the line with "Len(onestudent)" what is the value of onestudent when you try and get the length? onestudent = Nothing because you have not assigned a value to it.
Sergey Alexandrovich Kryukov 14-Mar-13 15:27pm
What part of error message do you fail to get? And this is not original message. Next time, please copy if and paste, you apparently mangled the message text...
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The compiler gives you a perfectly clear and a very valuable direction, but you don't want to appreciate it. You declare the variable onestudent and never initialize it by any value. As the variable's type is a structure, you need to initialize it with a constructor. The compiler just tried to save you from a spectacular failure. You really need to learn the very basics: how variables, members, references, types and instances work. As you don't understand the most clear compiler's message you could possibly get, you have no chance to continue development. Time to read the manuals…

Maciej Los 14-Mar-13 16:50pm
Short and to the point!
Sergey Alexandrovich Kryukov 14-Mar-13 16:52pm
Thank you, Maciej.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

It's much better to create class, then using structures.

See, how it's simple:
Public Class Student
    Private sForename As String
    Private sDescription As String
    Private sSurname As String
    Private iStId As Integer
    Private iAdvId As Integer
    Private sReason As String
    Private dte As Date

    Public Property Forename() As String
            Return sForename
        End Get
        Set(ByVal value As String)
            sForename = value
        End Set
    End Property
    'other properties in the same way

End Class
Dim stu as Student = New Student()
stu.Forename = "ForeName"

How to: Creating classes in VB.NET[^]
Please, read about Scope in VB[^] too.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 14 Mar 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100