473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 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 1336

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: yuraukar | last post by:
I am trying to create a garbage collection class in C++ to collect instances of a specific class (so no general gc). The approach is to use smart pointers and a mark and a simple sweep gc. ...
3
by: sushant | last post by:
hi all, whats the difference between assignement and initialisation in C. is assignement related with scanf() and initialisation with (int x=10;) sushant
5
by: I am Sam | last post by:
The codebehind is as follows: I have instenced the DataSet dsclub2a protected System.Data.DataSet dsclub2a; and I have instanced the DropdownList as follows: protected...
8
by: Flack | last post by:
Hey guys, In my app I have a bitmap where drawing is done and in the form's paint method I show the bitmap: private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {...
17
by: Alexander Eisenhuth | last post by:
Hello, is there a assignement operator, that i can overwrite? class MyInt: def __init__(self, val): assert(isinstance(val, int)) self._val = val a = MyInt(10)
10
by: Henrik Dahl | last post by:
Hello! I have an xml schema which has a date typed attribute. I have used xsd.exe to create a class library for XmlSerializer. The result of XmlSerializer.Serialize(...) should be passed as the...
9
by: Pyenos | last post by:
Approach 1: class Class1: class Class2: def __init__(self):self.variable="variable" class Class3: def method():print Class1().Class2().variable #problem Approach 1.1:
10
by: Hendri Adriaens | last post by:
Hi, I'm trying to automate the creation of an excel file via COM. I copied my code below. I read many articles about how to release the COM objects that I create. The code below runs just fine...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.