Click here to Skip to main content
15,917,645 members
Home / Discussions / C#
   

C#

 
GeneralRe: Data binding Pin
half-life25-Jan-09 2:54
half-life25-Jan-09 2:54 
GeneralRe: Data binding Pin
Wendelius25-Jan-09 2:59
mentorWendelius25-Jan-09 2:59 
QuestionDetecting similar URLs Pin
Meysam Mahfouzi24-Jan-09 21:17
Meysam Mahfouzi24-Jan-09 21:17 
AnswerRe: Detecting similar URLs Pin
Wendelius24-Jan-09 22:14
mentorWendelius24-Jan-09 22:14 
QuestionListener Pin
shuahsan24-Jan-09 17:28
shuahsan24-Jan-09 17:28 
AnswerRe: Listener Pin
SeMartens25-Jan-09 1:05
SeMartens25-Jan-09 1:05 
QuestionSNMP Pin
shuahsan24-Jan-09 17:26
shuahsan24-Jan-09 17:26 
QuestionGenerics - casting int/double to <t></t> Pin
devvvy24-Jan-09 17:24
devvvy24-Jan-09 17:24 
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

AnswerRe: Generics - casting int/double to Pin
DaveyM6924-Jan-09 22:19
professionalDaveyM6924-Jan-09 22:19 
AnswerRe: Generics - casting int/double to Pin
S. Senthil Kumar24-Jan-09 22:54
S. Senthil Kumar24-Jan-09 22:54 
GeneralRe: Generics - casting int/double to Pin
devvvy25-Jan-09 14:12
devvvy25-Jan-09 14:12 
AnswerRe: Generics - casting int/double to Pin
DaveyM6924-Jan-09 22:57
professionalDaveyM6924-Jan-09 22:57 
GeneralRe: Generics - casting int/double to Pin
devvvy25-Jan-09 14:10
devvvy25-Jan-09 14:10 
GeneralRe: Generics - casting int/double to Pin
DaveyM6926-Jan-09 0:15
professionalDaveyM6926-Jan-09 0:15 
Questiondata gridview problem Pin
bfis10813724-Jan-09 14:01
bfis10813724-Jan-09 14:01 
AnswerRe: data gridview problem Pin
paas25-Jan-09 6:17
paas25-Jan-09 6:17 
QuestionCan any one help please? Pin
Member 284246824-Jan-09 13:52
Member 284246824-Jan-09 13:52 
AnswerRe: Can any one help please? Pin
Not Active24-Jan-09 16:52
mentorNot Active24-Jan-09 16:52 
AnswerRe: Can any one help please? Pin
Christian Graus25-Jan-09 0:44
protectorChristian Graus25-Jan-09 0:44 
AnswerRe: Can any one help please? Pin
Guffa25-Jan-09 2:37
Guffa25-Jan-09 2:37 
GeneralRe: Can any one help please? Pin
NikoTanghe26-Jan-09 3:44
NikoTanghe26-Jan-09 3:44 
GeneralRe: Can any one help please? Pin
Guffa26-Jan-09 3:49
Guffa26-Jan-09 3:49 
QuestionGet html from url Pin
macsheriff24-Jan-09 8:00
macsheriff24-Jan-09 8:00 
AnswerRe: Get html from url Pin
Guffa24-Jan-09 8:44
Guffa24-Jan-09 8:44 
GeneralRe: Get html from url Pin
macsheriff24-Jan-09 9:03
macsheriff24-Jan-09 9:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.