|
Hello,
I'm new to c# but I have lots of programing experience, mostly with Delphi, and C.
so what I am trying to do is simple
take this string "1/32" ..
look for the "/" .. everything before that is the numerator, everything after is the denominator..
so I can't seem to get this simple code to work! What on earth am I doing wrong? Any help understand what is causing the out of bounds exception for the denominator would be most appreciated.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ScaleCalc.scaletoolsutility
{
class testScaleCalcUtils1
{
static void Main()
{
string stw;
string st1 = "1/32";
stw = st1.Substring(1,st1.length -1);
stw = st1.Substring(2, 3);
Console.WriteLine(stw);
}
}
}
modified 18-Mar-15 23:04pm.
|
|
|
|
|
Indices in strings are zero-based.
So, when you write stw = st1.SubString(1, st1.Length - 1); , you get "/32" (this line compiles well for me, btw).
But, when you write stw = st1.SubString(2, 3); , you are trying to get 3 characters from st1 starting from "3", which obviously leads you towards the end of the string.
You may use String.Split() instead:
string st1 = "1/32";
string[] parts = st1.Split(new char[] { '/' });
int numerator, denominator;
if (!int.TryParse(parts[0], out numerator)) {
}
if (!int.TryParse(parts[1], out denominator)) {
}
There are two kinds of people in the world: those who can extrapolate from incomplete data.
|
|
|
|
|
Great info - thanks so much!
|
|
|
|
|
You're welcome
There are two kinds of people in the world: those who can extrapolate from incomplete data.
|
|
|
|
|
I wish to be able to detect when a device is connected to a serial port, and if I have many serial port devices, I wish to detect the last device connected to a serial port.
Can someone please tell me how can I do this?
Any sample code will be greatly appreciated.
Thanks.
|
|
|
|
|
|
|
If that location is correct, then the latitude and longitude attributes are using seconds instead of degrees:
- 52° 42′ 1.00″ N -
(((52 × 60) + 42) × 60) + 1 == 189721 - 1° 18′ 25.00″ W -
(((1 × 60) + 18) × 60) + 25 == 4705
Divide both latitude and longitude by 3600, and you should be able to use them with the DbGeography.FromText method.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi,
do you know of a free online course to learn C# to an advanced level (not just a beginners course) ?
There are some great examples (like MVC tutorials) on this site but I was looking to improve my general C#. I know a fair bit of C# but would love to be able to use it better.
|
|
|
|
|
Yes, there is online course to become a C# programmer, to an expert level. Also, it is free! It is on the Microsoft Developer Network (AKA MSDN), and you can start your tutorial guide, from here... https://msdn.microsoft.com/en-us/library/kx37x362.aspx[^]. You can learn the basics about C#, keep applying them in your applications, convert basic real-world systems into .NET applications using C#, you will stumble upon a lot of problems while doing so. You will be able to tackle them, by getting help from MSDN or asking a question about that particular problem on CodeProject...
You cannot just cram a book and learn a programming language, you must use it in real-life too.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
thank you for your reply !!
I see the link you gave. Its good but a bit bullet point- there is an example and a few paragraphs underneath but not as interactive as i would have liked.
|
|
|
|
|
Programmers do not get attracted by the graphics or the images. They love the code, and the structure of the code. I have been using MSDN for many years, and I have always love the way they teach their APIs and languages.
If you have a better scheme of user-interface or user-experience, please do share with them. They would love your suggestions and feedback. It would also help others like you, to better like MSDN.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
its code that I am after and not graphics. its just a bit hard to make something out of MSDN. I would love to build a bigger and bigger example where you could start off at the basics of classes and cover all the topics in between (arrays, interfaces, xml etc etc) and keep adding to the same example - you could see it working and play around with it. MSDN seems a bit of a reference point. Thanks for the help !
|
|
|
|
|
I agree that MSDN is pretty much like reading an encyclopedia. Lots of accurate, detailed information, but drier than stale beef jerky. Sounds to me like you're looking for something similar to what I'm after. I have been considering taking a C# class at our local community college, but I'd prefer something I can do at home on my free time (and schedule). I've already read and experimented with most of the projects in a book called Head First C#. That's pretty much how I taught myself the basics. I am now using Professional C# 2005 (another book) as my go-to reference for my on-the-job projects here at FlightSafety (Broken Arrow, OK). If you find a web site or another book that's a more advanced version of Head First C#, please let me know! Thanks.
|
|
|
|
|
I think Head Frist C# is a great book for learning C#. I was going to get it but c# 6.0 is coming out and maybe it will do a new book for it. They should make a Pro or advanced version. I got books like 'Pro c# and the .NET framework' and it was interesting but even after finishing it I would not really be confident in writing a solution from scratch. Some of the tutorials here on codeproject are more tuned in to what I need than what Microsoft came up with
|
|
|
|
|
I agree. Well, then... I guess we will just have to keep our fingers crossed (or keep looking).
|
|
|
|
|
hey do you have the book c# 2013 step by step by Microsoft ? let me know if you want it
|
|
|
|
|
Hi there! Looks like a good learning book. I had a look at the free PDF of Chapter 1 and 10, but I found it to be a little too Windows 8 and Visual Studio 2013 centric. We use Windows 7 and VS 2012 here at work. Does the rest of the book read more as a progressive tutorial, or more like a reference book? How much do want for it?
|
|
|
|
|
|
That's only a sample! Nobody can learn all about C# by reading only chapters 1 and 10. That's why I asked about the rest of the book.
|
|
|
|
|
|
What is a PM???
|
|
|
|
|
How do you check PMs (Private Messages) in Code Project?
|
|
|
|
|
Thank you! Got the entire book. I had to download it from home, since your site was blocked at work.
|
|
|
|
|