VB.Net
Posted: March 2nd, 2008, 14:19
Anyone know anything to do with VB.Net? I have a mate who's looking for some help with a program he's trying to make.
Might work, but then he won't be able to learn another one will he?buzzmong wrote:Break his fingers until he promises never to use VB again?
Code: Select all
sub
new button (attb1, attb2, ..., attbn)
end sub
Code: Select all
arrayname [index of object in the array].
Code: Select all
txtTeamName() 'textbox containing team name
txtTeamScore() 'textbox containing team score
btnSubmit() 'button to apply changes]
btnScr2Load 'button to start second screen. Static control
Code: Select all
lblName() 'label containing Team Name
lblScore() 'label containing Team Score
Code: Select all
Dim sp1, sp2 As Integer
If teamNum < 3 Then
sp1 = Math.Round(Me.Width / 2)
Else
sp1 = Math.Round(Me.Width / 4)
End If
sp2 = Math.Round(Me.Height / 4)
Select Case teamNum
Case Is < 3
ReDim lblName(1)
lblName(1) = New lblch()
With lblName(1)
.Name = "lblName1"
.Text = "Team 1"
.Location = New System.Drawing.Point(sp1 - Math.Round(lblName(1).Width / 2), sp2)
.Font = New System.Drawing.Font("Bradley Hand ITC", 20.0!, FontStyle.Bold)
.AutoSize = True
End With
Me.Controls.AddRange(New System.Windows.Forms.Control() {lblName(1)})
ReDim lblName(2)
lblName(2) = New lblch()
With lblName(2)
.Name = "lblName2"
.Text = "Team 2"
.Location = New System.Drawing.Point(sp1 - Math.Round(lblName(2).Width / 2), sp2 * 3)
.Font = New System.Drawing.Font("Bradley Hand ITC", 20.0!, FontStyle.Bold)
.AutoSize = True
End With
Me.Controls.AddRange(New System.Windows.Forms.Control() {lblName(2)})
Case Is = 3
ReDim lblName(1)
lblName(1) = New lblch()
With lblName(1)
.Name = "lblName1"
.Text = "Team 1"
.Location = New System.Drawing.Point(sp1 - Math.Round(lblName(1).Width / 2), sp2)
.Font = New System.Drawing.Font("Bradley Hand ITC", 20.0!, FontStyle.Bold)
.AutoSize = True
End With
Me.Controls.AddRange(New System.Windows.Forms.Control() {lblName(1)})
ReDim lblName(2)
lblName(2) = New lblch()
With lblName(2)
.Name = "lblName2"
.Text = "Team 2"
.Location = New System.Drawing.Point((sp1 * 3) - Math.Round(lblName(2).Width / 2), sp2)
.Font = New System.Drawing.Font("Bradley Hand ITC", 20.0!, FontStyle.Bold)
.AutoSize = True
End With
Me.Controls.AddRange(New System.Windows.Forms.Control() {lblName(2)})
ReDim lblName(3)
lblName(3) = New lblch()
With lblName(3)
.Name = "lblName3"
.Text = "Team 3"
.Location = New System.Drawing.Point((sp1 * 2) - Math.Round(lblName(3).Width / 2), sp2 * 3)
.Font = New System.Drawing.Font("Bradley Hand ITC", 20.0!, FontStyle.Bold)
.AutoSize = True
End With
Me.Controls.AddRange(New System.Windows.Forms.Control() {lblName(3)})
Code: Select all
Private Sub btn2scr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2scr.Click
If Scr2.Visible = False Then
Scr2.Show()
For i = 1 To teamNum
Scr2.lblName(i).Text = txtTeamName(i).Text
Next
Else
Scr2.Hide()
End If
End Sub
and this occurs in this lineAn unhandled exception of type 'System.NullReferenceException' occurred in Teams.exe
Code: Select all
Scr2.lblName(i).Text = txtTeamName(i).Text
Code: Select all
Protected Sub Page_Load(blah)
Dim o as new Button
AddHandler o.OnClick, AddressOf ButtonClick
Me.Controls.Add(o)
End Sub
Protected Sub ButtonClick(sender as object, e as eventargs)
CType(sender,Button).text="You Clicked Me"
End Sub
Code: Select all
Private Sub MyPrivateSub
Dim lblMylabel as new label
Me.Controls.Add(lblMyLabel)
End Sub
Public Sub Main
MyPrivateSub
lblMyLabel.Text = "123" <would error (wouldn't even compile)
End Sub
Code: Select all
Public Sub Main
MyPrivateSub()
CType(Me.Controls(1),Label).Text = "123"
End Sub
Code: Select all
Public Sub Main
MyPrivateSub
CType(Me.FindControl(lblMyLabel),Label).Text = "123"
End Sub
Wrong.buzzmong wrote:NullReferenceException is saying that it can't assign a value to Scr2.lblName(i).Text because there is nothing in txtTeamName(i).Text.
Code: Select all
Set(ByVal value As String)
If (value Is Nothing) Then
value = ""
End If
If (value <> Me.Text) Then
If Me.CacheTextInternal Then
Me.text = value
End If
Me.WindowText = value
Me.OnTextChanged(EventArgs.Empty)
If Me.IsMnemonicsListenerAxSourced Then
Dim control As Control = Me
Do While (Not control Is Nothing)
Dim impl As ActiveXImpl = DirectCast(control.Properties.GetObject(Control.PropActiveXImpl), ActiveXImpl)
If (Not impl Is Nothing) Then
impl.UpdateAccelTable
Return
End If
control = control.ParentInternal
Loop
End If
End If
End Set
Did I mention I hadn't done VB in a while?Fear wrote:Wrong.buzzmong wrote:NullReferenceException is saying that it can't assign a value to Scr2.lblName(i).Text because there is nothing in txtTeamName(i).Text.
You can set a string to nothing, that is perfectly valid.
It means Scr2 is nothing, or Scr2.lblName(i) is nothing.
It just so happens that the VB.Net base control will convert a nothing value to an empty string anyway.
Code: Select all
Set(ByVal value As String) If (value Is Nothing) Then value = "" End If If (value <> Me.Text) Then If Me.CacheTextInternal Then Me.text = value End If Me.WindowText = value Me.OnTextChanged(EventArgs.Empty) If Me.IsMnemonicsListenerAxSourced Then Dim control As Control = Me Do While (Not control Is Nothing) Dim impl As ActiveXImpl = DirectCast(control.Properties.GetObject(Control.PropActiveXImpl), ActiveXImpl) If (Not impl Is Nothing) Then impl.UpdateAccelTable Return End If control = control.ParentInternal Loop End If End If End Set
Code: Select all
Dim myControl As New Label
myControl.ID="lblName2"
Me.Controls.Add(MyControl)
Code: Select all
Ctype(Me.FindControl("lblName2"),Label).Text="Whatever Text"
Ctype(Me.FindControl("lblName2"),Label).Font = New System.Drawing.Font("Bradley Hand ITC", 20.0!, FontStyle.Bold)
Code: Select all
Public ReadOnly Property lblName2 as Label
Get
Dim o as control = Me.FindControl("lblName2")
If o Is Nothing then Return Nothing
Return Ctype(o,Label)
End Get
End Property
Code: Select all
Private _lblName2 as Label = Nothing
Public ReadOnly Property lblName2 as Label
Get
If _lblName2 Is Nothing Then
_lblName2 = Me.FindControl("lblName2")
End If
Return _lblName2
End Get
End Property
Yeah, but I brought nothing more awesomely.HereComesPete wrote:Hey! I brought nothing here first dammit!