Post: VB Help for small task
02-18-2015, 03:30 PM #1
(adsbygoogle = window.adsbygoogle || []).push({}); I am new to visual basic(2013) and I was given the task as practice to do this little bit and I am stumped as to how to make this code simple and fully functional. Any help is greatly appreciated! Thanks.

Task:
Place into Output which Input position the largest number is in. The answer will be one of the words: First, Second, Third, Fourth or Fifth. For example, if the input boxes contained the numbers 5, 19, 32, 2 and 4 then the Output should have “Third” in it since the largest number, the 32, was in the Input3. No compound if statements (so no And or Or) allowed, no Select Case, and no more than 5 if statements total.*
02-18-2015, 08:16 PM #2
Boliberrys
^^ Sexy ^^
So you have. 6 textbox(one for each number). And the other one for the text, and a Button to check number location? I dont understand very well. Would u mind taking a Screenshot of the design? Maybe i can help Winky Winky
02-18-2015, 10:02 PM #3
Pichu
RIP PICHU.
Use Regex, put the numbers into an array and convert to double. (incase the user inputs decimal).

From there, you will loop through each number and test, if the next number is larger, replace it as the largest value until no other numbers are larger than the largest identified value.

Then you can output the indexed position:

0 =First
1 = Second
2 = Third
3 = Fourth

The following user thanked Pichu for this useful post:

Conveyy
02-18-2015, 10:43 PM #4
jagex
Gym leader
Here you go, change it to however is required.

    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VBTask
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

List<double> numbers = new List<double>();

private void txtNumber_KeyPress(object sender, KeyPressEventArgs e)
{
var key = e.KeyChar;

if(!char.IsDigit(key))
{
e.Handled = true;
}
}

private void btn_Sumbit_Click(object sender, EventArgs e)
{
double value = 0;
int indexCount;
double highestValue;
int indexOfHighest;

indexCount = numbers.Count;

if(indexCount == 4)
{
highestValue = numbers.Max();
indexOfHighest = numbers.IndexOf(highestValue) + 1;
MessageBox.Show("Highest Value: " + highestValue.ToString() + "Index Of: " + indexOfHighest.ToString());
numbers.Clear();
value = 0;

}
else
{
if (!string.IsNullOrEmpty(txtNumber.Text))
{
value = Convert.ToDouble(txtNumber.Text);
numbers.Add(value);
txtNumber.Clear();
}
}
}
}
}


Lol I just realized this won't let you even add decimals, so the whole converting to double is pointless unless you want to fix it in the keypress method
Last edited by jagex ; 02-19-2015 at 01:51 PM.
02-28-2015, 01:19 AM #5
MrRa1n
League Champion
Originally posted by jagex View Post
Here you go, change it to however is required.

    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VBTask
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

List<double> numbers = new List<double>();

private void txtNumber_KeyPress(object sender, KeyPressEventArgs e)
{
var key = e.KeyChar;

if(!char.IsDigit(key))
{
e.Handled = true;
}
}

private void btn_Sumbit_Click(object sender, EventArgs e)
{
double value = 0;
int indexCount;
double highestValue;
int indexOfHighest;

indexCount = numbers.Count;

if(indexCount == 4)
{
highestValue = numbers.Max();
indexOfHighest = numbers.IndexOf(highestValue) + 1;
MessageBox.Show("Highest Value: " + highestValue.ToString() + "Index Of: " + indexOfHighest.ToString());
numbers.Clear();
value = 0;

}
else
{
if (!string.IsNullOrEmpty(txtNumber.Text))
{
value = Convert.ToDouble(txtNumber.Text);
numbers.Add(value);
txtNumber.Clear();
}
}
}
}
}


Lol I just realized this won't let you even add decimals, so the whole converting to double is pointless unless you want to fix it in the keypress method


The title is for VB help lol :p
02-28-2015, 04:57 AM #6
jagex
Gym leader
Originally posted by Rain View Post
The title is for VB help lol :p


Online converter:

    
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows.Forms

Namespace VBTask
Public Partial Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub

Private numbers As New List(Of Double)()

Private Sub txtNumber_KeyPress(sender As Object, e As KeyPressEventArgs)
Dim key = e.KeyChar

If Not Char.IsDigit(key) Then
e.Handled = True
End If
End Sub

Private Sub btn_Sumbit_Click(sender As Object, e As EventArgs)
Dim value As Double = 0
Dim indexCount As Integer
Dim highestValue As Double
Dim indexOfHighest As Integer

indexCount = numbers.Count

If indexCount = 4 Then
highestValue = numbers.Max()
indexOfHighest = numbers.IndexOf(highestValue) + 1
MessageBox.Show("Highest Value: " + highestValue.ToString() + "Index Of: " + indexOfHighest.ToString())
numbers.Clear()

value = 0
Else
If Not String.IsNullOrEmpty(txtNumber.Text) Then
value = Convert.ToDouble(txtNumber.Text)
numbers.Add(value)
txtNumber.Clear()
End If
End If
End Sub
End Class
End Namespace

'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Twitter: @telerik
'Facebook: facebook.com/telerik
'=======================================================
Last edited by jagex ; 02-28-2015 at 04:59 AM.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo