[RESOLVED] Basic variable problem
Hi all. :wave:
There is a byte variable as follow
Code:
Dim Sendcmd() As Byte = { 0, 0, 0, 0 }
which need to be called in
Code:
SerialPort1.Write(Sendcmd, 0, Sendcmd.Lenght - 1)
The question is how to change "0"s with a set of TrackBar controls changeable values?
What I tried and failed:
Code:
Dim Sendcmd() As Byte = {CByte(TrackBar1.Value), CByte(TrackBar2.Value), CByte(TrackBar3.Value), CByte(TrackBar4.Value)}
Code:
Dim Sendcmd() As Byte = {(TrackBar1.Value).ToString, (TrackBar2.Value).ToString, (TrackBar3.Value).ToString, (TrackBar4.Value).ToString}
Re: Basic variable problem
The last code snippet is obviously wrong because why would you create a String array and assign it to a variable of type Byte array? The prior snippet looks OK though, so maybe you could explain what actually happened rather than just saying that it failed. You get different error messages under different circumstances for a reason. My guess would be that one or more of the values are not within the 0-255 range required for a Byte, but why would you not configure the TrackBar to prevent that in the first place?
Re: Basic variable problem
Long live the jmcilhinney.
Yes the latter one is totally invalid.
All trackbars are minimum=0 and maximum=255 set in properties already.
VS is not even able to build and/or run the debug and here's the error context:
Quote:
No Source Available.
The call stack contains only external code.
This thread is stopped with only external code frames on the call stack. External code frames are typically from framework code but can also include other optimized modules which are loaded in the target process.
InvalidOperationException was unhandled
An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object.
[some troubleshooting options]
Break when this exception type is thrown.
"OK", "Continue"
None of above options and buttons will lead to run the program even if it fails
Re: Basic variable problem
It is complaining about an object reference. You do not show much code so no way to know if it is thrown by what you showed or by something you did not show but the only object I see there is serialport1. So I would look there
Re: Basic variable problem
You say that VS can't build the project but that appears to be incorrect. If you just build then that will succeed. If you run the project then that will fail because, after the successful build, the application runs and then throws an exception. Where exactly is the code that you showed us? If it is outside any method, i.e. if Sendcmd is a member variable, then that would explain the exception. In that case, it's trying to execute that code before any of the controls are created, so of course TrackBar1 and the rest are Nothing. That code should be in a method, possibly an event handler, so that you get the three TrackBar values at the correct time during the execution of the application.
For the record, I never would have guess that that was the case without that diagnostic information, which is why you ALWAYS need to provide it. Also, if I'm right about the issue then you should stop using Dim for member variables and start using Private. In that case, it would be obvious even without any other context what is a local and what is a member.
Re: Basic variable problem
Alright people, Due to lack of information and my english skill, Here's the whole code:
Code:
Imports System
Imports System.Threading
Imports System.Management
Public Class Form1
Dim a As Byte = 0
'Dim Sendcmd() As Byte = {&H0, &H0, &H1C, 0, CByte(TrackBar1.Value), 0, CByte(TrackBar2.Value), 0, CByte(TrackBar3.Value), 0, CByte(TrackBar4.Value), 0, CByte(TrackBar5.Value), 0, CByte(TrackBar6.Value), 0, CByte(TrackBar7.Value), 0, CByte(TrackBar8.Value), 0, CByte(TrackBar9.Value), 0, CByte(TrackBar10.Value), 0, CByte(TrackBar11.Value), 0, CByte(TrackBar12.Value), 0, CByte(TrackBar13.Value), 0, CByte(TrackBar14.Value), 0, CByte(TrackBar15.Value), 0, CByte(TrackBar16.Value), 0, CByte(TrackBar17.Value), 0, CByte(TrackBar18.Value), 0, CByte(TrackBar19.Value), 0, CByte(TrackBar20.Value), 0, CByte(TrackBar21.Value), 0, CByte(TrackBar22.Value), 0, CByte(TrackBar23.Value), 0, CByte(TrackBar24.Value), 0, CByte(TrackBar25.Value), 0, CByte(TrackBar26.Value), 0}
Dim Sendcmd() As Byte = {&H0, &H0, &H1C, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0, 128, 0}
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Try
SerialPort1.Close()
Catch ex As Exception
End Try
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Try
TrackBar1.Value = TrackBar1.Value + 1
TrackBar2.Value = TrackBar2.Value + 1
TrackBar3.Value = TrackBar3.Value + 1
TrackBar4.Value = TrackBar4.Value + 1
TrackBar5.Value = TrackBar5.Value + 1
TrackBar6.Value = TrackBar6.Value + 1
TrackBar7.Value = TrackBar7.Value + 1
TrackBar8.Value = TrackBar8.Value + 1
TrackBar9.Value = TrackBar9.Value + 1
TrackBar10.Value = TrackBar10.Value + 1
TrackBar11.Value = TrackBar11.Value + 1
TrackBar12.Value = TrackBar12.Value + 1
TrackBar13.Value = TrackBar13.Value + 1
TrackBar14.Value = TrackBar14.Value + 1
TrackBar15.Value = TrackBar15.Value + 1
TrackBar16.Value = TrackBar16.Value + 1
TrackBar17.Value = TrackBar17.Value + 1
TrackBar18.Value = TrackBar18.Value + 1
TrackBar19.Value = TrackBar19.Value + 1
TrackBar20.Value = TrackBar20.Value + 1
TrackBar21.Value = TrackBar21.Value + 1
TrackBar22.Value = TrackBar22.Value + 1
TrackBar23.Value = TrackBar23.Value + 1
TrackBar24.Value = TrackBar24.Value + 1
TrackBar25.Value = TrackBar25.Value + 1
TrackBar26.Value = TrackBar26.Value + 1
Catch ex As Exception
End Try
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
SerialPort1.Open()
Timer2.Enabled = True
CheckBox1.Text = "&Disconnect"
End If
If CheckBox1.Checked = False Then
SerialPort1.Close()
Timer2.Enabled = False
CheckBox1.Text = "&Connect"
End If
End Sub
Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
Label1.Text = TrackBar1.Value
End Sub
Private Sub TrackBar2_Scroll(sender As Object, e As EventArgs) Handles TrackBar2.Scroll
Label2.Text = TrackBar2.Value
End Sub
Private Sub TrackBar3_Scroll(sender As Object, e As EventArgs) Handles TrackBar3.Scroll
Label3.Text = TrackBar3.Value
End Sub
Private Sub TrackBar4_Scroll(sender As Object, e As EventArgs) Handles TrackBar4.Scroll
Label4.Text = TrackBar4.Value
End Sub
Private Sub TrackBar5_Scroll(sender As Object, e As EventArgs) Handles TrackBar5.Scroll
Label5.Text = TrackBar5.Value
End Sub
Private Sub TrackBar6_Scroll(sender As Object, e As EventArgs) Handles TrackBar6.Scroll
Label6.Text = TrackBar6.Value
End Sub
Private Sub TrackBar7_Scroll(sender As Object, e As EventArgs) Handles TrackBar7.Scroll
Label7.Text = TrackBar7.Value
End Sub
Private Sub TrackBar8_Scroll(sender As Object, e As EventArgs) Handles TrackBar8.Scroll
Label8.Text = TrackBar8.Value
End Sub
Private Sub TrackBar9_Scroll(sender As Object, e As EventArgs) Handles TrackBar9.Scroll
Label9.Text = TrackBar9.Value
End Sub
Private Sub TrackBar10_Scroll(sender As Object, e As EventArgs) Handles TrackBar10.Scroll
Label10.Text = TrackBar10.Value
End Sub
Private Sub TrackBar11_Scroll(sender As Object, e As EventArgs) Handles TrackBar11.Scroll
Label11.Text = TrackBar11.Value
End Sub
Private Sub TrackBar12_Scroll(sender As Object, e As EventArgs) Handles TrackBar12.Scroll
Label12.Text = TrackBar12.Value
End Sub
Private Sub TrackBar13_Scroll(sender As Object, e As EventArgs) Handles TrackBar13.Scroll
Label13.Text = TrackBar13.Value
End Sub
Private Sub TrackBar14_Scroll(sender As Object, e As EventArgs) Handles TrackBar14.Scroll
Label14.Text = TrackBar14.Value
End Sub
Private Sub TrackBar15_Scroll(sender As Object, e As EventArgs) Handles TrackBar15.Scroll
Label15.Text = TrackBar15.Value
End Sub
Private Sub TrackBar16_Scroll(sender As Object, e As EventArgs) Handles TrackBar16.Scroll
Label16.Text = TrackBar16.Value
End Sub
Private Sub TrackBar17_Scroll(sender As Object, e As EventArgs) Handles TrackBar17.Scroll
Label17.Text = TrackBar17.Value
End Sub
Private Sub TrackBar18_Scroll(sender As Object, e As EventArgs) Handles TrackBar18.Scroll
Label18.Text = TrackBar18.Value
End Sub
Private Sub TrackBar19_Scroll(sender As Object, e As EventArgs) Handles TrackBar19.Scroll
Label19.Text = TrackBar19.Value
End Sub
Private Sub TrackBar20_Scroll(sender As Object, e As EventArgs) Handles TrackBar20.Scroll
Label20.Text = TrackBar20.Value
End Sub
Private Sub TrackBar21_Scroll(sender As Object, e As EventArgs) Handles TrackBar21.Scroll
Label21.Text = TrackBar21.Value
End Sub
Private Sub TrackBar22_Scroll(sender As Object, e As EventArgs) Handles TrackBar22.Scroll
Label22.Text = TrackBar22.Value
End Sub
Private Sub TrackBar23_Scroll(sender As Object, e As EventArgs) Handles TrackBar23.Scroll
Label23.Text = TrackBar23.Value
End Sub
Private Sub TrackBar24_Scroll(sender As Object, e As EventArgs) Handles TrackBar24.Scroll
Label24.Text = TrackBar24.Value
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label24.Text = TrackBar24.Value
Label23.Text = TrackBar23.Value
Label22.Text = TrackBar22.Value
Label21.Text = TrackBar21.Value
Label20.Text = TrackBar20.Value
Label19.Text = TrackBar19.Value
Label18.Text = TrackBar18.Value
Label17.Text = TrackBar17.Value
Label16.Text = TrackBar16.Value
Label15.Text = TrackBar15.Value
Label14.Text = TrackBar14.Value
Label13.Text = TrackBar13.Value
Label12.Text = TrackBar12.Value
Label11.Text = TrackBar11.Value
Label10.Text = TrackBar10.Value
Label9.Text = TrackBar9.Value
Label8.Text = TrackBar8.Value
Label7.Text = TrackBar7.Value
Label6.Text = TrackBar6.Value
Label5.Text = TrackBar5.Value
Label4.Text = TrackBar4.Value
Label3.Text = TrackBar3.Value
Label2.Text = TrackBar2.Value
Label1.Text = TrackBar1.Value
End Sub
Private Sub AutoIncrease_CheckedChanged(sender As Object, e As EventArgs) Handles AutoIncrease.CheckedChanged
If AutoIncrease.Checked = True Then
Timer1.Enabled = True
End If
If AutoIncrease.Checked = False Then
Timer1.Enabled = False
End If
End Sub
Private Sub TrackBar1_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar1.ValueChanged
Label1.Text = TrackBar1.Value
End Sub
Private Sub TrackBar2_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar2.ValueChanged
Label2.Text = TrackBar2.Value
End Sub
Private Sub TrackBar3_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar3.ValueChanged
Label3.Text = TrackBar3.Value
End Sub
Private Sub TrackBar4_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar4.ValueChanged
Label4.Text = TrackBar4.Value
End Sub
Private Sub TrackBar5_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar5.ValueChanged
Label5.Text = TrackBar5.Value
End Sub
Private Sub TrackBar6_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar6.ValueChanged
Label6.Text = TrackBar6.Value
End Sub
Private Sub TrackBar7_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar7.ValueChanged
Label7.Text = TrackBar7.Value
End Sub
Private Sub TrackBar8_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar8.ValueChanged
Label8.Text = TrackBar8.Value
End Sub
Private Sub TrackBar9_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar9.ValueChanged
Label9.Text = TrackBar9.Value
End Sub
Private Sub TrackBar10_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar10.ValueChanged
Label10.Text = TrackBar10.Value
End Sub
Private Sub TrackBar11_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar11.ValueChanged
Label11.Text = TrackBar11.Value
End Sub
Private Sub TrackBar12_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar12.ValueChanged
Label12.Text = TrackBar12.Value
End Sub
Private Sub TrackBar13_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar13.ValueChanged
Label13.Text = TrackBar13.Value
End Sub
Private Sub TrackBar14_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar14.ValueChanged
Label14.Text = TrackBar14.Value
End Sub
Private Sub TrackBar15_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar15.ValueChanged
Label15.Text = TrackBar15.Value
End Sub
Private Sub TrackBar16_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar16.ValueChanged
Label16.Text = TrackBar16.Value
End Sub
Private Sub TrackBar17_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar17.ValueChanged
Label17.Text = TrackBar17.Value
End Sub
Private Sub TrackBar18_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar18.ValueChanged
Label18.Text = TrackBar18.Value
End Sub
Private Sub TrackBar19_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar19.ValueChanged
Label19.Text = TrackBar19.Value
End Sub
Private Sub TrackBar20_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar20.ValueChanged
Label20.Text = TrackBar20.Value
End Sub
Private Sub TrackBar21_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar21.ValueChanged
Label21.Text = TrackBar21.Value
End Sub
Private Sub TrackBar22_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar22.ValueChanged
Label22.Text = TrackBar22.Value
End Sub
Private Sub TrackBar23_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar23.ValueChanged
Label23.Text = TrackBar23.Value
End Sub
Private Sub TrackBar24_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar24.ValueChanged
Label24.Text = TrackBar24.Value
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
BuffText.Clear()
SerialPort1.Write(Sendcmd, 0, Sendcmd.Length - 1)
Thread.Sleep(100)
For i As Integer = 0 To Sendcmd.Length - 1
BuffText.Text = BuffText.Text & " ," & Sendcmd(i)
Next
End Sub
End Class
Visual components are TrackBar x26, Lable x24 and TextBox x1
I'm not sure about "Private" or "Dim" but I'm sure the uncommenting variable declaration (Line no. 6) and commenting next one (Number based one) will not start simulation/run sequence and mentioned exception will pop-up.
/Update/
View details will provide following info:
Quote:
System.InvalidOperationException was unhandled
HResult=-2146233079
Message=An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object.
Source=TEST
StackTrace:
at TEST.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190
at TEST.My.MyProject.MyForms.get_Form1()
at TEST.My.MyApplication.OnCreateMainForm() in C:\Users\DFI\Documents\VB TEST\TEST\TEST\My Project\Application.Designer.vb:line 35
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at TEST.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.NullReferenceException
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=TEST
StackTrace:
at TEST.Form1..ctor() in C:\Users\DFI\Documents\VB TEST\TEST\TEST\Form1.vb:line 10
InnerException:
CByte(Val(Label1.Text)) was also checked, same failure. In case of TrackBar maximum value limits are out of range. Is it possible I have to convert value or its limit to something smaller? Just asking...
CBool, CByte, CChar, CDate, CDbl, CDec, CInt, CLng, CObj, CSByte, CShort, CSng and CStr also were tested.
Re: Basic variable problem
So the issue is exactly what I said. The line of code you said is the problem is not inside a method, i.e. the variable declared is a member variable (a field) rather than a local variable. Member variables are initialised before the class constructor executes and it is the form constructor that calls InitializeComponent, which is where controls are created. That means that you are trying to get the Value of your TarckBar controls before they exist. Even apart from that issue, what could be the point of that? If you want to write the data to the SerialPort when a Timer Ticks, wouldn't that be the time to get the Values from the TrackBars? At the very least, surely it needs to be after the user has set them.
AS I also said, don't use Dim to declare fields. ALWAYS be explicit about the access level of everything. Just as you have declared your methods Private, so you should be declaring any fields Private. If you had done that then the problem would have been obvious from the start.
Re: Basic variable problem
Yes, It works.
Create a field first, after Public class:
Code:
Private Sendcmd(56) As Byte
Equaling it with values and/or a component's value is not allowed here for some reason.
Then call your components and settings (if you have any) initialization:
Code:
Public Sub New()
InitializeComponent()
InitializeSettings()
End Sub
In final, add and mix up the field, where ever you want in your project:
Code:
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
BuffText.Clear()
Sendcmd = {&H0, &H0, &H1C, CByte(TrackBar1.Value), CByte(TrackBar1.Value), CByte(TrackBar2.Value), CByte(TrackBar2.Value), CByte(TrackBar3.Value), CByte(TrackBar3.Value), CByte(TrackBar4.Value), CByte(TrackBar4.Value), CByte(TrackBar5.Value), CByte(TrackBar5.Value), CByte(TrackBar6.Value), CByte(TrackBar6.Value), CByte(TrackBar7.Value), CByte(TrackBar7.Value), CByte(TrackBar8.Value), CByte(TrackBar8.Value), CByte(TrackBar9.Value), CByte(TrackBar9.Value), CByte(TrackBar10.Value), CByte(TrackBar10.Value), CByte(TrackBar11.Value), CByte(TrackBar11.Value), CByte(TrackBar12.Value), CByte(TrackBar12.Value), CByte(TrackBar13.Value), CByte(TrackBar13.Value), CByte(TrackBar14.Value), CByte(TrackBar14.Value), CByte(TrackBar15.Value), CByte(TrackBar15.Value), CByte(TrackBar16.Value), CByte(TrackBar16.Value), CByte(TrackBar17.Value), CByte(TrackBar17.Value), CByte(TrackBar18.Value), CByte(TrackBar18.Value), CByte(TrackBar19.Value), CByte(TrackBar19.Value), CByte(TrackBar20.Value), CByte(TrackBar20.Value), CByte(TrackBar21.Value), CByte(TrackBar21.Value), CByte(TrackBar22.Value), CByte(TrackBar22.Value), CByte(TrackBar23.Value), CByte(TrackBar23.Value), CByte(TrackBar24.Value), CByte(TrackBar24.Value), CByte(TrackBar25.Value), CByte(TrackBar25.Value), CByte(TrackBar26.Value), CByte(TrackBar26.Value), 255, 255, 255}
SerialPort1.Write(Sendcmd, 0, Sendcmd.Length - 1)
Thread.Sleep(100)
For i As Integer = 0 To Sendcmd.Length - 1
BuffText.Text = BuffText.Text & " ," & Sendcmd(i)
Next
End Sub
Re: [RESOLVED] Basic variable problem
Quote:
Equaling it with values and/or a component's value is not allowed here for some reason.
IT's not "for some reason" ... it's for a very specific reason... the control does _not_ exist yet at the time that code runs. THAT's the reason. You were trying to get the value of the Trackbars... at a time when the Trackbars don't yet exist, so you can't do that. The second line DOES work because you removed the dependency of the Trackbars. So you CAN initialize variables on the same line that you declare them on, and you should... but what you CAN'T do is use the values of controls at that point because they simply don't exist.
-tg