Im new to C# and having trouble with this code: I have a combo box on a form optypeCB and when a particular type is picked(Draw,Form,Pierce,Trim) I would like a numerical value assigned to a text box designhrsTB. My code is as follows but it does not work can anyone help me. Thanks in advance

        optypCB.Text ;
        switch(int);
        {
            case "Draw":
                designhrsTB = 80;
            case  "Form":
                designhrsTB = 120;
            case "Pierce":
            designhrsTB =120;
            case "Trim":
            designhrsTB =160;
        }

Dani AI

Generated

The code posted has a few concrete, fixable issues that cause compile/runtime errors and incorrect behavior. The immediate problems are: using the type name int inside switch(...) instead of an expression, a stray semicolon after switch(...) which ends the statement, missing flow-control after each case (C# requires break/return/throw), and assigning a numeric literal to the control variable instead of to its Text (or to a numeric control's Value). As and noted, those syntax errors are the first things to correct. As suggested, switching on the combo text is fine, but you also need to handle case sensitivity and binding scenarios.

A clearer, more robust approach is to map option names to hours once and then look up the value when the selection changes. This avoids falling into fall-through mistakes and makes case-insensitive matching easy:

private readonly Dictionary<string,int> hoursByOp = new Dictionary<string,int>(StringComparer.OrdinalIgnoreCase)
{
    {"Draw", 80}, {"Form", 120}, {"Pierce", 120}, {"Trim", 160}
};

private void optypeCB_SelectedIndexChanged(object sender, EventArgs e)
{
    var key = (optypeCB.SelectedItem ?? optypeCB.Text).ToString().Trim();
    if (hoursByOp.TryGetValue(key, out int hrs))
        designhrsTB.Text = hrs.ToString();
    else
        designhrsTB.Text = string.Empty; // fallback if unknown
}

Troubleshooting checklist: verify the control Name values in the Designer (optypeCB, designhrsTB), wire the SelectedIndexChanged event, and make sure designhrsTB is a TextBox (use .Text) or a numeric control (.Value) as appropriate. If the ComboBox is data-bound, use SelectedValue or bind a small DTO (Display/Value) instead of relying on Text. Consider using an enum or constants for the operation types to make the mapping safer and easier to maintain. As implied, investing a little time in the language reference (switch, ComboBox events, control properties) will pay off quickly.

Recommended Answers

All 9 Replies

switch(int); What is int? Remove the ; after it to begin with.
Where is default? Where is break?

I thought i had to call it an integer? Not sure where break or default should be.

The switch statement is similar for all languages. I'm trying not to be rude but saying "Im new to C#" and put a ; after switch() is more like "I'm new to programming altogether".

If this is the case, I'd suggest reading on line about switch and other language syntax.

any suggestions for a good source to read. I have read a few but still very fuzzy

You can switch directly on the string, but make sure you won't have a case-sensitive issue. More can be read at: http://blogs.msdn.com/brada/archive/2003/08/14/50227.aspx

switch(optypCB.Text)
{
case "Draw":
designhrsTB = 80; break;
case "Form":
designhrsTB = 120; break;
case "Pierce":
designhrsTB =120; break;
case "Trim":
designhrsTB =160; break;
default:
throw new Exception("Unknown selection");
}

Yeah, sknake, you are very good. But the point was to let him figure that out by himself...

I have gotten the sense of a lot of users on this forum sharing the same mindset? Why is that :(

We posted at the same time before i saw you provided articles, but I figured I would help him out since he was getting blasted about his misuse of a semi colon.

I have gotten the sense of a lot of users on this forum sharing the same mindset? Why is that :(

I agree, it seems like somehow the mindset of figuring out how to write code on your own has been transformed in this thread into the mindset of knowing all the features of a language and figuring out how it works, all while somehow being able to distinguish between good sources of information and bad sources of information without having the knowledge needed to do so.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.