|
Thanks,
I know about the 'valueType' and the 'struct'.
I'm using .net 4.0 - and i still did not found how to do it here.
I try to define in the 'where' an operator - but it not working.
|
|
|
|
|
How about dynamic , would that work? I haven't done much C# 4 programming so I'm not sure..
|
|
|
|
|
I dont see how 'dynamic' can help me here ( maybe i'm wrong )
|
|
|
|
|
dynamic supports operators and the like, right? So at least you'd be able to do that. What are your exact requirements?
|
|
|
|
|
Yes,
I try the dynamic already as the first solution - its not working as we expected in this case
|
|
|
|
|
Then I'm afraid what you want is impossible..
|
|
|
|
|
public class Foo<T> : IConvertible {
public void bar() {
T t = default(T);
((IConvertible)t) //Check out the intellisense
}
}
|
|
|
|
|
Sorry - this is not working.
|
|
|
|
|
This won't work, at least not in the way you want it... You are writing a numeric algorithm which should work upon different 'arithmetic' types and want to utilize arithmetic operators on T , right? This is not possible. There is no common base type for all 'numerics' which implements the appropriate operators (operators must be defined static on the type which implements them). Each CLR numeric type is implemented as a structure without common base (which is not supported for structures anyway), ergo you cannot restrict a type parameter in a generic to something which does not exists. The numeric types implement various interfaces, but these cannot implement operators since these must be static . So you are stuck, for the moment.
The only clean way (without CLR sourcery) is to define this non-existent common base:
public abstract class NumericBase<T> where T : struct {
public static NumericBase<T> operator + (NumericBase<T> pA, NumericBase<T> pB) {
return pA.Add(pB);
}
public static NumericBase<T> operator - (NumericBase<T> pA, NumericBase<T> pB) {
return pA.Sub(pB);
}
public abstract T Value { get; set; }
public abstract NumericBase<T> Add(NumericBase<T> pOther);
public abstract NumericBase<T> Sub(NumericBase<T> pOther);
}
Specialize for int :
public class NumericInt : NumericBase<int> {
public NumericInt() : this(0) {}
public NumericInt(int pValue) {
Value = pValue;
}
public override int Value { get; set; }
public override NumericBase<int> Add(NumericBase<int> pOther) {
return new NumericInt(Value + pOther.Value);
}
public override NumericBase<int> Sub(NumericBase<int> pOther) {
return new NumericInt(Value - pOther.Value);
}
}
And you can 'Foo ':
public class MySummator<T>
where T : struct {
public NumericBase<T> Result { get; set; }
public void Foo(NumericBase<T> pParameter) {
Result = Result + pParameter;
}
Clearly, you have a lot of overhead with virtual calls, so I would not use it that way in situations where performance is critical. On the other hand, you could use a bit of CLR sourcery here:
1. Write your algorithms using NumericBase<T> . You can run and test them and you are maintaing one generic common code base.
2. For real world usage System.Emit code, which is structurally identical to the generic NumericBase implementation, but uses concrete numeric types i.e. you replace NumericBase<int> with int and so on.
|
|
|
|
|
hi all,
normally in win app. or web app. I can take windows log on username with this code
"System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();"
but with windows service I can't do this, it takes like this:
"NT AUTHORITY\SYSTEM"
Howcan I do this with windows service??
thanks..
|
|
|
|
|
What account have you defined it to run under? Seems like Local System..
|
|
|
|
|
Go to your control panel-Admin Tools-Services and right click on the service, select properties -Log On tab ... have fun.
BTW,
System.Security.Principal.WindowsIdentity.GetCurrent().Name
is a string. If you call ToString on a string you look foolish. It is my personal crusade to get .NET developers to understand the concept of type.
|
|
|
|
|
Dumb answer of the day.
The service is running on a SERVER, the user name is what you started the service with. You nee dto get the username from the CLIENT and pass it to the service to record with the data.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi.
I now know how to ask my question I have been trying to find out, without confusing people.
All I would like to do is take a string of digits
"123456789"
and get its byte representation (hex) value.
The above, 123456789 entered into the calculator gives :
75BCD15
How would I do that in c#
There, finaly I think I have put what I am trying (desperatly) to do. sorry for all the posts and proberbly need to say sorry for being so confusing.
Thank you in advance
Steve
|
|
|
|
|
Parse it as an int (or long if you expect it to be that big)
Then do something like string.Format("{0:X}", value) or value.ToString("X")
|
|
|
|
|
Thank you.
Yes, that works, but the string will be 41 digits long and I do not what to use bignum or anything.
I am trying to get this function to work, it did in c++, I have modified it but it produces the wrong hex value and I do not know why.
static int decodeSerial(String serial, byte[] result, int ResLen)
{
int m;
int digit;
Array.Clear(result, 0, ResLen);
for (int i = 0; i < serial.Length; i++)
{
digit = serial[i] - '0';
m = 0;
do
{
digit += result[m] * 10;
result[m++] = (byte)(digit % 255);
digit >>= 8;
}
while (m < ResLen);
}
return 0;
}
Thank you
Steve
|
|
|
|
|
Here is an implementation, together with a series of tests.
I'm not claiming it is anywhere near the optimum, I mimicked your code for as much as I liked it.
Given full freedom I would return a byte array of variable length and never have to signal an overflow.
private static void runAllTests() {
test("1");
test("1000");
test("1000000");
test("1000000000");
long j=1;
for(long i=0; i<63; i++) {
test(j.ToString());
j+=j;
}
test("-1");
test("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
}
private static void test(string decimalDigits) {
try {
byte[] bytes=decodeSerial(decimalDigits, 10);
string s=decimalDigits+" = ";
foreach (byte b in bytes) s+=" "+((int)b).ToString("X2");
log(s);
} catch (Exception exc) {
log(exc.ToString());
}
}
private static byte[] decodeSerial(string decimalDigits, int numberOfBytes) {
byte[] bytes=new byte[numberOfBytes];
foreach (char c in decimalDigits) {
int digit="0123456789".IndexOf(c);
if (digit<0) throw new ArgumentException("bad char in decodeSerial("+decimalDigits+")");
for (int m=0; m< numberOfBytes; m++) {
digit+=bytes[m]*10;
bytes[m]=(byte)(digit&0xFF);
digit>>=8;
}
if (digit!=0) throw new OverflowException("decodeSerial("+decimalDigits+","+numberOfBytes+")");
}
return bytes;
}
FYI: log(string s) is any method that somehow outputs a string, could be as simple as Console.WriteLine(s);
|
|
|
|
|
Thank you very much.
Obviosly I take it you now understand what I meant originaly?
It was hard to explain lol.
thank you again,
Steve
|
|
|
|
|
stephen.darling wrote: It was hard to explain
Not really, however you made it rather hard to understand. I summarized it in a three-line comment (I just added an example).
|
|
|
|
|
Yes you did
Now I just need to figure out a function that will do the reverse, take the byte array and product the original string!
Do I dare ask for more help?
Steve
|
|
|
|
|
Yes you dare, and no you're not going to get a solution from me.
As far as I can tell, you haven't learned a single thing yet, all I saw you do was copy some C++ code,
then massage it blindly until it compiled well and ran all wrong. It is time to learn and understand what is really going on.
If you want more help, you write code yourself, debug it, and when stuck, post here, and we all will help you.
|
|
|
|
|
That is fair enough.
I am trying, and I have managed to learn some things from my books. I have managed to write my first little game that works well.
I dont what you to write the code for me, just point me in the right direction.
When it comes to this area it is not covered in my books, and because I dont know what search terms to use for this kind of programing (You see how hard it was to explain what I wanted to do) I find it difficult to find out.
I will try and plod on and lear, hopefully I didnt offend, or worse still come across as a lazy person not willing to learn on my own.
Also, if I may add, I tried my best to rewrite the code so that I didnt have to ask anyone in the first place, and I have been on with this problem for over 2 months before asking for help.
Thank you again for your time.
Steve
|
|
|
|
|
You're welcome.
Seems like we will meet again.
|
|
|
|
|
stephen.darling wrote: I have been on with this problem for over 2 months before asking for help
I admire your ambition, but maybe it's time for you to tackle simpler problems for the time being (before you burn out on problems beyond your capability). I saw nothing in your posts that indicated you were taking a logical approach to solving this problem. You didn't post that you had tried debugging it incrementally by outputting values as the algorithm ran. You didn't post that you tried with very simple values and made them more and more complex until you could reproduce the bug. You didn't describe the mathematical reasoning behind the algorithm (if you don't know how it's supposed to work, how are you supposed to make it work). You couldn't even describe what it is you were so desperately trying to do for the past 2 months (if you can't define the question, you can't solve the problem). Seriously, take a step back and take another look at the basics. Finding a mentor would be good; I mean a real, live person... not some random people on a forum. A teacher, a friend who is good at programming, or even a tutor at your school (if you are going to school) would be great. It would be a shame to waste all the passion you seem to have on reaching too high too quickly and experiencing only failure. You need to experience success if you are going to keep doing programming. Good luck.
|
|
|
|
|
Is that the new beginner assignment?
|
|
|
|