|
Hi guys,
Quick question:
I got this class:
public class PresaHidraulica<br />
{
.....
public System.Collections.ArrayList listaRevizii;
}
and i want to overload the operator + to add an object of another class(Revizie) in this ArrayList from this class.
I tried :
public static PresaHidraulica operator +(Revizie r1)<br />
{<br />
}
And i get the error : The parameter of a unary operator must be the containing type
Any suggestions please?
Regards,
Alex
“Be the change you want to see in the world.”
|
|
|
|
|
That means that your parameter r1 is of wrong type: Revizie.
When you overload operator + you must pass in parameter PresaHidraulica
public static PresaHidraulica operator +(PresaHidraulica ph)
{
}
Maybe you should create a method called AddRevizie
public void AddRevizie(Revizie r1)
{
listaRevizii.Add(r1);
}
|
|
|
|
|
The first parameter must be the containing type. In this instance, you should change the operator method signature to public static PresaHidraulica operator +(PresaHidraulica self, Revizie r1)
|
|
|
|
|
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
|
|
|
|