|
Sorry, I'm new to C# so please forgive the ignorance...
I have a list, and want to make another list from the first list:
int[] list1 = { 1, 3, 5, 7, 9 };
int[,] list2;
for (int i = 0; i < list1.Length; i++)
{
list2[i] = [item from list1] , [i];
}
How do i put items in list2? I'm looking for:
{{1,0} {3,2} {5,3} {7,4} {9,5}}
Thank you for your time.
|
|
|
|
|
What are you really looking for? Your example does not make sense to me.
|
|
|
|
|
I have this list:
int[] list1 = { 1, 3, 5, 7, 9 };
Using 'for', how do I create the following list using the first list?
{{1,0} {3,2} {5,3} {7,4} {9,5}}
Thanks.
|
|
|
|
|
The second elements of the sub-array's in your second array does not make enough sense and does not match your description.
{{1,0} {3,1} {5,2} {7,3} {9,4}} would make sense and match your description.
|
|
|
|
|
I'm sorry all, my example was poor. Yes, this is what is needed.
I can't figure out the syntax needed inside 'for'. Could you help me please?
|
|
|
|
|
warning: untested
int[] list1 = { 1, 3, 5, 7, 9 };
int[,] list2 = new int[5,2];
for (int i = 0; i < list1.Length; i++)
{
list2[i, 0] = list1[i];
list2[i, 1] = i;
}
|
|
|
|
|
Thank you. I wasn't even close & wasn't aware of 'code block' which scrambled my example.
Thanks again for your patience.
|
|
|
|
|
|
int[,] gives you a two dimensional list, not a list of pairs of numbers.
int[3,3] gives you a grid of 9 values, all of them a single int. To store pairs you'd need to define a struct, or you could use a map if you wanted ( which pairs values and lets you look up one value based on the other instead of an index ). I suspect that you're just experimenting, b/c I can't see any use for your final example ( the second value is always the same as the array index, so you have access to that number all the time anyhow ). Imagine I had a tic tac toe board for a game:
| |
___________
| |
___________
| |
Now - I can create a grid that's 3x3 with List[3,3] and I can use co-ordinates to look up positions in that list and track the game positions. That's the sort of thing a 2D array is used for.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
Thank you.
I want to get:
{{1,0} {3,2} {5,3} {7,4} {9,5}}
from an original list of:
int [] list1 = { 1, 3, 5, 7, 9 };
using the increment in 'for' as the second number
|
|
|
|
|
Then you need to define a struct that contains those two numbers and build a list of them.
struct myStruct
{
public int value;
public int index;
}
will work just fine. Of course then you need to write your own code if you want to search or sort the list.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
I have a datalist with an embedded dropdownlist. How can I fire an event from the dropdownlist and caclulate the results for a label in the datalist?
The datalist is similar to a shopping cart. The fore mentioned dropdownlist is the qty for each item. The total price is calculted using the qty and another field "PriceEach".
Most of the data is retrieved from a sql stored produre.
I wrote the following Item_Bound function to set the totalprice at the page_load.
protected void Item_Bound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DropDownList ddlist = (DropDownList)e.Item.FindControl("ddlQty");
Label TotPrice = (Label)e.Item.FindControl("lblTotalPrice");
Label PriceEA = (Label)e.Item.FindControl("lblPrice");
if (PriceEA.Text != null && PriceEA.Text.ToString() != "")
{
string price = PriceEA.Text.Replace("$", "");
Double dblPrice = Convert.ToDouble(price);
Double ProdQty = Convert.ToDouble(ddlist.SelectedValue.ToString());
Double ExtPrice = (dblPrice * ProdQty);
TotPrice.Text = ExtPrice.ToString();
}
}
}
|
|
|
|
|
Then what is the error you are getting... The code looks fine to me.
|
|
|
|
|
I'm trying to add code to get the qty when it is changed. I added an event to the Dropdownlist "ddlQty" called OnSelectedIndexChanged="ddlQty_Changed" and started to add the following:
protected void ddlQty_Changed(object sender, DataListItemEventArgs e)
{
DropDownList ddQty = (DropDownList)e.Item.FindControl("ddlQty");
if (ddQty.SelectedValue != null && ddQty.SelectedValue.ToString() != "")
{
Double ProdQty = Convert.ToDouble(ddQty.SelectedValue.ToString());
}
}
When I run this I get the error: No overload for 'ddlQty_Changed' matches delegate 'System.EventHandler'
|
|
|
|
|
I think the signature of ddlQty_Changed should be
protected void ddlQty_Changed(object sender, EventArgs e)
|
|
|
|
|
When I try that I can't find the "e.item" in:
DropDownList ddQty = (DropDownList)e.Item.FindControl("ddlQty");
Fixed it:
protected void ddlQty_Changed(object sender, EventArgs e)
{
foreach (DataListItem item in DataList1.Items)
{
Label TotPrice = (Label)item.FindControl("lblTotalPrice");
Label PriceEA = (Label)item.FindControl("lblPrice");
DropDownList ddlist = (DropDownList)item.FindControl("ddlQty");
if (ddlist.SelectedValue != null && ddlist.SelectedValue.ToString() != "")
{
string price = PriceEA.Text.Replace("$", "");
Double dblPrice = Convert.ToDouble(price);
Double ProdQty = Convert.ToDouble(ddlist.SelectedValue.ToString());
Double ExtPrice = (dblPrice * ProdQty);
TotPrice.Text = String.Format("${0 ,0}", ExtPrice.ToString());
}
}
}
Thanks for all the help
modified on Tuesday, November 10, 2009 3:55 PM
|
|
|
|
|
Thats nice.
|
|
|
|
|
Hello,
The question is simple but I found it tricky to accomplish.
I want to extract all the text from within the default.code node:
< default.code >
<script type="text/javascript"><!--
google_ad_client = "<?php echo($adsense_pubid);?>";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "image";
google_ad_channel = "";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "245917";
google_color_text = "555555";
google_color_url = "555555";
google_ui_features = "rc:6";
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
< /default.code >
The problem is that when I read it, the is also considered a XML node. I simply want to extract the javascript code block.
How can I do this ?
Thanks
|
|
|
|
|
The script tag IS an XML node. However you can select the default.code and select the InnerXml ( from memory ) to get the script blocks with the XML tags which also happen to be script tages.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
Hi,
I have a habit of storing my connection string in a member of a base clase for my data access objects. That way, each object can access the database independently of each other. However, I was wondering how others handle this.
With the current way, each object is independent, but each object must maintain its own connection string value, even if its the same throughout all objects of the application.
One answer would be a static member, that way it's shared throughout all objects, but then the capability for different objects to access different databases would be lost unless the static member was constantly updated.
Thanks for any suggestions.
|
|
|
|
|
That's done in the Data Access Layer. Are you saying you do it in your business objects?
|
|
|
|
|
Nope, this is in a base class for the Data Access Layer code objects. Sorry if my post was not clear.
|
|
|
|
|
Good.
In my data access base classes[^] I hold a System.Data.IDbCommand , if I want the connection string I use this.cmd.Connection.ConnectionString ; there's no need to store it a second time.
I don't keep instantiating and throwing away connection and command instances -- that's wasteful (not that I've benchmarked it).
But now I don't know what you mean by "each object can access the database independently". What objects? I have one data access class that is basically a wrapper around one Connection/Command (with specialized versions for the various databases I use). I can instantiate one for each database and I can clone it when I need an additional connection to a particular database.
|
|
|
|
|
class base
{
string m_connString;
public string ConnString
{
get{ return m_ConnString;}
private set{ m_ConnString = value;
}
public Base()
{
ConnString = default_value
}
pubic Base(string connString)
{
ConnString = connString;
}
}
class Derived : Base
{
pubic Derived(string connString) : base(connString)
{
}
}
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
I've got a tree view with multiple nodes added during run time and they have different images from image list.
But once you start clicking them, they change the image to recent added one.
There is somthing to do with SelectedImageKey property. How to disable such changes?
Чесноков
|
|
|
|