|
there's always
List< Customer > myArray = new List< Customer > ();
if you aren't tied to using an ArrayList
|
|
|
|
|
hpjchobbes wrote: My understanding of why the first way doesn't work is because an ArrayList could hold different object types. So I need to box/unbox it before I can use it
Sounds about right to me.
You could use a Generic List to achieve what you're trying to do. It is in the System.Collections.Generic namespace, and is similar to an array list, but the items contained in it are strongly typed (so you won't have to do the boxing/unboxing)
You could use it as follows:
<br />
List<Customer> myCustomerList = new List<Customer>();<br />
<br />
Customer customer = new Customer();<br />
<br />
myCustomerList.Add(customer);<br />
myCustomerList[0].Name = "Bob Smith";<br />
----
Edit: Doh! Too slow...
|
|
|
|
|
hpjchobbes wrote: If I create a class and place an object of that class into an ArrayList, is it possible to access the members of the object of a particular element of the ArrayList without having to first cast it to a new class?
You don't cast the object to a new class, you cast the reference of the object to the class. Creating a new class instance serves no purpose at all, that instance would be thrown away when you assign the new reference to the variable.
Customer temp;
temp = (Customer)ArrayList[0];
hpjchobbes wrote: So I need to box/unbox it before I can use it.
Actually, boxing is only used for value types. If you for example store integers in an ArrayList, each integer is boxed inside an object which is stored on the heap. That way the integer becomes a reference type so that the reference can be stored in the ArrayList.
If you are using framework 2, you can use a generic list instead. They are type safe, so there is no need for any casting:
List<Customer> list = new List<Customer>();<br />
list.Add(new Customer());<br />
<br />
list[0].Name = "Eric Gently";<br />
<br />
Customer temp = list[0];
Generic lists also works with value types, so there is no boxing if you store value types in a list.
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
Thanks for the answers everyone! I am using 2.0 so I looked at the generic List<t> and it seems to do what I need!
Looks like I have yet another topic to add to my list of things to learn: When to use ArrayList or List<t>
Thanks again for the information and assistance!!
|
|
|
|
|
hpjchobbes wrote: Looks like I have yet another topic to add to my list of things to learn: When to use ArrayList or List
There's no need to use ArrayList in .NET 2.0 anymore.
modified 12-Sep-18 21:01pm.
|
|
|
|
|
hpjchobbes wrote: When to use ArrayList
That is very easy: Never.
If you ever need a list of object references, use List<object> instead of ArrayList .
Most of the time when you store objects of different types in a list, they still have a common base type that you can use rather than an object reference, like for example List<Control> for storing references to any type of controls.
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
Hi I createed a DB under MS SQL 2005, table has 3 columns name, box, PhoneNumber.
In my c# code i want to check if the phone number entered by the user exists under PhoneNumber column of my table.
Thanks in advance.
|
|
|
|
|
So what have you tried that doesn't work?
Most people won't be inclined to give you any help unless they see you've at least tried something yourself.
Posting code that you're having problems with at least shows that you've tried to figure the problem out...
|
|
|
|
|
Create a stored proc something like this and call it from your app.
IF( EXISTS( SELECT * FROM table WHERE PhoneNumber = @PhoneNumber) )
RETURN 1
ELSE
RETURN 0
only two letters away from being an asset
|
|
|
|
|
write a query and check it
|
|
|
|
|
I populated an OleDb DataTable using a multi-table SELECT statement and need to bind some textboxes and comboboxes with the values from either one or the other table contained in the SELECT statement.
However, when I run the program I get the following error:
Child list for SerialNumber cannot be created
What does that error mean?
Thanks.
|
|
|
|
|
polishprogrammer,
Maybe show the SQL you're trying to do and a bit of code...
Regards,
Gareth.
|
|
|
|
|
Thanks. Here's a piece of the SQL SELECT statement:
SELECT ServiceDetails.SerialNumber, ServiceDetails.ManuDate, ServiceDetails.Manufacturer, " +
"ServiceDetails.Customer, ServicDetails.GasType, " +
"SerialNumber.SerialNumber, SerialNumber.Manufacturer, SerialNumber.ManufacturerDate, " +
"SerialNumber.Owner, SerialNumber.GasType, " +
"FROM SerialNumber, ServiceDetails WHERE " +
"SerialNumber.SerialNumber=ServiceDetails.SerialNumber " +
"AND SerialNumber.Manufacturer=ServiceDetails.Manufacturer " +
"AND SerialNumber.ManufacturerDate=ServiceDetails.ManuDate " +
"ORDER BY SerialNumber.SerialNumber"
The databinding that causes the problem involves binding to, say, the Manufacturer field in the SerialNumber table due to the fact that the same field name exists in both tables. I need to qualify the name of the field, but am unsuccessful when trying it in a databinding context, such as:
this.cbManuSer.DataBindings.Add("SelectedValue", dtSer, "SerialNumber.Manufacturer");
If I bind controls to fields in the datatable filled by the SQL SELECT statement given above that have unique names (not belonging to both tables), the binding works fine, but qualifying the name by table doesn't work. I get the Child list for x not created.
There must be a piece I'm missing, but I could not find a discussion of this topic.
|
|
|
|
|
polishprogrammer,
I am not to sure about the error, but i noticed you have an extra comma where there doesn't need to be one.
polishprogrammer wrote:
"SerialNumber.Owner, SerialNumber.GasType, " +
"FROM SerialNumber, ServiceDetails WHERE " +
I am a bit puzzled by your db tables setup though. Why have 3 columns in 2 different tables that are storing the same values?
Regards,
Gareth.
|
|
|
|
|
Thanks. I wound up just changing the names of the fields in one of the data tables and that worked for the data binding issue. Regarding the repetitive fields, well, it takes the 3 fields to uniquely identify a record, but I want to separate the data into two tables because the information in these particular tables aside from the primary key fields is quite different and used in different contexts, for different purposes.
I suppose I could create a lookup table that would link an ID to the three fields and both tables could reference the look up table, but, well, that would require more software revision because the ServiceDetails table is a recent addition to the database in question whereas the SerialNumber table has been used extensively with its current structure for quite some time and I'd have to redo a bit of code to use a modified structure.
|
|
|
|
|
Hi i developed a tool , which allows you to download whole the source code from microsoft at once.
You don't need vs2008 for this, and you can debug the source code without vs2008.
Just grab your copy from Net Mass Downloader.
You can contact me about the tool via my mail address keremskusmezer@gmail.com
Best Regards
Kerem Kusmezer
|
|
|
|
|
There was an excellent article that someone wrote on relation to updating GUI from a seperate thread.
This article related to setting a property called RaiseExceptionOfSomeKind = false.
Very simple yet very effecient.
I am sad to report that I am blanking out compeltely on that article. If someone has the foggiest idea as to what am I talking about please post a link.
This is what happens when you give up drinking..amnesia.
Thanks
|
|
|
|
|
Updating a Gui from a thread other than the one it was created on "often leads to unpredictable results" (MSDN[^]). See here[^] for the proper way to do so.
|
|
|
|
|
Found what I was looking for. It took a little while to do the right search.
http://www.codeproject.com/KB/user-controls/AlternateMultithreadedGUI.aspx.
The property I was looking for is CheckForIllegalThreadCalls = false.
Back to drinking,
|
|
|
|
|
THRERE are two ways
1. the wrong way that microsoft allowed
Set this property like:
<br />
CheckForIllegalCrossThreadCalls = false;<br />
2. the right way :
create a delegate to the function that updates the GUI and a handler
and use the invoke Method to wake the method
see here : http://msdn2.microsoft.com/en-us/library/zyzhdc6b.aspx[^]
Have Fun
Never forget it
|
|
|
|
|
Thank half-life. I was aware of the second way as well. I was experimenting with a few things so I thoiught of this CheckForIllegalCrossThreadCalls = false;
thing I read on codeproject a while ago.
|
|
|
|
|
glad to help
Have Fun
Never forget it
|
|
|
|
|
I wanted to add a dockable interface to my application and I don't really like the idea of including a third-party dll in my application. So I though and I had the idea of embedding my controls in toolbars.
Can you give me an example of how to put a custom control in a toolbar. Or is this impossible? Help would be greatly appreciated.
|
|
|
|
|
So, I'd like this to work:
Thread t = new Thread(new ThreadStart(StartProcess));<br />
int i = 0;<br />
int numThreads = 10;<br />
while (i <= numThreads)<br />
{<br />
t.Start();<br />
i++;<br />
}<br />
But I know it doesn't .
Can anyone tell me how to get this working?
|
|
|
|
|
Keep in mind you don't want to do something like this so I'm not sure what value there is in showing you.
int i = 0;<br />
int numThreads = 10;<br />
while (i <= numThreads)<br />
{<br />
Thread t = new Thread(new ThreadStart(StartProcess));<br />
t.Start();<br />
i++;<br />
}
led mike
|
|
|
|