471,350 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Having trouble understanding assignement of Object in this instance

RSH

I am slowly getting the hang of objects and creating my own. I was given
some help in assigning an object to a ComboBox collection. I have been
looking at it and I get the concept which is very powerful but I'm having a
bit of a problem conceptually. I was hoping someone might be able to shed
some light on creating this object and assigning it in the manner. Also how
would I reference this object by name after it is created in this manner?
Is there a collection of objects that I could iterate through with respect
to this ComboBox?

Thanks for the help,
Ron

// Fill ComboBox with Employees

public void FillEmployees(DataSet DS, ComboBox ComboList)

{

if (bAuthenticated == true)

{

ComboList.Items.Clear();

foreach (DataRow DR in DS.Tables[0].Rows)

{

String sTemp = DR["ID"].ToString().PadLeft(8, '0');

ComboList.Items.Add(new Employee(sTemp, DR));

}

}

}

// Employee Class

class Employee

{

private String iD;

private String firstName;

private String lastName;

private int iselected;
public Employee(String ID, String LastName, String FirstName)

{

iD = ID.PadLeft(8, '0');

lastName = LastName;

firstName = FirstName;

iselected = 0;

}

public Employee(String ID, DataRow DR)

{

iD = DR["ID"].ToString().PadLeft(8, '0');

lastName = DR["LastName"].ToString();

firstName = DR["FirstName"].ToString();

iselected = 0;

}

public string ID

{

get { return iD; }

set { iD = value; }

}

public string FirstName

{

get { return firstName; }

set { firstName = value; }

}

public string LastName

{

get { return lastName; }

set { lastName = value; }

}

public int iSelected

{

get { return iselected++; }

set { iselected = value; }

}

public override string ToString()

{

return lastName + " " + firstName;

}

}

// ComboBox Select Index Changed Event

private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)

{

MessageBox.Show(((Employee)comboBox1.SelectedItem) .ID.ToString());

richTextBox1.Text += ((Employee)comboBox1.SelectedItem).FirstName.ToStr ing()
+ " " + ((Employee)comboBox1.SelectedItem).LastName.ToStri ng() + "\n";

comboBox1.Items.Remove((Employee)comboBox1.Selecte dItem);

}


Jan 11 '06 #1
3 1255

RSH wrote:
I am slowly getting the hang of objects and creating my own. I was given
some help in assigning an object to a ComboBox collection. I have been
looking at it and I get the concept which is very powerful but I'm having a
bit of a problem conceptually. I was hoping someone might be able to shed
some light on creating this object and assigning it in the manner. Also how
would I reference this object by name after it is created in this manner?
Is there a collection of objects that I could iterate through with respect
to this ComboBox?

[snip code]

Objects don't necessarily have names, and collections don't necessarily
support getting contained members by name. Of course, there's always
_some_ way to 'get at' them. You can see that here in the case of these
Employee objects that you put in the ComboBox. See the line where you
put them in:

ComboList.Items.Add(new Employee(sTemp, DR));

that tells you that ComboBox ComboList has a a collection object called
Items. So you go to the documentation (or just the object browser) and
you see
[C#]
public ComboBox.ObjectCollection Items {get;}

A System.Windows.Forms.ComboBox.ObjectCollection representing the items
in the ComboBox.

Remarks
This property enables you to obtain a reference to the list of items
that are currently stored in the ComboBox. With this reference, you can
add items, remove items, and obtain a count of the items in the
collection. For more information on the tasks that can be performed
with the item collection, see the
System.Windows.Forms.ComboBox.ObjectCollection class reference topics.


So then you go and look at ComboBox.ObjectCollection, to see the ways
you can get objects back out of the collection. And you see that this
class implements IEnumerable, which means you can foreach over the
collection. So that's one way you can get each item back. Another way
is the indexer for the collection - Items[0] is the first member of the
collection, Items[1] is the second, and so on.

--
Larry Lard
Replies to group please

Jan 11 '06 #2
RSH
Larry,

Thanks for the explanation. I didn't really think of tackling it like that
but wow that is powerful too! I'm starting to see the light that you
probablly have seen long before me...this OOP is seriously powerful but it
sure requires looking at things from a different angle than the old VB
procedural days!

Very cool!

Ron
"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...

RSH wrote:
I am slowly getting the hang of objects and creating my own. I was given
some help in assigning an object to a ComboBox collection. I have been
looking at it and I get the concept which is very powerful but I'm having
a
bit of a problem conceptually. I was hoping someone might be able to
shed
some light on creating this object and assigning it in the manner. Also
how
would I reference this object by name after it is created in this manner?
Is there a collection of objects that I could iterate through with
respect
to this ComboBox?

[snip code]

Objects don't necessarily have names, and collections don't necessarily
support getting contained members by name. Of course, there's always
_some_ way to 'get at' them. You can see that here in the case of these
Employee objects that you put in the ComboBox. See the line where you
put them in:

ComboList.Items.Add(new Employee(sTemp, DR));

that tells you that ComboBox ComboList has a a collection object called
Items. So you go to the documentation (or just the object browser) and
you see
[C#]
public ComboBox.ObjectCollection Items {get;}

A System.Windows.Forms.ComboBox.ObjectCollection representing the items
in the ComboBox.

Remarks
This property enables you to obtain a reference to the list of items
that are currently stored in the ComboBox. With this reference, you can
add items, remove items, and obtain a count of the items in the
collection. For more information on the tasks that can be performed
with the item collection, see the
System.Windows.Forms.ComboBox.ObjectCollection class reference topics.


So then you go and look at ComboBox.ObjectCollection, to see the ways
you can get objects back out of the collection. And you see that this
class implements IEnumerable, which means you can foreach over the
collection. So that's one way you can get each item back. Another way
is the indexer for the collection - Items[0] is the first member of the
collection, Items[1] is the second, and so on.

--
Larry Lard
Replies to group please

Jan 11 '06 #3

RSH wrote:
Larry,

Thanks for the explanation. I didn't really think of tackling it like that
but wow that is powerful too! I'm starting to see the light that you
probablly have seen long before me...this OOP is seriously powerful but it
sure requires looking at things from a different angle than the old VB
procedural days!


Yes, which makes for interesting mental gymnastics when you've made the
switch and then have to maintain VB6 code :)

Really I think a very productive learning exercise is just exploring
the Framework (via the docs and the object browser) - doing this you
get to see OO principles 'in action', which probably teaches you better
than the usual theoretical examples involving Dogs and Animals, and you
also get to learn the '.NET way' of doing things, which is almost
always different and - IMO, once you think in the same way - more
elegant than the procedural way.

For a good example of what I mean, have a look at Bob Powell's GDI+
FAQ. Even if you don't do any graphics work at all, it's a good insight
into various .NET technologies and approaches.

--
Larry Lard
Replies to group please

Jan 11 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by I am Sam | last post: by
17 posts views Thread by Alexander Eisenhuth | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.