|
Hello everyone,
I am designing a simple convertor, that will take an ammount and convert it to a different unit.
the uits that one can convert between are:
moles
millimoles
grams
milligrams
mirolitres
millilitres
The user enters an amout, selects its unit, and then selects a unit to convert to, and the new value is output onto the winform.
Now, should I write individual conversion functions, and maybe use a case to decide what unit we have, and then call the required converter.
Or, should I have an overloaded function and pass something in to let it decide how to convert?
Any other advice would be greatly appreciated.
Thank you
Steve
|
|
|
|
|
Hmmm...
You should probably have an enumeration that contains the supported units and have one method that takes the magnitude and two units.
But you could also provide separate methods and the above method could just be a switching station -- but with Dictionaries, not switch es.
private static Dictionary<FromUnit,Dictionary<ToUnit,delegate>> dic ... ;
dic [ Unit.Grams ] = ... ;
dic [ Unit.Grams ] [ MilliGrams ] = ConvertGramsToMilliGrams ;
...
return ( dic [ FromUnit ] [ ToUnit ] ( Magnitude ) ) ;
Actually, the populating of the Dictionary can be automated -- perhaps create an Attribute to decorate your methods:
[UnitConvertorAttribute ( From , To )]
public double ConvertGramsToMilliGrams ( double Magnitude , Unit From Unit To ) ...
|
|
|
|
|
stephen.darling wrote: Or, should I have an overloaded function and pass something
IMO, Function overload is fine.
Maintaining switch cases has its own overhead, especially for the developer who is going to maintain this code in the future.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
Visit the Hindi forum here.
|
|
|
|
|
Abhinav S wrote: IMO, Function overload is fine.
So what would I pass in to diffenciate the overloaded function?
For example, please consider the following punction
convert(float startValue, float outValue)
And I wanted to convert milliGrams to grams, how would this function know that?
would I need something like...
convert (float from, float to, UNIT_TYPE)
Where unit_type would be an enum, perhaps, to allow the correct units to be used. Or can you think of a better idea?
Thank you
Steve
|
|
|
|
|
Have you looked at applying the Strategy Pattern[^] or here[^].
[corrected links]
I know the language. I've read a book. - _Madmatt
modified on Sunday, October 10, 2010 5:20 PM
|
|
|
|
|
stephen.darling wrote: Now, should I write individual conversion functions, and maybe use a case to decide what unit we have, and then call the required converter.
The function is "always" the same; you take the base, and multiply it with a conversion-factor. The only thing you'd need to do is to match a factor to a unit, relative to it's base.
The only exception to this rule that I could find are temperatures
I are Troll
|
|
|
|
|
What about shoe sizes?
|
|
|
|
|
PIEBALDconsult wrote: What about shoe sizes?
Never considered those, but you're right
I are Troll
|
|
|
|
|
Is there a direct way to access a datatable in a collection directly by its name?
Currently, the only way I know how access a datatable is by using a foreach statement to find the table I wish to update/modify:
DataTableCollection tables = collection.Tables;<br />
<br />
foreach (DataTable tbl in tables)<br />
{<br />
string name = tbl.TableName;<br />
<br />
if (name == "name")<br />
{<br />
DataRow myNewRow;<br />
myNewRow = tbl.NewRow();<br />
<br />
code.....<br />
<br />
tbl.Rows.Add(myNewRow); <br />
<br />
}<br />
}
Thanks for any help
|
|
|
|
|
Umm... perhaps DataTableCollection.Item[^]
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
Thanks for the answer. I've not used datatable collections before and was spinning my wheels. So I can replace the foreach loop with:
DataTable table = tables["name"];
|
|
|
|
|
Yep. Should work for all collections!
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
ds = DB.FillDataSet(cmd, "StRecord");
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
foreach (DataColumn col in ds.Tables[0].Columns )
{
}
From: <ahref="http: www.programlive.tk"="">http://www.programlive.tk
|
|
|
|
|
I have written a program in C# using inpout32.dll which, when you press a button, it sets all the data pins on the paralell port to high by setting the address as 888 and the decimal value 255 to that address. I have heard that when a pin on the port is a high, it is outputting a voltage of 5v, but it shows about 1.5v constantly, not changing, while none of the other data ports do anything. I have a feeling this is not working because I either have the wrong address, or I am setting the wrong value.
In my BIOS, it says that the address is 378h, which I know if 888 in decimal, which is why I am setting it as the address. I also have the mode set to EPP. Can somebody tell me what I am doing wrong?
|
|
|
|
|
I don't know. I've used that in one small app. My code is:
[
System.Runtime.InteropServices.DllImportAttribute
(
"INPOUT32"
,
SetLastError=true
,
EntryPoint="Out32"
)
]
private static extern void
API_Out32
(
System.Int32 Port
,
System.Int32 Data
) ;
public static void
WriteToPort
(
System.UInt16 Port
,
System.Byte Data
)
{
API_Out32
(
Port
,
Data
) ;
return ;
}
PIEBALD.Lib.LibApi.WriteToPort ( 0x378 , (byte) this.tvStati.Tag ) ;
|
|
|
|
|
hi, i want to store 2 strings which are named "player" and "score" ,
how do i create a table for this and i want to know how to display this information ...
thanks for all your help
|
|
|
|
|
XML
<Game>
<Players>
<Player Name="Joshua" Score="42" />
</Players>
</Game>
How you display it is up to you.
|
|
|
|
|
Won't that give InsaneDataException at name?
|
|
|
|
|
What? I said Joshua, not HAL.
|
|
|
|
|
You need to get some books on databases and start reading...
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
Visit the Hindi forum here.
|
|
|
|
|
Store them where? In memory, database, text file, ...
Display them how? On screen, console, printer, ...
Try and describe your problem in clearer terms.
Just say 'NO' to evaluated arguments for diadic functions! Ash
|
|
|
|
|
For loading a file that was originally written by an application written in C++, I have been declaring each part of the file as a struct, and converting the data into a struct using this function:
public static TStruct GetStruct<TStruct>(byte[] data,Type t)
where TStruct : new()
{
int structSize = Marshal.SizeOf(typeof(TStruct));
TStruct outstruct = new TStruct();
GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
outstruct = (TStruct)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),t);
handle.Free();
return outstruct;
}
This works fine for structs that do not contain arrays or that contain only arrays that can be defined using fixed (eg fixed int someVariable[10]; works fine) ,but I run into a problem when I want to do this to a struct such as:
struct Foobar
{
int count;
customStructType someStruct[count];
}
I've tried using [MarshalAs(UnmanagedType.<tried all these>, <parameters specified type takes here>)] but it either requires me to specify a fixed number upfront or causes my GetStruct method to give me an error that it "could not meaningfully obtain the size of the struct". LPArray allowed me to specify a particular element in the struct that acts as the number of elements in the array but that gave me the aforementioned lack of meaningful size error. Is there any way to make this work?
|
|
|
|
|
Nasty one. AFAIK .NET does not allow any direct coding that will create a structure whose length is not fixed at compile time - and I can understand why not.
I haven't tried it, but this[^] appears to address the problem. In a suitably nasty way, of course...
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
Hi everyone..
I am getting the above error in one of the application on this portal.
The application is:
http://www.codeproject.com/KB/IP/CSNetworkSniffer.aspx?msg=3625563#xx3625563xx
I am really new to socket programming so i need your help
Thank you and any help would be appreciated
|
|
|
|
|
Try posting your question in the forum at the end of the article, and then the author should get notified.
Just say 'NO' to evaluated arguments for diadic functions! Ash
|
|
|
|