|
I was having problem (see below "TROUBLE") casting int/double to generics ... This is what I intended to write,
<br />
public T RecursiveGetNextRandom<t>(T Max, T Min, List<t> lstExcludedNum) <br />
{<br />
int nNextNum = 0;<br />
double dbNextNum 0;<br />
T NextNum;<br />
<br />
nMin = Convert.ToInt32(Min);<br />
nMax = Convert.ToInt32(Max);<br />
nNextNum = oRand.Next(nMin, nMax);<br />
if(Max is double)<br />
{<br />
dbNextNum = oRand.NextDouble() * nNextNum;<br />
NextNum = (T) dbNextNum; << TROUBLE: Can't cast to <t>!<br />
} else if (Max is int) {<br />
NextNum = (T) nNextNum; << TROUBLE: Can't cast to <t>!<br />
}<br />
<br />
return NextNum;<br />
}<br />
</t></t></t></t>
I ended up writing the following ugly code:
public static object RecursiveGetNextRandom(object Max, object Min, ArrayList lstExcludedNum) <br />
{<br />
object NextNum = 0;<br />
Random oRand = new Random(Guid.NewGuid().GetHashCode());<br />
double dbMin;<br />
double dbMax;<br />
int nNextNum;<br />
<br />
if (Min == null)<br />
{<br />
Min = Int32.MinValue;<br />
}<br />
<br />
if (Max == null)<br />
{<br />
Max = Int32.MaxValue;<br />
}<br />
<br />
if (!(Max is int || Max is double))<br />
{<br />
...<br />
}<br />
<br />
dbMin = Convert.ToInt32(Min);<br />
dbMax = Convert.ToInt32(Max);<br />
SwapIfGreater(ref dbMin, ref dbMax);<br />
nNextNum = oRand.Next(Convert.ToInt32(Math.Floor(dbMin)), Convert.ToInt32(Math.Floor(dbMax)));<br />
<br />
if (Max is double)<br />
{<br />
NextNum = (double)nNextNum * oRand.NextDouble();<br />
}<br />
else if (Max is int)<br />
{<br />
NextNum = (int)nNextNum;<br />
}<br />
<br />
if (lstExcludedNum != null)<br />
{<br />
if (lstExcludedNum.Contains(NextNum))<br />
{<br />
RecursiveGetNextRandom(Max, Min, lstExcludedNum);<br />
}<br />
}<br />
<br />
return NextNum;<br />
}<br />
Any suggestion? Thanks!
dev
|
|
|
|
|
You may be able to create a generic interface, and provide an implementation for each numric type you wish to support... could be an interesting solution if it works, I haven't tried it.
There's also an excellent article here on CP[^] about generic numerics.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
|
Thanks, I have used Convert class since birth of .NET but never seen ChangeType. Good to know!
dev
|
|
|
|
|
The generic interface method does work.
IRand<int> intVersion = new Rand(); ;
Console.WriteLine(intVersion.GetNext(1, 2, new List<int>()));
IRand<long> longVersion = new Rand(); ;
Console.WriteLine(longVersion.GetNext(1, 2, new List<long>()));
public interface IRand<T>
{
T GetNext(T min, T max, List<T> exclude);
}
public class Rand : IRand<int>, IRand<long>
{
public int GetNext(int min, int max, List<int> exclude)
{
int result = 0;
return result;
}
#region IRand<long> Members
public long GetNext(long min, long max, List<long> exclude)
{
long result = 0;
return result;
}
#endregion
}
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Thanks, I just found it. This is from Darwen[^], which involves declaring <t> as IConvertible.
<br />
class RangedRandom<t><br />
where T : IConvertible<br />
{<br />
private Random _random = new Random();<br />
private double _min;<br />
private double _range;<br />
private static readonly Type ElementType = typeof(T);<br />
<br />
public RangedRandom(T min, T max)<br />
{<br />
_min = Convert.ToDouble(min);<br />
_range = Convert.ToDouble(max) - _min;<br />
}<br />
<br />
public T Next()<br />
{<br />
double rand = _random.NextDouble();<br />
double value = _min + (rand * _range);<br />
return (T)Convert.ChangeType(value, ElementType);<br />
}<br />
}<br />
</t>
dev
|
|
|
|
|
Yeah, that could be a good way of achieving what you want. I'd use a try/catch either inside the method or inside the calling method as even if T implements IConvertible, there's no guarantee that the conversion can actually be done - it depends on the class/struct's individual implementation.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
For whatever reason, when I try to set the value of the DatGridView, it doesn't stick. I have been wrecking by brain over why this is Here is my code
Basically here I am just building the data into my data gridview and then since they will be editing I want the amounts to be loaded into the dgv which is what PopulateList() does.
private void bookList_Load(object sender, EventArgs e)
{
BLL = new BooksBLL("C");
dataGridView1.DataSource = BLL.allBooks();
if (isEdit)
{
PopulateList();
}
}
private void PopulateList()
{
foreach (OrderedBook ob in order.MyOrderedBooksCollection)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (ob.BookCode.ToString() == dataGridView1.Rows[i].Cells[2].Value.ToString())
if (ob.CopiesOrdered > 0)
dataGridView1.Rows[i].Cells[0].Value = ob.CopiesOrdered.ToString();
}
}
}
Now the interesting thing is that the value property hold and then somehow disappears when the form is loading because when i check the value immediately after a button is pressed without touching anything, it is already null. Note that the dgv works fine other than the fact that the Populate list feature won't load the values.
|
|
|
|
|
It certainly sounds like your grid's data source is somehow getting reset to its apparent starting value of BLL.allBooks(). Have you considered creating a DataSet, DataTable or DataView from BLL.allBooks and having your PopulateList method update the desired column with 'CopiesOrdered', accepting those changes, and then setting your grid data source to that object?
|
|
|
|
|
I am doing work in asp.net using c# in VS2008. I would like to give output in java script window because client dont want button and address, tool bars. so i need to design my own window for out put. I am very new in this field. I really don know which ddl i need to use and where i need to place code (in html, in page_load)
Can any one please help me in this regard?
thanks
|
|
|
|
|
Member 2842468 wrote: I am very new in this field.
Then you probably shouldn't have taken on the work.
only two letters away from being an asset
|
|
|
|
|
Tell your client you were ripping them off, then buy a book and read it before taking more work. also, when you have ASP.NEt questions, use the ASP.NEt forum.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Member 2842468 wrote: I would like to give output in java script window because client dont want button and address, tool bars.
That is not possible. Even if you open a new window using Javascript, you can't get rid of the address field.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
yes,
this is possible
from javascript, use:
window.open(url, targetFrame, "toolbar=no,width=500,height=500,resizable,scrollbars");
<< Nearly all men can stand adversity, but if you want to test a man's character, give him power. >>
|
|
|
|
|
No, it's not possible. That doesn't remove the address field.
(It might work it if the page is a file on your own computer, but that's not really relevant.)
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Hi.
How to get html from url requiring login, with c#? First login the page and then get html.
for instance this url: https://www.google.com/accounts/Login?continue=http://www.google.com/
|
|
|
|
|
Send the login information in a post to the login page and get the cookies that you get in the response. Put the cookies in the request when you get pages that require login.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
I try this, but is not working. Not login.
CookieContainer cookie = new CookieContainer();
string pageData = string.Empty;
StreamReader respReader = null;
label1.Text = "Finding Site ...";
Application.DoEvents();
HttpWebRequest _mainRequest = (HttpWebRequest)WebRequest.Create("https://www.google.com/accounts/Login?continue=http://www.google.com/");
label1.Text = "Setting Parameters";
Application.DoEvents();
_mainRequest.Method = "POST";
_mainRequest.ContentType = "application/x-www-form-urlencoded";
_mainRequest.CookieContainer = cookie;
label1.Text = "Building String to Send";
Application.DoEvents();
StreamWriter reqWriter = new StreamWriter(_mainRequest.GetRequestStream());
label1.Text = "Sending Form data";
Application.DoEvents();
reqWriter.Write("Email=" + myMail + "&Passwd=" + myPass);
reqWriter.Close();
label1.Text = "Closing Response from Site";
Application.DoEvents();
_mainRequest.GetResponse().Close();
label1.Text = "Looking up secure Site";
Application.DoEvents();
_mainRequest = (HttpWebRequest)WebRequest.Create("http://www.google.com.tr");
_mainRequest.CookieContainer = cookie;
label1.Text = "Getting Response from Site";
Application.DoEvents();
respReader = new StreamReader(_mainRequest.GetResponse().GetResponseStream());
pageData = respReader.ReadToEnd();
label1.Text = "Cleaning up";
Application.DoEvents();
respReader.Close();
label1.Text = "Ready";
textBox1.Text = pageData;
Application.DoEvents();
|
|
|
|
|
You didn't get the cookies from the response.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
thank you. did work.
I using timer. but, page source code not refresh. Why?
|
|
|
|
|
macsheriff wrote: I using timer. but, page source code not refresh. Why?
Well, that's hard to say without knowing anything about your code. In what way would the source code refresh, and how would the timer cause that to happen?
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
I'm new to C# and trying to add a new row to my form using tableLayoutPanel using an Add button. When I click the Add Button I believe a new row is added but the previous row disappears. I've search the web and MS for help and followed their suggestions, but continue to receive the same results. Below is the code used in the click button method
private void buttonAddDebt_Click(object sender, EventArgs e)
{
// Set the GrowStyle to Add Rows.
debtTableLayoutPanel.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
// Ensure the AutoSize property is set to allow for the row to be added
debtTableLayoutPanel.AutoSize = true;
// Set Row Number to Be inserted
int InsertRowNum = debtTableLayoutPanel.RowCount;
debtTableLayoutPanel.AutoScroll = true;
debtTableLayoutPanel.Visible = true;
// Increment the row count by one for the new row.
debtTableLayoutPanel.RowCount++;
// Insert new RowStyles for correspoinding new row
RowStyle newRowStyle = new RowStyle();
newRowStyle.SizeType = SizeType.Absolute;
newRowStyle.Height = 30;
debtTableLayoutPanel.RowStyles.Insert(InsertRowNum,newRowStyle);
// Add the new row
debtTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(SizeType.Absolute, 30));
// Add controls to the new row
debtTableLayoutPanel.Controls.Add(textBoxDebtAmount, 0, InsertRowNum);
debtTableLayoutPanel.Controls.Add(textBoxInterestRate, 1, InsertRowNum);
debtTableLayoutPanel.Controls.Add(listBoxDebtType, 2, InsertRowNum);
debtTableLayoutPanel.Controls.Add(listBoxCompoundingFrequency, 3, InsertRowNum);
debtTableLayoutPanel.Controls.Add(numericUpDownAmortizationPeriods, 4, InsertRowNum);
debtTableLayoutPanel.Controls.Add(listBoxPaymentFrequency, 5, InsertRowNum);
debtTableLayoutPanel.Controls.Add(listBoxPaymentType, 6, InsertRowNum);
debtTableLayoutPanel.Controls.Add(textBoxPaymentAmount, 7, InsertRowNum);
/*
this.debtTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 29F));
this.debtTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.debtTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.debtTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.debtTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.debtTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.debtTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.debtTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.debtTableLayoutPanel.Size = new System.Drawing.Size(620, 27*debtTableLayoutPanel.RowCount);
*/
debtTableLayoutPanel.TabIndex++;
debtTableLayoutPanel.Visible = true;
} // end buttonAddDebt_Click
|
|
|
|
|
looking at the designer.cs file after dropping a TableLayoutPanel on a form, it produces 2 columns and 2 rows by using this code which you should be able to use as the base for your code:
this.tableLayoutPanel1.ColumnCount = 2;
this.tableLayoutPanel1.ColumnStyles.Add(
new System.Windows.Forms.ColumnStyle(
System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.ColumnStyles.Add(
new System.Windows.Forms.ColumnStyle(
System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.RowCount = 2;
this.tableLayoutPanel1.RowStyles.Add(
new System.Windows.Forms.RowStyle(
System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.RowStyles.Add(
new System.Windows.Forms.RowStyle(
System.Windows.Forms.SizeType.Percent, 50F));
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
hi evreyone
how i can make Beep in WinCE (C#) ?
thank's for any help
|
|
|
|
|
Not sure about CE, but try System.Console.Beep()
|
|
|
|
|