CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 4 FirstFirst 1234 LastLast
Results 16 to 30 of 46
  1. #16
    Join Date
    Oct 2016
    Posts
    41

    Re: Resistor color code

    How to convert C++ to C# ?

  2. #17
    Join Date
    Oct 2016
    Posts
    41

    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?

  3. #18
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Resistor color code

    Quote Originally Posted by Pavlex4 View Post
    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#.

    make an app
    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)

  4. #19
    Join Date
    Oct 2016
    Posts
    41

    Re: Resistor color code

    For Windows.I translated code and I it works fine.

  5. #20
    Join Date
    Oct 2016
    Posts
    41

    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]);

  6. #21
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    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)

  7. #22
    Join Date
    Oct 2016
    Posts
    41

    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]);

  8. #23
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    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)

  9. #24
    Join Date
    Oct 2016
    Posts
    41

    Re: Resistor color code

    For C++?

  10. #25
    Join Date
    Oct 2016
    Posts
    41

    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]);

  11. #26
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    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)

  12. #27
    Join Date
    Oct 2016
    Posts
    41

    Re: Resistor color code

    I tested 5 and 6 band and it's working good!!!

  13. #28
    Join Date
    Oct 2016
    Posts
    41

    Re: Resistor color code

    mΩ is working good but I have problem with uΩ,nΩ,pΩ !!!
    https://postimg.org/image/y6q4bw64d/

  14. #29
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    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)

  15. #30
    Join Date
    Oct 2016
    Posts
    41

    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

Page 2 of 4 FirstFirst 1234 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured