|
Hello to all, now I expose my problem:
I'm developing a program with Visual C# express to calculate the ratio of my radio-controlled model!
How do I enter the following input data:
Number of teeth of spur gear (int), internal ratio (float), and ratio that you want to get (float).
I need as output the number of teeth of the pinion to use. The pinion is obtained with the following formula:
Pinion = (Spur Gear * Ratio) / Internal Ratio
My problem is that I don' t know how to operate the data conversions, because obviously I can' t write:
Int SpurGear = textbox1.Text;
Float Ratio = textbox2.Text;
Float InternalRatio= textbox3.Text;
Float Pinion = (SpurGear * Ratio) / InternalRatio;
label1.text= Pinion;
Etc Etc
Someone can help me?
|
|
|
|
|
Try:
Int SpurGear = int.Parse(textbox1.Text);
Float Ratio = Float.Parse(textbox2.Text);
Float InternalRatio= Float.Parse(textbox3.Text);
Float Pinion = ((Float) SpurGear * Ratio) / InternalRatio;
label1.text= Pinion.ToString();
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
Ok thank you very much, now I try it!
|
|
|
|
|
Most of the value types have Parse method for data conversion. Don't forget to have a look at TryParse methods too
|
|
|
|
|
Hi,
a TextBox contains text, so Text returns a string, not a number.
converting strings to some numeric type is called parsing, use the Parse or TryParse method of the target type, e.g. double.TryParse(...) , see documentation.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
Hello there I am hoping that someone might be able to help me optimize my code for an XNA Program I have made, basically I have made my own custom interface in XNA, now my interface works pretty similar to the ways that windows does, however if you nest too many controls inside each other, then the game really starts to run at a slow rate (fps wise), as it obviously the draw method for the controls are taking too long.
I have tried doing a background draw (using an event via BeginInvoke and EndInvoke), but I get lots of flashing and sometimes it even crashes, as it tries to access the Sprite Batch sometimes when it is still is use
Is there another way to optimize my code that will allow me to get my performance back again??
Here is a sample of the draw code for a container object:
if (m_pTexture != null)
{
graphicsManager.Begin();
if (bSetupClientArea)
graphicsManager.SetupClientRect(TranslatedArea);
if (Image_States == 0 || Image_States == 1)
graphicsManager.Draw(m_pTexture, TranslatedArea, new Color(255, 255, 255, (byte)Alpha));
else
{
int nSubHeight = m_pTexture.Height / Image_States;
Rectangle src = new Rectangle(0, (int)Current_State * nSubHeight, m_pTexture.Width, nSubHeight);
graphicsManager.Draw(m_pTexture, TranslatedArea, src, new Color(255, 255, 255, (byte)Alpha));
}
graphicsManager.End();
DrawBorder(graphicsManager);
}
for (int i = 0; i < m_pComponents.Count; ++i)
{
Component component = m_pComponents[i];
if (component.Visible)
component.Draw(gameTime, graphicsManager, true);
}
As you can see each container has its own Rectangular Area, and it will basically use the graphics manager (which contains the sprite batch object for my XNA Program) to setup it's clip area and draw all of the contents to it, of course using this method means every container has to use begin and end from the graphics manager, to make sure that the objects are drawn correctly
Does calling the SpriteBatch.Begin and End function numerous times cause it to slow down considerably??
Or do you think something else is causing it to slow down??
|
|
|
|
|
jdneul wrote: Does calling the SpriteBatch.Begin and End function numerous times cause it to slow down considerably?
Yes, don't do that. SpriteBatch is a batch for a reason, graphics calls and especially context changes are slow, so it tries to accumulate as many as possible to do many of them at once.
Sometimes it may be required to restart your batch, but I've found that can get quite slow if you do it often.
|
|
|
|
|
i am using the GraphicsDevice.ScissorRectangle to clip all controls, is there another way (code wise) to do this that does not require havin a call to the spritbatch Begin and End just to make sure controls are drawn correctly??
|
|
|
|
|
So you have to clip every control to an other rectangle?
|
|
|
|
|
i can get away with normal controls, because they use textures, you can easily alter the destination and the src to affect the overall look of the interface, but the label controls have strings which are drawn to them and i don't know no easy way to manipulate the DrawSting so that it only draws the correct part of the string depending on it's location in the parent
|
|
|
|
|
Oh drawstring.. It's kinda evil really
I just use my own bitmap fonts, draw them as sprites, like in the old days of XNA 1 - it's just so much more flexible
If you really want to/have to use DrawString, then, I'm sorry but I don't know any clean workarounds
Also, there are usually very few (in any) replies here to XNA questions, it might be better to ask on Ziggyware or something like that
|
|
|
|
|
yeah i was thinking of doing that, thanks for your help again lol, (i presume you mean by using System.Drawing??)
if not can I have some sample code please.
|
|
|
|
|
Hm no, why would you mix XNA with System.Drawing?
I mean you'd have a big texture with all your glyphs on it, and you'd map chars to coordinates on that texture, and keep a list of widths (if it's a variable width font) to update the X coordinate on the screen with
Just use a SpriteBatch for that, it'll be fast because it's all the same texture
|
|
|
|
|
Hi,
In my application i have to print receipt using Dot Matrix Printer(EPSION LQ-300+II). My receipt have preprinted format, in this format i have to send datas using C# code.
My papers size is:Height:10 Inch and Width 9.5 inch. The paper is regular paper.
The problem is in first page datas are printed in required location but in next page it is printed in other location. So, How to set paper size in Crystal Report using C# application for this case: I have set like this:
Reports.frmCrViewer.PaperSize = PaperSize.Paper10x14;
Reports.frmCrViewer.PaperOrientiation = PaperOrientation.DefaultPaperOrientation;
this syntax is correct but the paper size Paper10*14 didn't matched my requirement. So, what should i have to do to print in paper having size 10*9.5 inch.
Please Help Me
|
|
|
|
|
I have a StreamWriter used to create log files and it works fine the first time but if I try to use it again writes fail because the file is not open to write to.
This is called to create a new log file:
FileStream fs = new FileStream(timestr, FileMode.CreateNew);
ScriptData.LogFile = new StreamWriter(fs);
ScriptData.LogFile.WriteLine("Logile created {0:yyyy'-'MM'-'dd'T'HHmmss}.log", DateTime.Now);
This is called to close a file:
ScriptData.LogFile.WriteLine("Script {0} completed.", ScriptData.LogName);
ScriptData.LogFile.Close();
ScriptData.LogFile.Dispose();
ScriptData persists outside the scope of these calls.
Any ideas? All I want to do is be able to write ASCII text to log files.
Thanks.
|
|
|
|
|
Where and how do you destroy the filestream object fs?
The problem is, if you close the underlying filestram, you also close the file.
Greetings
Covean
|
|
|
|
|
The StreamWriter is a member of Scriptdata which is persistent.
|
|
|
|
|
Take a look at your code!
<pre>FileStream fs = new FileStream(timestr, FileMode.CreateNew);
ScriptData.LogFile = new StreamWriter(fs);</pre>
Now if you close fs then your StreamWriter can be a static item from hell, its closed cause you closed the underlying filestream!
StreamWriter is just a class to have a simpler way to transfer data from and to a file.
The FileStream itself "represents" the file!
Greetings
Covean
|
|
|
|
|
Elaine - rather than rolling your own logging solution, you should really look into using something like log4net[^]. We use it all the time now.
"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
|
|
|
|
|
Pete,
that looks good but I'm not sure it would do what I need which is basically to capture the input from a serial port, write it to a file and occasionally insert comments along the way.
Log4net seems to be about logging for the application whereas I am logging the embedded target being controlled.
|
|
|
|
|
That wouldn't be a problem. Instead of calling your log code, you can call Log.Write on the log4net log manager. You might want to log to the messagequeue and log from there, for instance.
"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
|
|
|
|
|
Hi all,
i want to create an application(c#) through which i need to send and receive sms using gsm modem or phone.so i need some basic idea regarding this. please help
|
|
|
|
|
This would be the best place to learn about basics of SMS.
Developers home[^]
you can create your own framework, after reading the basics...
All the best...
Have a Happy Coding.....
|
|
|
|
|
You can use at commands for interacting with modem.
|
|
|
|
|
Hi,
This very long post consists of a relatively short question and a lot of background information. Please read at least the question before deciding this isn't worth your time!
Question
Does anyone know if it's feasible to inject code into a precompiled object at runtime? I suppose it would be quite tricky, but I am not sure since the .NET Framework has surprised me many times by providing easy ways to do previously difficult things!
What I want is to have a bunch of objects implement an interface by generating this implementation at run-time. Existing instances (at the time of generating the implementation) of the type would have to be replaced by the generated type, and still be "seen" as the same type (I don't know much about how type checking is implemented in the runtime) by all the other precompiled code.
Is this possible? If so, and if anyone knows how to do it, or more likely where I can find the information I need to figure out how to do it, I'd very much appreciate the help.
Background
If anyone thinks what I am trying to do seems very odd and find themselves thinking there must be better ways to achieve the same, here's that background. The following doesn't really add to my question, although it's possible of course that there is some other way of achieving the result I desire, so I think it's worth including it.
I'm toying around with making a fairly general data access framework intended for rapid prototyping purposes. The emphasis is therefore on minimal programming and configuration (for the user of the framework), and sensible default behaviors that would work in most if not all situations but need not be optimal in terms of performance or scalability. However, I find myself thinking if I can achieve this and also get good performance, that would be pretty awesome and make the framework into something far more valuable than just a prototyping tool.
I want to completely encapsulate the storage system so it's essentially reduced to exposing just two operations: Load and Save. (Some other types would also be exposed, such as attributes to override default behaviors, a Selection type to specify a subset of objects to load at once, interfaces the entity objects may implement to optimize performance by letting the storage system ask the object if it has changed since it was loaded, and more. But only two operations: Load and Save.) When saving and loading objects I want to do so based on fields, not properties, since the fields always contain the true state of the object. The user of the framework would therefore not need to necessarily expose everything as properties that are read-write, and that's important to me, since using read-only properties is a very good way to encapsulate and protect state in many cases.
I know this can be achieved by using reflection to read and write the fields. However, I find myself thinking that if I could somehow inject change the type that is being loaded or saved at run-time so it implements a simple interface, I could instead use reflection to generate code that relies on the interface to get and set private fields of the object. This would lead to considerable overhead the first time a type is saved or loaded, but then be very fast since the generated and compiled code would simply be reused.
Here is some very very incomplete code to illustrate the idea:
public class Toto
{
int n;
string s;
Toto parent;
}
public class Storage
{
static public ObjectManagerFactory ObjectManagerFactory { get; set; }
Dictionary<Type, IObjectManager> objectManagers;
public List<T> Load<T>(Selection selection)
{
IObjectManager m;
Type t = typeof(T);
if (!objectManagers.TryGetValue(t, out m))
{
m = ObjectManagerFactory.CreateManager(t);
objectManagers[t] = m;
}
return (T)m.Load(selection);
}
public void Save(object data) { ... }
}
Now imagine my user wants to save an instance of Toto. He would have to first have instantiated the Storage class (but this is obviously intended to have the same lifetime as the application, since otherwise caching the object managers would not have as much effect), but then the code would just be something like this:
Storage.Save(toto);
What I would like to do, but doubt is actually possible, is to modify the Toto type so it becomes something like this:
public class Toto : IFieldAccessible
{
int n;
string s;
Toto parent;
object[] IFieldAccessible GetFieldValues()
{
return new object[] { n, s, parent };
}
void IFieldAccessible SetFieldValues(object[] values)
{
n = (int)values[0];
s = (string)values[1];
parent = (Toto)values[2];
}
}
Since the code that would use the two interface methods is also dynamically generated there should be no problem with having to rely on the index in the object arrays to know what field belongs to what value.
The trouble is I don't know if this is even possible (I doubt it!) and much less how it could be done if it is. Also, I don't think it can be done by hooking into the build process, because the types that may be loaded or saved aren't necessarily known at compile-time.
|
|
|
|
|