-
Jul 17th, 2021, 05:31 AM
#1
Thread Starter
Hyperactive Member
Changing a field length and its values in VB.NET
Hi all.
Consider a field of variables like below:
Code:
Private Sendcmd() As Byte
Public Sub New()
InitializeComponent()
End Sub
There is a NumericUpDown control (called SendLenght) which determines Sendcmd length i.e. from 1 to 64.
By clicking a button, filling Sendcmd with NumericUpDown values from 1 to current value of SendLenght. (There are 64 NumericUpDown maximum)
How can I do such thing in VB.NET?
I tried for loop as shown below but I suspected an array or collection making is required.
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim NumericUpDown As NumericUpDown() = {NumericUpDown1, NumericUpDown2, NumericUpDown3, NumericUpDown4, NumericUpDown5, NumericUpDown6, NumericUpDown7, NumericUpDown8, NumericUpDown9, NumericUpDown10, NumericUpDown11, NumericUpDown12, NumericUpDown13, NumericUpDown14, NumericUpDown15, NumericUpDown16, NumericUpDown17, NumericUpDown18, NumericUpDown19, NumericUpDown20, NumericUpDown21, NumericUpDown22, NumericUpDown23, NumericUpDown24, NumericUpDown25, NumericUpDown26, NumericUpDown27, NumericUpDown28, NumericUpDown29, NumericUpDown30, NumericUpDown31, NumericUpDown32, NumericUpDown33, NumericUpDown34, NumericUpDown35, NumericUpDown36, NumericUpDown37, NumericUpDown38, NumericUpDown39, NumericUpDown40, NumericUpDown41, NumericUpDown42, NumericUpDown43, NumericUpDown44, NumericUpDown45, NumericUpDown46, NumericUpDown47, NumericUpDown48, NumericUpDown49, NumericUpDown50, NumericUpDown51, NumericUpDown52, NumericUpDown53, NumericUpDown54, NumericUpDown55, NumericUpDown56, NumericUpDown57, NumericUpDown58, NumericUpDown59, NumericUpDown60, NumericUpDown61, NumericUpDown62, NumericUpDown63, NumericUpDown64}
For i As Decimal = 1 To SendLenght.Value
Sendcmd(i - 1)=Sendcmd(i - 1) {NumericUpDown i - 1} '<-- End of statement expected error to this line
Next
End Sub
Thanks in advanced.
______________________________________________
/UPDATE/
Expected results:
For SendLenght.Value = 1
Code:
Sendcmd(1) = {NumericUpDown1.Value}
For SendLenght.Value = 2
Code:
Sendcmd(2) = {NumericUpDown1.Value, NumericUpDown2.Value}
For SendLenght.Value = 10
Code:
Sendcmd(10) = {NumericUpDown1.Value, NumericUpDown2.Value, NumericUpDown3.Value, NumericUpDown4.Value, NumericUpDown5.Value, NumericUpDown6.Value, NumericUpDown7.Value, NumericUpDown8.Value, NumericUpDown9.Value, NumericUpDown10.Value}
Last edited by pourkascheff; Jul 17th, 2021 at 06:01 AM.
-
Jul 17th, 2021, 05:58 AM
#2
Re: Changing a field length and its values in VB.NET
LINQ is your friend.
VB.NET Code:
Dim count = CInt(SendLenght.Value)
Sendcmd = Controls.OfType(Of NumericUpDown)().
Where(Function(nud) nud.Name.StartsWith("NumericUpDown")).
Take(count).
Select(Function(nud) CByte(nud.Value)).
ToArray()
The first line gets the number of NUDs to include based on the first NUD. The second line first gets all the NUDs on the form in z-order, then excludes all those whose name doesn't start with "NumericUpDown", then excludes those not within the count determined earlier, then converts the Value of those remaining into Bytes and then puts those Bytes into an array.
That assumes that the z-order of the NumericUpDown controls matches the order of the numeric suffix on the name. If you didn't change the default names then that will be the case. Also, be aware that that is creating a new array object each time.
-
Jul 17th, 2021, 06:02 AM
#3
Re: Changing a field length and its values in VB.NET
Note that, rather than using that Where call, you could put all the NUDs you do want to include and no others onto a Panel, then use the Controls collection of that Panel. As there would be no other NUD controls in the collection, no filter would be required. If there were no other controls in the Panel at all then, while OfType would still work, Cast would be more appropriate:
VB.NET Code:
Sendcmd = Panel1.Controls.
Cast(Of NumericUpDown)().
Take(count).
Select(Function(nud) CByte(nud.Value)).
ToArray()
-
Jul 17th, 2021, 06:18 AM
#4
Thread Starter
Hyperactive Member
Re: Changing a field length and its values in VB.NET
wow. too much information to digest. please wait for one more try or two to give a proper feedback. Even docs.microsoft LINQ page looks like new with weird titles. Another linq page is also interesting but I'm very new to this concept.
-
Jul 17th, 2021, 10:31 AM
#5
Re: Changing a field length and its values in VB.NET
LINQ is basically a way to perform SQL-like queries in .NET code. It doesn't achieve anything that you can't do without LINQ but it can make a lot of code more succinct - often much more succinct. Primarily, LINQ can compress loops to a single statement. The code in post #2 could be written old-school style like this:
VB.NET Code:
Dim upperBound = CInt(SendLenght.Value) - 1
ReDim Sendcmd(upperBound)
For i = 0 To upperBound
Dim nud = DirectCast(Controls("NumericUpDown" & (i + 1)), NumericUpDown)
Sendcmd(i) = CByte(nud.Value)
Next
Tags for this Thread
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
|