PDA

View Full Version : Learning C#, variable problem



schrodingerscat
2008-10-03, 05:54 AM
I'm learning C# basics so that I can use that knowledge with Revit API later, but I'm having some trouble getting started. Had no problem writing a simple addition calculator, but I'm trying to write some code to work out Fibonacci numbers (so I can learn variable redefinition, loops, and selection) but I'm having some trouble with the variables. I can't find a straight answer anywhere on how you're supposed to redefine variables.


using System;
public class FibonacciTest
{
public static void Main (string [] args)
{
Console.Write ("Enter sequence number: ");
decimal F = Convert.ToDecimal (Console.ReadLine ());

decimal Fn = 0;
decimal Count = 1;

if (F==0)
{
Console.WriteLine(Fn);
decimal Fn = 0;
}
else
if (F==1)
{
Console.WriteLine(Fn);
decimal Fn = 1;
}
else
{
Console.WriteLine(Fn);
decimal Fn = 1;
decimal Fp = Fn;
while (Count < F)
{
Console.WriteLine(Fn);
Console.WriteLine(Fp);
Console.WriteLine(Count);
decimal Fn = (Fn + Fp);
decimal Count = (Count + 1);
}
}

Console.Write ("\nFibonacci number: " + Fn);

Console.WriteLine ("\nAny key to end.");
Console.Read ();
}
}

That's what I've done so far (not very pretty, but it's teaching me heaps about what I want to learn).

Any idea as to why I can't redefine the variables, or just how you're supposed to go about redefining?

I read somewhere you have to redefine the allocated memory instead of the actual variable or something.

david.bartliff
2008-10-03, 08:56 AM
Its a good job you didn't post this on one of the Autodesk programming discussion groups - there's a bloke on there who's very helpful but he hates posts like this and would have gone over the top.
Anyway - you need to understand the difference between declaring a variable, i.e. saying what type of data it will hold, which you just need to do once and assigning a value to it, which you can do as often as you like.
i.e.
decimal Fn = 0
...
Fn = 0
...
Fn = 1
...
i.e. don't repeat the "decimal" keyword when you change the value.

It gets more complicated for arrays which are re-defined in Basic if you need to change the size, which might be where your confusion came from.

Please don't take offence but you really need to get yourself some basic programming training. I've got a lot of programming experience so I picked up C# without any help from books or courses, but hopefully someone else on here will recommend something. Or you could search other threads for suggestions.

schrodingerscat
2008-10-06, 10:02 PM
I've only ever worked in HTML, Javascript, VB, BASIC, and AutoLISP. Never had a need to know any more than that.

I may be getting some training but until I see the worth of it in regards to Revit, I'm just going to explore it on my own. At the moment the only use I can think of for it is writing a tool similar to the "divide" command in ACAD but specifically for luminaire spacings.

r.howarth
2008-10-07, 03:06 AM
grab yourself a good C# book and you'll learn fairly quickly ;)

I believe in VB you say 'Dim variableName' - in VB.Net you say 'Dim variableName As integer'
ie integer is the type.

in C# you say 'integer variableName'
so then after that you just refer to it as variableName.

you can use VB.Net in the revit API too, might be more familiar if you've done VB before.

Elizabeth Shulok
2008-10-07, 05:12 AM
I came across this website (http://www.yevol.com/en/csharp/index.htm)when I was learning C#. It has a lot of walk-through examples for someone just getting started.

schrodingerscat
2008-10-07, 06:41 AM
I would grab myself a C# book, but they seem to be around the $150 mark average for a decent one and for me that's a fair amount of money. That's equivalent to 3 weeks groceries.

I'll check out that website, should help considering I'm someone who learns by doing. The tutorials I've looked at so far seem like you need to know C# before you start. You would have to read it, then re-read it to make sense of it because things in the first section refer to things in the last few sections, so you read it and then sit there confused as hell.

Cheers!

RobertB
2008-10-07, 03:28 PM
... around the $150 mark average for a decent one and for me that's a fair amount of money. That's equivalent to 3 weeks groceries.Yikes! What do your meals consist of? Top Ramen and bologna? :mrgreen:

schrodingerscat
2008-10-08, 01:19 AM
Hmm.... my meals... had crumbed prawns and steamed veggies last night, bacon and mushroom sauce on pasta the night before...

I eat fairly well. All that for only $50 a week, so you can see how a $150 book on something I MAY not use is considered a fair amount for me.

RobertB
2008-10-08, 01:23 AM
Hmm.... my meals... had crumbed prawns and steamed veggies last night, bacon and mushroom sauce on pasta the night before... MMMM... must get food.

r.howarth
2008-10-08, 01:33 AM
he's in australia so that's aussie dollars but still a tight budget there!

Wouldn't Connell Wagner foot for the book? if not, it'd be tax deductable if its related to your career (which it probably is?) I'm sure you can buy some Ebooks fairly cheap, hit up amazon.com for cheaper prices. $150 sounds a bit too much, even for borders etc! from memory I got an asp.net ebook for around 30-40 bucks. I just print the subjects I want..

Having said that, you just need to know the basics of OOP as well as the syntax of C#, you don't need to go into great depth as most books would go to.

I'm sure there's guides online, just a book (or even a uni subject as I did) would be good.

Steve_Stafford
2008-10-08, 02:11 AM
Food aside what is your time worth? Would you rather struggle for a week or learn something in 20 minutes from a book...? The answer is different for everyone...

schrodingerscat
2008-10-08, 06:37 AM
I'd rather eat XD

Amazon isn't really an option. I don't believe in buying something that isn't a necessity without having the capital at the time of purchase, therefore, no credit card, therefore no Amazon.

If I do find C# useful I can get my work to send me to a course, but if not I'd rather not get them to fork out $2500 for the course, my weekly pay, and get nothing in return.

r.howarth
2008-10-08, 07:18 AM
I agree with your philosophy on credit cards, I usually buy off amazon with a visa debit card (ie, no credit). But anyway thats irellevant:

http://msdn.microsoft.com/en-us/library/aa288436.aspx is some microsoft C# tutorials, im sure theres similar stuff on the net.

And a google search or 2 returned this:
http://www.programmersheaven.com/2/CSharpBook

looks great, a quick flick through and it looks juts as usefull as your $150 ones, so you can have your C# AND eat it :)

schrodingerscat
2008-10-09, 12:33 AM
Sounds good, I think I'm getting the hang of it.

If anyone's interested I ended up rewriting the program to this


using System;
public class FibonacciTest
{
public static void Main (string [] args)
{
Console.Write ("Enter sequence number: ");
double n = Convert.ToDouble (Console.ReadLine ());

double Phi = ((1 + (Math.Sqrt (5))) / 2);

double Fn = (((Math.Pow (Phi, n)) - (Math.Pow ((1 - Phi), n))) / (Math.Sqrt (5)));

Console.Write ("\nFibonacci number: " + Fn);

Console.WriteLine ("\nAny key to end.");
Console.Read ();
}
}


I'm still not actually sure if I can interface with Revit at all. I know you use the Autodesk.Revit namespace (sorry if my terminology is wrong) but the program I'm using (sharpdevelop) won't recognise it exists and so won't compile.Guessing it's going to be harder than just learning C# and then adding a few more data types and objects to interface with Revit.

r.howarth
2008-10-09, 03:17 AM
Not sure on sharpdevelop, though you can get VIsual Studio 2008 express edition for free from microsofts site.

a couple of useful blog posts:
http://thebuildingcoder.typepad.com/blog/2008/08/getting-started.html
http://roddotnet.blogspot.com/2008/02/setting-up-template-for-revit-api.html

In visual studio you have to add a reference (ie import) to the revit api DLL file (in the revit directory) then when you use the autodesk.revit namespace, it should recognize it then, i guess it would be similar in sharpdevelop, I'm sure the functionality is there (as .net is pretty limited without it !)

schrodingerscat
2008-10-09, 04:19 AM
Yeah, not too keen on downloading that, it's kind of a big file and if I sit at work downloading it I'll slow down the Internet for other people, and I don't have the Internet at home. Sharpdevelop was about 10Mb.

Elizabeth Shulok
2008-10-09, 03:17 PM
I've seen GuyR advocate using Sharpdevelop to work with the Revit API, so you should be able to get it to work. If you don't have any luck, start a new thread about your problem and maybe someone using Sharpdevelop can help you out.

And if it makes you feel any better, my family of 3 (one of us being an 18 month old) spends on groceries in a week nearly as much as you spend in 3 weeks. And our meals don't sound nearly as good as yours.

schrodingerscat
2008-10-12, 10:31 PM
I'll look into it a bit more, but at the moment Sharpdevelop just won't build because it says it can't find Autodesk.Revit or it isn't registered or something.

By the way, I spend $50 on groceries but then I also spend $10 every Friday buying my lunch, $10 every Sunday buying my tea, and a few dollars every few days buying a random snack. All adds up! No way I could support a family of 3 XD