'How to calculate ohms in C# ASP.NET MVC using SQL table data
Currently I am trying to make a calculator in c# that lets me calculate ohm values of resisitors based on the color bands.
Assuming, the user inputs the color bands bandA, bandB, bandC, and bandD
- input is converted into an array.
- array is converted to string
- string converted to result
Algorithm
bandAColor + bandBColor + bandCColor(number of zeros) = q = q~=k(ohms)
yellow purple black orange = 470 k(ohms)
(str(4) + str(7) + str(0)) * 1 = 470 k(ohms)
My question in short how do i access the table data correctly and less messy? Do I need to add a second model with a different table called ohmscalculator and gather the data into that?
Here is my model:
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ohms.Models
{
public class ohmsClass
{
// Class contains an Id field, which is required
// by the database for the primary key.
public int Id { get; set; }
// Ring Color
public string Name { get; set; }
public string Code { get; set; }
public int RAL { get; set; }
// Significant Figures
public string SigFig { get; set; }
// Multiplier
public long Multiplier { get; set; }
public double Multiplier2 { get; set; }
// Tolerance
public string Percent { get; set; }
public string Letter { get; set; }
// Temperature coefficient
public long Ppmk { get; set; }
public string TCLetter { get; set; }
/*
The electronic color code(http://en.wikipedia.org/wiki/Electronic_color_code) is
used to indicate the values or ratings of electronic components, very commonly
for resistors. Write a class that implements the following interface. Feel free
to include any supporting types as necessary.
*/
public interface IOhmValueCalculator
{
/// <summary>
/// Calculates the Ohm value of a resistor based on the band colors.
/// </summary>
/// <param name="bandAColor">The color of the first figure of component value band.</param>
/// <param name="bandBColor">The color of the second significant figure band.</param>
/// <param name="bandCColor">The color of the decimal multiplier band.</param>
/// <param name="bandDColor">The color of the tolerance value band.</param>
//C# code here
//algorithm
// bandAColor + bandBColor + bandCColor(number of zeros) = q = q~=k(ohms)
// yellow purple black orange = 470 k(ohms)
//Name Code RAL Percent [%] Letter [ppm/K] Letter
//Pink PK 3015 – ×10−3[8] ×0.001 – –
int CalculateOhmValue(string bandAColor, string bandBColor, string bandCColor, string bandDColor);
}
}
}
Here is my view:
@model IEnumerable<ohms.Models.ohmsClass>
@{
ViewData["Title"] = "Index";
}
<h1>The standard color code per IEC 60062:2016 is as follows:</h1>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Code)
</th>
<th>
@Html.DisplayNameFor(model => model.RAL)
</th>
<th>
@Html.DisplayNameFor(model => model.SigFig)
</th>
<th>
@Html.DisplayNameFor(model => model.Multiplier)
</th>
<th>
@Html.DisplayNameFor(model => model.Multiplier2)
</th>
<th>
@Html.DisplayNameFor(model => model.Percent)
</th>
<th>
@Html.DisplayNameFor(model => model.Letter)
</th>
<th>
@Html.DisplayNameFor(model => model.Ppmk)
</th>
<th>
@Html.DisplayNameFor(model => model.TCLetter)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Code)
</td>
<td>
@Html.DisplayFor(modelItem => item.RAL)
</td>
<td>
@Html.DisplayFor(modelItem => item.SigFig)
</td>
<td>
@Html.DisplayFor(modelItem => item.Multiplier)
</td>
<td>
@Html.DisplayFor(modelItem => item.Multiplier2)
</td>
<td>
@Html.DisplayFor(modelItem => item.Percent)
</td>
<td>
@Html.DisplayFor(modelItem => item.Letter)
</td>
<td>
@Html.DisplayFor(modelItem => item.Ppmk)
</td>
<td>
@Html.DisplayFor(modelItem => item.TCLetter)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>
@using (Html.BeginForm("Index", "ohmsClasses", FormMethod.Post, new { enctype = "multipart/form-data" })){
<div>
<h2>@Html.Label("Enter Band A") : @Html.TextBox("bandAColor")</h2>
<h2>@Html.Label("Enter Band B") : @Html.TextBox("bandBColor")</h2>
<h2>@Html.Label("Enter Band C") : @Html.TextBox("bandCColor")</h2>
<h2>@Html.Label("Enter Band D") : @Html.TextBox("bandDColor")</h2>
<input type="submit" name="Calculate Ohms" value="Index"/>
</div>
<div>
<h2>
@Html.RadioButton("calkey", "0") Calculate populated bands
<br>
</h2>
<a class="btn btn-primary" href="@Url.Action("Index","ohmsClasses")">Calculate Ohms</a>
</div>
<div>
@Html.TextBox("result")
</div>
}
Here is my controller and its action method:
public async Task<IActionResult> YourAction(ohmsClass ohms )
{
//C# code here
//algorithm
// bandAColor + bandBColor + bandCColor(number of zeros) = q = q~=k(ohms)
// yellow purple black orange = 470 k(ohms)
//Name Code RAL Percent [%] Letter [ppm/K] Letter
//Pink PK 3015 – ×10−3[8] ×0.001 – –
ModelState.Clear();
string bandAColor, bandBColor, bandCColor, bandDColor;
List<ohmsClass> listOfColors = new List<ohmsClass>() {
new ohmsClass
{
Name = "None",
Code = "0",
RAL = 0,
SigFig = "-",
Multiplier = 10^-3,
Multiplier2 = 0.001,
Percent = "20",
Letter = "M",
Ppmk = 0,
TCLetter = "0"
},
new ohmsClass
{
Name = "Pink",
Code = "PK",
RAL = 3015,
SigFig = "-",
Multiplier = 10 ^ -2,
Multiplier2 = 0.01,
Percent = "0",
Letter = "M",
Ppmk = 0,
TCLetter = "0"
},
new ohmsClass
{
Name = "Silver",
Code = "SR",
RAL = 0,
SigFig = "-",
Multiplier = 10 ^ -1,
Multiplier2 = 0.1,
Percent = "10",
Letter = "K",
Ppmk = 0,
TCLetter = "0"
},
new ohmsClass
{
Name = "Gold",
Code = "GD",
RAL = 0,
SigFig = "-",
Multiplier = 10 ^ -1,
Multiplier2 = 0.1,
Percent = "5",
Letter = "J",
Ppmk = 0,
TCLetter = "0"
},
new ohmsClass
{
Name = "Black",
Code = "BK",
RAL = 9005,
SigFig ="0",
Multiplier = 10 ^ -0,
Multiplier2 = 1,
Percent = "20",
Letter = "M",
Ppmk = 250,
TCLetter = "U"
},
new ohmsClass
{
Name = "Brown",
Code = "BN",
RAL = 8003,
SigFig = "1",
Multiplier = 10 ^ 1,
Multiplier2 = 10,
Percent = "1",
Letter = "F",
Ppmk = 100,
TCLetter = "S"
},
new ohmsClass
{
Name = "Red",
Code = "RD",
RAL = 3000,
SigFig = "2",
Multiplier = 10 ^ 2,
Multiplier2 = 100,
Percent = "2",
Letter = "G",
Ppmk = 50,
TCLetter = "R"
},
new ohmsClass
{
Name = "Orange",
Code = "OG",
RAL = 2003,
SigFig = "3",
Multiplier = 10 ^ 3,
Multiplier2 = 1000,
Percent = "0.05",
Letter = "W",
Ppmk = 15,
TCLetter = "P"
},
new ohmsClass
{
Name = "Yellow",
Code = "YE",
RAL = 1021,
SigFig = "4",
Multiplier = 10 ^ 4,
Multiplier2 = 10000,
Percent = "0.02^[8][nb 1][9]",
Letter = "P",
Ppmk = 25,
TCLetter = "Q"
},
new ohmsClass
{
Name = "Green",
Code = "GN",
RAL = 6018,
SigFig = "5",
Multiplier = 10 ^ 5,
Multiplier2 = 100000,
Percent = "0.5",
Letter = "D",
Ppmk = 20,
TCLetter = "Z^[nb 2]"
},
new ohmsClass
{
Name = "Blue",
Code = "BU",
RAL = 5015,
SigFig = "6",
Multiplier = 10 ^ 6,
Multiplier2 = 1000000,
Percent = "0.25",
Letter = "C",
Ppmk = 10,
TCLetter = "Z^[nb 2]"
},
new ohmsClass
{
Name = "Violet",
Code = "VT",
RAL = 4005,
SigFig = "7",
Multiplier = 10 ^ 7,
Multiplier2 = 10000000,
Percent = "0.01",
Letter = "B",
Ppmk = 10,
TCLetter = "M"
},
new ohmsClass
{
Name = "Grey",
Code = "GY",
RAL = 7000,
SigFig = "8",
Multiplier = 10 ^ 8,
Multiplier2 = 100000000,
Percent = "0.01^[8][nb 3][nb 1][9]",
Letter = "L(A)",
Ppmk = 1,
TCLetter = "K"
},
new ohmsClass
{
Name = "White",
Code = "WH",
RAL = 1013,
SigFig = "9",
Multiplier = 10 ^ 9,
Multiplier2 = 1000000000,
Percent = "-",
Letter = "-",
Ppmk = 0,
TCLetter = "-"
}};
//listOfColors[0].Name;
for (int i = 0; i < listOfColors.Count; i++)
{
int selectedFunction = Convert.ToInt32(Request.Form["calkey"]);
//Random ran = new Random();
//Console.WriteLine("Random Ohm " + listOfColors[ran.Next(5)].Name + listOfColors[ran.Next(5)].Name + listOfColors[ran.Next(5)].Name + listOfColors[ran.Next(5)].Name);
var None = listOfColors.SingleOrDefault(c => c.Name == "None");
var NoneValue = listOfColors.SingleOrDefault(c => c.SigFig == "-");
var Pink = listOfColors.SingleOrDefault(c => c.Name == "Pink");
var PinkValue = listOfColors.SingleOrDefault(c => c.SigFig == "-");
var Silver = listOfColors.SingleOrDefault(c => c.Name == "Silver");
var SilverValue = listOfColors.SingleOrDefault(c => c.SigFig == "-");
var Gold = listOfColors.SingleOrDefault(c => c.Name == "Gold");
var GoldValue = listOfColors.SingleOrDefault(c => c.SigFig == "-");
var Black = listOfColors.SingleOrDefault(c => c.Name == "Black");
var Brown = listOfColors.SingleOrDefault(c => c.Name == "Brown");
var Red = listOfColors.SingleOrDefault(c => c.Name == "Red");
var Orange = listOfColors.SingleOrDefault(c => c.Name == "Orange");
var Yellow = listOfColors.SingleOrDefault(c => c.Name == "Yellow");
var Green = listOfColors.SingleOrDefault(c => c.Name == "Green");
var Blue = listOfColors.SingleOrDefault(c => c.Name == "Blue");
var Violet = listOfColors.SingleOrDefault(c => c.Name == "Violet");
var Grey = listOfColors.SingleOrDefault(c => c.Name == "Grey");
var White = listOfColors.SingleOrDefault(c => c.Name == "White");
}
return View();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

