|
|
I have some classes I am writing that represent units of measurement, and I am trying to do this:
each unit (Inch, Foot, Yard) is essentially a decimal, but has different properties, eg. a foot has 12 inches, a yard has 3 "foots" and 36 inches, so on. Is there a way to say "this class is a decimal" in a way similar to inheretence?
I have tried using the same structure of a decimal, including all of the interfaces it inherets, but that was a god awful mess. Any ideas/suggestions?
______________________
Oh Hamburgers!
|
|
|
|
|
I would imagine that creating wrappers around the decimal struct would do the job. I did a similar thing for binary, octal and hexadecimal with an int here[^].
[Edit] I found this interesting so I've tried a little bit of coding - figured I may as well share it with you! Depending on what you're doing with this data, this may help. This (incomplete but working) struct wraps three ints and does some basic stuff. [/Edit]
public struct Imperial
{
public static readonly Imperial Empty = new Imperial();
public Imperial(int inches)
{
m_Yards = 0;
m_Feet = 0;
m_Inches = 0;
if (inches != 0)
{
m_Yards = inches / 36;
int remainder = inches % (m_Yards * 36);
if (remainder != 0)
{
m_Feet = remainder / 12;
if (m_Feet != 0)
m_Inches = remainder % (m_Feet * 12);
else
m_Inches = remainder;
}
}
}
public Imperial(int yards, int feet, int inches)
{
m_Yards = yards;
m_Feet = feet;
m_Inches = inches;
}
public static implicit operator Imperial(int value)
{
return new Imperial(value);
}
public static Imperial operator +(Imperial value1, Imperial value2)
{
return new Imperial(value1.GetTotalInches() + value2.GetTotalInches());
}
private int m_Yards;
private int m_Feet;
private int m_Inches;
public int Yards
{
get { return m_Yards; }
}
public int Feet
{
get { return m_Feet; }
}
public int Inches
{
get { return m_Inches; }
}
public int GetTotalInches()
{
return ((m_Yards * 36) + (m_Feet * 12) + m_Inches);
}
public static Imperial MillimetersToImperial(int millimeters)
{
return new Imperial((int)(millimeters * 0.0393700787));
}
public override string ToString()
{
return string.Format("{0} yards, {1} feet, {2} inches", m_Yards, m_Feet, m_Inches);
}
}
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)
|
|
|
|
|
Awesome. The method mentione din the article is the one i went with. I created a class that handles the basics like converting and constructors, then inheret it with the other classes. Works like a charm
______________________
Oh Hamburgers!
|
|
|
|
|
Cool - glad you found it useful. If you haven't already, comment and/or vote on the article!
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)
|
|
|
|
|
If you have a logical way of doing the conversion, you could add implicit conversions to and from a numerical data type. That way you can assign a numerical value to a measurement variable, and use a measurement value where a numerical value is required. (I'm not sure that Decimal is the right choise of numerical data type though. A double might be a better fit.)
However, it looks like you would have to specify the unit for the value to make sense as a measurement?
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Your message title is misleading.
You cannot "inherit" a struct .
"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
|
|
|
|
|
That was why it was in quotes.
______________________
Oh Hamburgers!
|
|
|
|
|
[EDIT]We have a workaround now, because the workflow would result in other errors [/EDIT]
I should 'simulate' a left-click or set the cursor to the position of the mouse, when right clicking (popup contextmenu) is occuring.
I tried mainly this:
mea = new MouseEventArgs(MouseButtons.Left, e.Clicks, e.X, e.Y, e.Delta);
OnMouseClick(mea);
but that doesn't work. I also tried to move my cursor position:
Cursor.Position = new Point(e.X, e.Y);
but nothing works. My guess is that the context menu is ahead of me although I should think that OnMouseDown would be before the contextmenu request?
How can I do this?
Many thanks !
modified on Thursday, October 23, 2008 10:03 AM
|
|
|
|
|
hi
use
int index = RichTextBox.GetCharIndexFromPoint(Point pt);
to get the position of the char under the mouse.
use
RichTextBox.SelectionStart = index;
RichTextBox.SelectionLength = 0; // maybe this line is not necessary
to move the cursor inside the textbox.
regards
|
|
|
|
|
i m using Visual studio 2005 with c#.here i am having a problem that i have a RadioButtonList which retrives data from sql datasource. it displays the Items correctly.Now when i try to display the SelectedItems on a label i get an error.kindly help me m stuck..m a student a really new to it.please help..thanx a lot...
DETAILS OF PROBLEM IS AS FOLLOWS:
the error i am getting on browser page is :-
"Server Error in '/Website_dtc' Application.
--------------------------------------------------------------------------------
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
"
the RadiobuttonList is databound as follows-
"RadioButtonList1.DataSource = ds.Tables["temp1"];
RadioButtonList1.DataTextField = "buses";
RadioButtonList1.DataValueField = "buses";
RadioButtonList1.DataBind();
"
the RadiobuttonList Item is selected as:-
" protected void Button1_Click(object sender, EventArgs e)
{
lbltemp.Text = RadioButtonList1.SelectedItem.Text;
}
"
Winning isn't everything,its the ONLY thing.
|
|
|
|
|
i am giving the exception....
((System.ComponentModel.ISupportInitialize)(this.performanceCounterMemoryPageRead)).EndInit();
plz...help me out
I am very interested with OOP & its modeling.
|
|
|
|
|
That's the line of code that throws the exception, but what exception are you getting?
|
|
|
|
|
|
hi to all....... i have made a text box on my web page.....i want that when a user enters the name of the place in that box then the words that sounds same should be displayed at that time........like if i want to enter haridwar then as it is same as hardwar then both these should be displayed...same as it happens in google searching.........how can i do it?????
|
|
|
|
|
Check out the SoundEx algorithm. You can write a method-extension that converts a string to it's soundex equivalent.
You will need a list of words though
|
|
|
|
|
Hi,
As i have a small doubt that why if i don't convert a integer to a string using ToString() method then also the output is coming. can any one try this.
Example Code:
int year = 1998;
string message = "Sandy was born in " + year.ToString();//Here if i remove the ToString() //also the code working properly
System.Console.WriteLine(message);
System.Console.ReadLine();
|
|
|
|
|
It still works because, the '+' operator has been overloaded to take various different combinations of types. In this case, the + operator is taking a string an an integer so is calling its overload for 2 objects: String.Concat(Object, Object)
The integer gets boxed as an object.
The Concat() method obviously then calls ToString() on the objects, concatenates the strings and returns the result. Which is then assigned to the 'message' variable.
You can confirm this by using ILDasm to look at the compiled IL.
(Because there is boxing occurring, this is not as efficient as calling ToString() on the integer yourself, although for most cases it won't actually make any difference)
Simon
|
|
|
|
|
Thanks for your response. But actually i did this small program using visual studio 2005. In that how can i use the ILDASM tool. It can be used only when i work with command prompt know. Can you please suggest me....
|
|
|
|
|
You can download the .NET Reflector and view the code as IL code.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Open up a visual studio command prompt (in the start menu, under visual studio tools).
Type the command ildasm
Click File->Open and select your program. This will show you a tree of the various classes and namespaces within your program. You will be able to browse to the method you are interested in and view the IL for that method.
It's quite an advanced tool, and much use if you are just starting out with .net. I used it to verify what method the '+' overload was calling.
Simon
|
|
|
|
|
Instead of using ILDASM directly I recommend using Reflector instead and having a GUI version.
http://www.red-gate.com/products/reflector/[^]
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall
|
|
|
|
|
Good morning,
Ive spent a few days trying to work this one out and was hoping that maybe someone else had experienced this problem.
I am receiving a Soap envelope through a TCPListener and TCPClient. That part works fine as I move the message to a stringbuilder.
The problem comes when trying to load the string back to an Xml document in c#. Heres a snippet of my code
public string InsertTagLocation(string OuterXML)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(OuterXML);
OuterXml contains the soap message which looks like this (its a long message so Ive cut it off. The problem lies with the AESNotification message=\". It appears this maybe an embedded xml or something so I wanted to know why LoadXml doesnt work with this string or what classes to use to deal with this problem?
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body><Notify><AesNotification message=\"<NorthboundNotificationEvent><AesTagLocation calcFloorId=\"6\" calcX=\"64.81\" calcY=\"13.93\" changedOn=\"1224749515732\"
The Soap message has an end tag for this message as well.
></NorthboundNotificationEvent>\"/></Notify></SOAP-ENV:Body></SOAP-ENV:Envelope>"
Any help, advice would be much appreciated
Cheers
Scott
|
|
|
|
|
Hi,
I'm now coding a tool to draw shapes like Visio.
I notice that in Visio there are some functions to combine 2 or more shapes like Union, Intersect, Combine...,
I don't know how to do it in C#. Currently I create a GraphicsPath, then assign some rect, ellipse to it, then create a Region and add the GraphicsPath to it.
I can then fill the region, but for the outline, I don't know how to draw.
And it's just similar to Union in Visio, what about other?
Thanks alot for your help.
|
|
|
|
|
I'm finding some documents about 3 layers model. Could you please share me some ? And please don't lead me to "Google"
|
|
|
|