|
correct, you can't.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
in C# pointers are applicable to value types only. Example: pixel bytes inside a Bitmap,
but not the whole Bitmap.
|
|
|
|
|
I would like to make a "dumb" conversion factory, to convert one type to another (call them types A and B). To perform this action, I require that I can construct the return type with a default constructor. I will then take all the public read/write properties of both A and B, compare them, then copy the matching ones over. However, I want this to be a generic operation, that can convert between any two classes. Therefore, I am attempting to do the following:
public class AbstractBase<T, U> where T : AbstractBase<T, U> {
public static implicit operator T(U other) {
T rval = new T();
foreach (PropertyInfo Upi in typeof(U).GetProperties()) {
PropertyInfo Tpi = typeof(T).GetProperty(Upi.Name, Upi.PropertyType);
if (Tpi != null && Tpi.CanWrite && Upi.CanRead
&& Upi.GetIndexParameters().Length == 0)
Tpi.SetValue(rval, Upi.GetValue(other, null), null);
}
return rval;
}
public static implicit operator U(T other) {
U rval = new U();
foreach (PropertyInfo Tpi in typeof(T).GetProperties()) {
PropertyInfo Upi = typeof(U).GetProperty(Tpi.Name, Tpi.PropertyType);
if (Upi != null && Upi.CanWrite && Tpi.CanRead
&& Tpi.GetIndexParameters().Length == 0)
Upi.SetValue(rval, Tpi.GetValue(other, null), null);
}
return rval;
}
} And then declare a user to this class as follows:
public class myClass : AbstractBase<myClass, otherClass> { ... } I know that one of the types passed in, say T, implements AbstractBase<T, U>, so I thought that I could do the above, since my conversion is from one type that inherits from this class to some other unknown type. However, I get the error that I can only convert between types related to the class. Does anyone know a way around this so I can use the above code without retyping it in every single class (like myClass in the above example) where I need this functionality? Thanks,
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
I suspect that you need to put a new() constraint in
public class AbstractBase<T, U> where T : AbstractBase<T, U> new()
where U : class, new()
|
|
|
|
|
My current solution is to include these methods in a static utility class as template methods, then call them from within the class that will use them to convert to/from it's type. That way, I only need one definition as follows:
public static class Helper {
public static T Convert<T, U>(U other) where T : new() {
}
}
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
I had to write a validation function for some data being read from an app.config. The point of the validation was to ensure that the value was a power of 2 and within a certain range (4k - 4mb). This is how I did it:
private bool IsPowerOfTwo(int numeric, int min, int max)
{
bool valid = false;
if (numeric >= min && numeric <= max && numeric % 2 == 0)
{
int pow2 = min;
while (pow2 <= max && !valid)
{
valid = ((pow2 *= 2) == numeric);
}
}
return valid;
}
Assume that the min and max values are valid powers of 2. I passed in the values 4096 for min, and 4194304 for max).
How would you do it?
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
I'd make sure there was only one '1' in the binary representation of the number.
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Hi John, the following test applies to signed integers and returns true if and only if the
value is either zero, a positive power of 2, or the minimum value (which equals the negative
of a power of 2); it basically checks that there are either zero or one bits set:
(x & -x)==x
It relies on the fact that -x, when looking from right to left, preserves all the low-end zeroes
of x, as well as one low-end one, and flips all the other bits.
|
|
|
|
|
Pretty nifty, I must say
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
Found this nifty little function as part of the excellent AForge library [^]
public static bool IsPowerOf2(int x)
{
return (x & (x - 1)) == 0;
}
|
|
|
|
|
yep, that's mathematically equivalent to mine. It too calls zero a power of two.
PS: how is the memory?
|
|
|
|
|
Luc Pattyn wrote: PS: how is the memory?
Haven't solved it yet. We are profiling it over several days to get a better picture. Will let you know what happens.
|
|
|
|
|
An alternative approach could be:
private static bool IsPowerOf2(double number)
{
double value = Math.Log(number, 2);
return value == Convert.ToDouble(Convert.ToInt32(value));
}
|
|
|
|
|
Hi,
i have added one checkbox to my datagridview..
i want to select the checkbox....
after selecting the check box i need to get the whole row details of that perticular checked row..
DataGridViewCheckBoxColumn ckb = new DataGridViewCheckBoxColumn();
ckb.Name = "Checkbox";
ckb.HeaderText = "Select";
dataGridView1.Columns.Insert(0, ckb);
how do i select and deselect the checkbox check options...
please reply me ASAP
|
|
|
|
|
<br />
for (int cnt = 0; cnt <= DataGridView1.Rows.Count - 1; cnt++)<br />
{<br />
DataGridView.Rows[cnt].Cells["ColSelect"].Value = true;<br />
}<br />
1. Where ColSelect is your column name.
2. If you want to de-select just write false instead of true.
3. And if you want to know which row is checked and which is not then
<br />
for (int index = DataGridView1.Rows.Count - 1; index >= 0; index--)<br />
{<br />
if (Convert.ToBoolean(DataGridView1.Rows[index].Cells[0].Value))<br />
{<br />
"Selected"<br />
}<br />
else<br />
{<br />
"Not Selected"<br />
}<br />
}
<br />
:-D <br />
<br />
Regards
Pankaj Joshi
If you want to shape your dreams into reality, please wake-up...
|
|
|
|
|
Hi,
I am writing my first control and would like to create an event so the form the control is sitting on can pick up user activity in the control.
How can I do this?
Thanks in advance.
Stephen
|
|
|
|
|
|
I have a MDI container, inside of it I have a child form that is maximized. If I have a second form created and displayed by the first form that is maximized (with its MdiParent property set properly) the second form will also be maximized. Is there anyway to override this behavior? I know I can use ShowDialog, but I don't want it to be a dialog I want it to be a normal, nonmaximized window. The property on the form is set to Normal and I event do a WindowState = FormWindowState.Normal in the constructor, but it still comes as maximized.
Any suggestions?
|
|
|
|
|
eggsovereasy wrote: Is there anyway to override this behavior?
No. All the MDI child forms are treated the same. If one is maximized, they all are. If one is "normal", they all are.
|
|
|
|
|
Disappointing
Thank you for the reply though, I guess I will try and make something work with ShowDialog().
|
|
|
|
|
My scenario is:
Based on Remoting concept, Server is listening to client on particular port using TCPChannel. Suddenly Server (Listner) went off, which client does not have information. When client sends a message, error is thrown saying "No connection could be made because the target machine actively refused it".
Is there any way to re-establish client - server connection?
Here Server is using randomly selected ports, so we can't hardcode port at client.
Client is getting information of port from configuration file created at user's local directory.
Thanks
|
|
|
|
|
From my experience, you only get the exception if you try to call a remote method while the server is down. As soon as the remoting server is running again, all remoting method calls work as if the server was there all the time.
If you say that the server is using randomly selected ports, how do your clients connect to the remoting server?
You have to give a URI for the remoting server object and part of it is the port, so how can this be "randomly selected"?
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
hello everybody,
I have set some text as labels used in windows app, then I have set the Cursor one bye one for their respective input. Now I want to set fixed width for their fixed input size, as user cannot enter more text as much allow for that. I want also to change the BackGroundColor for their fixed input area.
Please help me.
Thank You
Dipak
|
|
|
|
|
Just use Console.ReadKey in a while loop, and keep track of how many keys they have entered (or keep track of the cursor position). The code would be similar to the following:
ConsoleKeyInfo cki;
string str = string.Empty;
do {
cki = Console.ReadKey(true);
switch (cki.Key) {
case ConsoleKey.LeftArrow:
--Console.CursorLeft;
break;
case ConsoleKey.RightArrow:
++Console.CursorLeft;
break;
case ConsoleKey.UpArrow:
goto case ConsoleKey.Home;
case ConsoleKey.DownArrow:
goto case ConsoleKey.End;
case ConsoleKey.Home:
Console.CursorLeft = 0;
break;
case ConsoleKey.End:
Console.CursorLeft = 20;
break;
default:
if (IsPrintable(cki.Key)) {
str.PadRight(Console.CursorLeft - str.Length + 1, ' ');
str[Console.CursorLeft] = cki.KeyChar;
Console.Write(cki.KeyChar);
}
break;
}
if (Console.CursorLeft > 20)
--Console.CursorLeft;
} while (cki.Key != ConsoleKey.Enter); Hope this helps,
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
fellas been developing a c# battleship game and have did go enuf so far
quick question, when i shot at the grid i write back the coordinates such as 1-1, but this is trying to compare it will the coordinates of the sub(for example) which is 1-1,2-1, but i tseems to trying to compare againest the entire coordinates how would i extract the 1-1 or the 2-1??
the cooridnates can go past 10 meaning it cant be a set number
any help please?
thanks
|
|
|
|