变量''在被赋值之前使用 [英] Variable '' is used before it has been assigned a value

查看:722
本文介绍了变量''在被赋值之前使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是项目的一些编码....我一直在解决这个问题......变量''< variablename>''在被赋值之前使用...变量''onestudent''用红色加下划线...



Here is a bit of coding for a project.... I keep getting this problem......Variable ''<variablename>'' 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)
    FileClose(1)
    NumberOfrecords = NumberOfrecords + 1
    lblnumberofrecords.Text = NumberOfrecords
    txtAdStId.Text = ""
    txtforname.Text = ""
    txtsearch.Text = ""
    txtStId.Text = ""
    txtsurname.Text = ""
    RichTextBox1.Text = ""
    txtforname.Focus()
End Sub</variablename>

推荐答案

编译器为您提供了一个非常明确且非常有价值的方向,但你不想欣赏它。您声明变量 onestudent 并且永远不会按任何值初始化它。由于变量的类型是一个结构,你需要用构造函数初始化它。编译器只是试图让你免于一次惊人的失败。您真的需要学习基础知识:变量,成员,引用,类型和实例如何工作。由于你不了解你可能得到的最清晰的编译器信息,你没有机会继续开发。是时候阅读手册......



-SA
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…

—SA


创建更好然后使用结构。



看,它是如何简单:

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
        Get
            Return sForename
        End Get
        Set(ByVal value As String)
            sForename = value
        End Set
    End Property

    'other properties in the same way

End Class



用法:


Usage:

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





如何:在VB.NET中创建类 [ ^ ]

请阅读 VB中的范围 [ ^ 也是。


这篇关于变量''在被赋值之前使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆