-
October 6th, 2016, 10:18 AM
#16
Re: Resistor color code
How to convert C++ to C# ?
-
October 9th, 2016, 04:43 AM
#17
Re: Resistor color code
How to make support for 5 band and 6 band resistor and how to make support for mΩ,uΩ,nΩ,pΩ ?
What should I add?
-
October 9th, 2016, 05:14 AM
#18
Re: Resistor color code
Originally Posted by Pavlex4
How to make support for 5 band and 6 band resistor and how to make support for mΩ,uΩ,nΩ,pΩ ?
What should I add?
This is going to be up to you. I very much doubt that any forum member will provide this - and I'm not going to. As I indicated in previous posts, the code I posted was a basic starter to demonstrate some concepts as to one way in which the problem could be approached - and by no means the only way. You are free to use this code as required. Your original question was to help you to make an app that converts resistance to colour - not write the whole program! I provided the code as c++ as I don't program using c# - but if you are a programmer then it shouldn't require much work to to be able to understand how the c++ program works and to apply its methods to c#.
For what type of device is this app intended for?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 9th, 2016, 05:17 AM
#19
Re: Resistor color code
For Windows.I translated code and I it works fine.
-
October 9th, 2016, 05:21 AM
#20
Re: Resistor color code
What this part of code means:
Color.FromName(colours[res[0] - '0']);
Color.FromName(colours[res[1] - '0']);
Color.FromName(colours[third != 0 ? third : res.Count() - 2]);
-
October 9th, 2016, 05:35 AM
#21
Re: Resistor color code
colours is an array with an index that starts at 0. res is a string representation of the value. res[0] is the first char in the string (the left char) So if res has the value 4700 then res[0] is the char 4 and res[1] is the char 7. But colours requires an index starting at 0, so subtracting '0' from the char value gives a number starting at 0 (id res[0] is '0' then subtracting '0' gives 0) that can be used to index into colours array to obtain the colour name.
Code:
third != 0 ? third : res.Count() - 2]
This is effectively the same as
Code:
if (third != 0)
colours[third];
else
colours[res.Count() - 2];
So if the value of third has been set, then use this value as a direct element index into colours. If it has not been set then find the number of digits to the right of the first two. This will give the value of the third band as the multiplier. .Count() provides the number of chars in the string res so subtract 2 to ignore the first 2 and then use this value as the index into colours to get the required colour for the multiplier band.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 9th, 2016, 05:48 AM
#22
Re: Resistor color code
Do I just need to add something below this:
Color.FromName(colours[res[0] - '0']);
Color.FromName(colours[res[1] - '0']);
Color.FromName(colours[third != 0 ? third : res.Count() - 2]);
-
October 9th, 2016, 06:06 AM
#23
Re: Resistor color code
For c# ????? As I've previously said, I don't program using c#. Perhaps a c# guru could help?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 9th, 2016, 06:07 AM
#24
-
October 9th, 2016, 06:23 AM
#25
Re: Resistor color code
Can it be done like this:
label15.BackColor = Color.FromName(colours[res[0] - '0']);
label16.BackColor = Color.FromName(colours[res[1] - '0']);
label17.BackColor = Color.FromName(colours[res[2] - '0']);
label18.BackColor = Color.FromName(colours[third != 0 ? third : res.Count() - 3]);
-
October 9th, 2016, 07:08 AM
#26
Re: Resistor color code
For 5 band? No. That would also require changes in other parts of the program.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 9th, 2016, 07:21 AM
#27
Re: Resistor color code
I tested 5 and 6 band and it's working good!!!
-
October 9th, 2016, 07:27 AM
#28
Re: Resistor color code
mΩ is working good but I have problem with uΩ,nΩ,pΩ !!!
https://postimg.org/image/y6q4bw64d/
-
October 9th, 2016, 09:43 AM
#29
Re: Resistor color code
As the issue is out of range with res[1] in line 136, the string res only has one char so trying to access the second char is causing the error. Therefore val has a value that is less than 10. For that line to work, val has to have a value of 10 or greater. In my original c++ code, there was code to ensure this.
PS Same goes for the code in post #25 where you are accessing res[2]. For that to work, val must have a value of at least 100 - other the same issue with out of range will occur.
Last edited by 2kaud; October 9th, 2016 at 09:52 AM.
Reason: PS
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 9th, 2016, 11:39 AM
#30
Re: Resistor color code
I have that code:
Code:
if (isUnitCorrect && isValueCorrect)
{
double mul = factorDictionary[lastChar];
double val = double.Parse(value) * mul;
int third = 0;
if (val < 1)
{
val *= 100;
third = 9;
}
else if (val < 10)
{
val *= 10;
third = 10;
}
res = val.ToString();
if (res.Count() > 24)
MessageBox.Show("Invalid value");
else
{
label15.BackColor = Color.FromName(colours[res[0] - '0']);
label16.BackColor = Color.FromName(colours[res[1] - '0']);
label17.BackColor = Color.FromName(colours[third != 0 ? third : res.Count() - 2]);
}
Last edited by 2kaud; October 9th, 2016 at 12:15 PM.
Reason: Code tags added
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
|