|
Thanks a lot. It worked just fine
here it is:
public static PresaHidraulica operator +(PresaHidraulica p, Revizie r)<br />
{<br />
p.ListaRevizii.Add(r);<br />
return p;<br />
}
Regards,
Alex
“Be the change you want to see in the world.”
|
|
|
|
|
You're very welcome. And thank you for working the code out - too many people here expect to be spoon-fed
|
|
|
|
|
Hi,
this is a bad idea.
When you have integers a, b, c and execute c = a + b; do you expect a or b to change?
That is what your code is doing!
You should not change the value of the operands,
Overloaded operators are not the right solution, create an Add() method instead.
|
|
|
|
|
I totally agree. What you are trying to do is also adding apples and oranges:
foo = 3.1415 + "Hello";
Use an Add method, rather than trying to twist the compiler into effectively allowing implicit casts between unrelated objects.
That didn't make as much sense when I read it back. Try this:
...
int a, c;
string b;
...
c = a + b;
...
You would not expect that to compile, would you?
So why would you expect this to compile?
class AClass
{
private static AClass operator + (BClass b)
...
}
class BClass
{
...
}
...
AClass a, c;
BClass b;
...
c = a + b;
...
|
|
|
|
|
I agree to a point. There are situations where it could be acceptable (the OP's may be one).
To continue your adding apples and oranges analogy, imagine a class FruitBasket that has a List of Apples. Adding a new Apple to the basket via an overloaded + binary operator could make sense. I prefer exposing the list of Apples as a property so it's Add method can be called directly or creating an AddApple method, but it's not 'bad' IMO.
public class FruitBasket
{
private List<Apple> _Apples;
public FruitBasket()
{
_Apples = new List<Apple>();
}
public static FruitBasket operator +(FruitBasket basket, Apple apple)
{
basket._Apples.Add(apple);
return basket;
}
}
public class Apple
{
public Apple(string name)
{
Name = name;
}
public string Name
{
get;
set;
}
}
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) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
I see what you are saying, that there may be instances where it could be acceptable.
I am not sure though, that it adds anything usefull over using an Add method - at least you expect
fruitBasket.Add(apple); to change fruitBasket without looking at the implementation. I wouldn't expect
foo = fruitbasket + apple; to result in a change in foo and fruitBasket at first view. Surely, good practice would say "don't do it"?
Just a thought, not an arguement...
|
|
|
|
|
i'm new to programming n networking, i like to know more about how bittorrent working. so, recently i start building torrent retriever. i come across this site http://dotnet.org.za/weareu/archive/2007/04/12/decoding-bencoded-streams-in-c.aspx with the following code to decode torrent file. In the (public static object BDecode(BinaryReader reader)). there have four different type return. if i called this method, how to retrieve the four different variable. thanks.
public class BEncoding
{
public static object BDecode(string filePath)
{
byte[] data = File.ReadAllBytes(filePath);
return BDecode(data);
}
public static object BDecode(Stream stream)
{
if (!stream.CanSeek)
{
throw new Exception("Your stream needs to support seeking for this algorithm");
}
BinaryReader reader = new BinaryReader(stream, System.Text.Encoding.ASCII);
return BDecode(reader);
}
public static object BDecode(byte[] data)
{
return BDecode(new MemoryStream(data));
}
public static object BDecode(BinaryReader reader)
{
char ch = reader.ReadChar();
Int64 ires = 0;
if (ch == 'd')
{
ch = (char)reader.PeekChar();
Hashtable hash = new Hashtable();
while (ch != 'e')
{
object key = BDecode(reader);
hash[key] = BDecode(reader);
ch = (char)reader.PeekChar();
}
return hash;
}
else if (ch == 'l')
{
ch = (char)reader.PeekChar();
ArrayList list = new ArrayList();
while (ch != 'e')
{
list.Add(BDecode(reader));
ch = (char)reader.PeekChar();
}
return list;
}
else if (ch == 'i')
{
ch = reader.ReadChar();
string number = "";
while (ch != 'e')
{
number += ch;
ch = reader.ReadChar();
}
return Int64.Parse(number);
}
else if (Int64.TryParse(ch.ToString(), out ires))
{
string number = "";
while (Int64.TryParse(ch.ToString(), out ires))
{
number += ch;
ch = reader.ReadChar();
}
string line = "";
for (int i = 0; i < Int64.Parse(number); i++)
{
line += reader.ReadChar();
}
return line;
}
throw (new Exception("Torrent Error"));
}
}
|
|
|
|
|
This is really badly formed code. You should rewrite it as a more type-safe method. Your original question however: check the return type. Something like this:
object result = BDecode(...);
string resultAsAString = "";
HashTable resultAsHashTable = null;
ArrayList resultAsArrayList = null;
if(result != null)
{
if(result is string)
resultAsAString = result.ToString();
else if(result is HashTable)
resultAsHashTable = result as HashTable;
else if(result is ArrayList)
resultAsArrayList = result as ArrayList;
}
This is just a kludge however. Your original method is still badly in need of a refactor
|
|
|
|
|
thanks for your valuable answer. i get it working.
|
|
|
|
|
nay one can tell me how we use joining more than one table in asp.net by inner join and outer joi
please send website name to find out
rizvan sivally
|
|
|
|
|
sheemap wrote: please send website name to find out
google[^].
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
that site is nnot accessibe please send again
rizvan sivally
|
|
|
|
|
|
What, have you lost the internet?? How is google not accessible.
Here is a nice list of EVERY google site (that I was able to find, using google)
google.de
google.com
google.at
google.pl
google.fr
google.nl
google.it
google.com.tr
google.es
google.ch
google.be
google.gr
google.com.br
google.lu
google.fi
google.pt
google.hu
google.hr
google.bg
google.com.mx
google.si
google.sk
google.ro
google.ca
google.co.uk
google.cl
google.com.ar
google.se
google.cz
google.dk
google.co.th
google.com.co
google.lt
google.co.id
google.co.in
google.co.il
google.com.eg
google.cn
google.co.ve
google.ru
google.co.jp
google.com.pe
google.com.au
google.co.ma
google.co.za
google.com.ph
google.com.sa
google.ie
google.co.kr
google.no
google.com.ec
google.com.vn
google.lv
google.com.mt
google.com.uy
google.ae
google.ba
google.co.nz
google.com.ua
google.co.cr
google.ee
google.com.do
google.com.tw
google.com.hk
google.com.my
google.com.sv
google.com.pr
google.lk
google.com.gt
google.com.bd
google.com.pk
google.is
google.li
google.com.bh
google.com.ni
google.com.py
google.com.ng
google.com.bo
google.co.ke
google.hn
google.com.sg
google.mu
google.ci
google.jo
google.nu
google.com.jm
google.com.ly
google.co.yu
google.tt
google.com.kh
google.ge
google.com.na
google.com.et
google.sm
google.cd
google.gm
google.com.qa
google.dj
google.com.cu
google.com.pa
google.gp
google.az
google.as
google.pl
google.mn
google.ht
google.md
google.am
google.sn
google.je
googlee.com
google.com.bn
google.com.ai
google.co.zm
google.ma
google.rw
google.co.ug
google.com.vc
googlenews.de
google.com
google.at
google.com.gi
google.to
google.com.om
google.kz
google.co.uz
if every single one is inaccessible, then I will do your research for you. Not before.
|
|
|
|
|
You have way too much time on your hands
|
|
|
|
|
I thought you were a web developer? surely that kind of stuff is basic to someone of your job description? thou maybe your 'web' definition is not what ours is. your 'web' doesnt even have google!!!
Oh sorry... I thought you were asking a question... but you just want a website name... how about 'CodeProject'? Now what do i find out?
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
|
Hi,
I have a small problem.I have created a form using OpenGL controls.My task is to set an image file as background to the form.I am able to set the image when the form is loading but my image is flickering on the form.I dunnow what to do.Please have a look at my code below and do tel me what is the mistake there.
code :
public Form1()
{
sName = "C:\\Winter.jpg";
InitializeComponent();
simpleOpenGlControl1.InitializeContexts();
nFstFime = 0;
Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
simpleOpenGlControl1.BackgroundImage = Image.FromFile(sName);
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glLoadIdentity();
Glu.gluOrtho2D(0, simpleOpenGlControl1.Width, 0, simpleOpenGlControl1.Height);
}
private void simpleOpenGlControl1_Paint_1(object sender, PaintEventArgs e)
{
Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
if (nFstFime == 0)
{
b = new Bitmap(simpleOpenGlControl1.Width, simpleOpenGlControl1.Height);
nFstFime++;
}
b = (Bitmap)Image.FromFile(sName);
simpleOpenGlControl1.BackgroundImage = b;
Graphics g = e.Graphics;
g.DrawImage(Image.FromFile(sName), new Rectangle(0, 0, simpleOpenGlControl1.Width, simpleOpenGlControl1.Height));
}
Thnx,
Raesa
|
|
|
|
|
can some body tell me how i can save the string in to PDF file using C# .Net???
WANTED wasim khan(Killed 50 Innocent Buggs, Distroyed 200 Exception, make 5 Project Hostage) any Compnay Hire him will pay 30,000. Best place where u can get him is Sorcim Technologies Murre Road RWP
|
|
|
|
|
|
thnx
WANTED wasim khan(Killed 50 Innocent Buggs, Distroyed 200 Exception, make 5 Project Hostage) any Compnay Hire him will pay 30,000. Best place where u can get him is Sorcim Technologies Murre Road RWP
|
|
|
|
|
You can also see iTextSharp who is an adaptation of a java library who's very effective
|
|
|
|
|
Hi there,
creating a project with multiple namespaces/ classes. Basically I have two threads that each have own windows (GUIs) -> don't ask why... know that this is no criteria to launch seperate thread, need it that way anyways. Both threads use a function library, that accesses hardware (driving some motors, reading out sensors, sending commands, receiving replies...). Communication to that hardware via network.
I need to make sure that my static function library is locked if one of the threads executes read or write operations. I need the locking to be handled within this function library. So my experience up to now is that a semaphore doesn't do the job because if the semaphore is part of the function library it always is owned (semaphore "ignores" that access to my funclib is done by different threads, state of my semaphore always "owned" (obviously by my funclib) if once "taken" -> the funclib takes its implemented semaphore, not the accessing thread).
So what I need is a different method to lock my funclib or a method to identify the accessing threads in my funclib and add that info to my semaphore handling... or any other ideas?
|
|
|
|
|
I don't know the details of your implementation, but I guess you're using Windows Forms.
Remember that having more than one open Form open doesn't mean they are running on different threads.
So I will give it for granted that you are running your communications code in two separate threads you created.
I guess your function library exposes functions to start the communications and callback events to signal the end of communications or errors (timeout, wrong response, etc.). You should use lock in your library's functions (or in calls to those functions, if you don't want to modify the library) in order to achieve a first level of thread-safety, but this won't solve the problem of not having the two threads trying to communicate to the same device at the same time if the communication is in progress between calls to the various library functions (i.e. if it is asynchronous). If the communication is synchrnonous, it will be enough.
If the communication is asynchronous, using thread locking to avoid concurrent access to the devices is usually not a good practice, since communications can take a long time (especially when checking timeouts), and you would keep you threads locked during all that time, thus losing all the benefits of async comms.
In that case, you should implement a (thread-safe) queue of requests coming from the two threads. Each request will be satisfied when the instrument is "free" and the result posted in another (thread-safe) queue or sent to the calling thread by means of events or similar. All this can be implemented in a "controller" class, which will stand between your calling threads and your library. The controller might also use a simple round-robin system (with priority management, if you need it) to manage access to the library.
If you want to be more OO, you can also create a class for each type of instrument and create instances for every physical instrument. These classes will expose properties and functions to read and write the desired values to/from the devices, and will stand between your calling threads and the controller class.
Hope this can be of some help.
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
rpm8200 wrote: I need to make sure that my static function library is locked if one of the threads executes read or write operations
It's my understanding that static methods are always thread safe. That if thread 1 is executing Read then thread 2 will have to wait if it calls Read.
If you want thread 2 to wait if it calls Write while thread 1 is executing Read, then simply create an object to lock on:
private static readonly object lockme = new object() ;
Then in each of your methods:
lock ( lockme ) { ... }
I don't think I've ever had to do that in static methods, but I expect it will work.
|
|
|
|