1

I have a method that is supposed to accept a structure variable and return a bool. This is working fine - at least in terms of syntax, perhaps.

private bool equalsidcomparison(Employee newGuy)
    { 
        foreach (Employee E in employees)
        { if (E.Name == newGuy.Name || E.phone == newGuy.phone) { return true; } return false; }
        return false;
    }

Later on in a button click method, where i am passing data to a struct variable's fields(newGuy), and then passing newGuy to the method above, I am told that newGuy is an unassigned local variable.

Employee newGuy;
        newGuy.id = nextIDnumber;
        newGuy.Name = txtbName.Text;
        newGuy.department = (string)comboDept.SelectedItem;
        newGuy.title = comboJob.SelectedText;
        newGuy.phone = txtbPhone.Text;


        foreach (Employee E in employees)
        {
            if (equalsidcomparison(newGuy) == true) { };
        }

I feel like this is an easy fix but i'm new and at a loss for what it should be. I've looked around to no avail and I can't stray too far from the process i've used, as it's part of an assignment.

3
  • assign it an initial value. Commented Apr 14, 2017 at 1:35
  • This is usually the solution for variables of other data types, but how would I go about doing it with a struct? I assume I'd have to go through and set "0" values for all its fields? Commented Apr 14, 2017 at 1:40
  • 2
    when I said "assign it an initial value" I mean "Employee newGuy = new Employee();" local variables must all have a value before the method in which it belongs exits. Otherwise, you'll get the compiler error you've just got. Commented Apr 14, 2017 at 1:43

2 Answers 2

2

You need to initialize newGuy with operator new. You can combine its field initialization with the declaration:

Employee newGuy = new Employee {
    id = nextIDnumber
,   Name = txtbName.Text
,   department = (string)comboDept.SelectedItem
,   title = comboJob.SelectedText
,   phone = txtbPhone.Text
};
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. Thank you very much.
0

You need to instantiate an instance of your variable -- like so:

Employee newGuy = new Employee();
newGuy.id = nextIDnumber;
...

You could also write it as 'var newGuy = new Employee();' (depending on who you ask, this is the better syntax...)

You could also create your object like: (note -- this requires your 'set' method to be accessible)

Employee newGuy = new Employee()
  {
      id = nextIDnumber,
      Name = "something",
      ... 
  };

I would also recommend using consistent casing in your variable names. (id, Name, title, ...). In general, the standard for c# is to use PascalCase for publicly available methods/properties.

1 Comment

Thanks for the response and suggestion. I usually do a decent job at being consistent with casing but this project is rushed and I got a bit lazy with it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.