Unofficial Programming Thread

Post » Thu Sep 23, 2010 5:13 am

I think I deleted a line in pico by moving the cursor to the line and pressing ctrl-k, simple. Then you can paste the same line with ctrl-u. In most Windows editors, I do need three keystrokes to do the same though - shift+down and delete. Finding patterns in pico was accomplished by ctrl-w and then typing whatever you wanted to find. I forgot what find the next occurance.. enter key? That's a little longer operation for pico, yes, but at least most hotkeys are displayed right there on screen when you edit text, and there are no separate modes for commands and editing.

Pico did exactly what I needed to do when I found Emacs bloated and wouldn't want to touch it with a long stick. Learning vi never really crossed my mind although I was forced to use it on an occasion. It took me a good 10 minutes to change some setting in a configuration because I didn't know how to save or quit. In any other editor it's at least displayed on screen what does what. From user interface design point of view I find vi horrid - just the kind of stuff that causes nightmares to a budding UI designer. I'm sure it's a good editor once you study manual and memorize all the commands but it should have some warning like, "Are you sure you want to run this thing? You are not going find your way out of here if this is your first try, you know." lol
User avatar
Nick Pryce
 
Posts: 3386
Joined: Sat Jul 14, 2007 8:36 pm

Post » Thu Sep 23, 2010 7:08 am

How do you guys archive your programs? I have one printed out in landscape mode with syntax highlighting and line numbers, though sometimes my comments get cut off and sent to a new line which looks bad. I also keep a folder on my PC full of .zip files of the Visual Studio solutions. Any other ways I should consider?
User avatar
Josh Sabatini
 
Posts: 3445
Joined: Wed Nov 14, 2007 9:47 pm

Post » Thu Sep 23, 2010 8:22 am

Alright guys got this really weird problem.

I am making a program to extract files from .zip or .7z archives, which works so far. But then I want to save a text file to a subdirectory that contains the paths of each file I extracted. Sort of like FOMM or OBMM does with mods. Now I am using a variable List to store each path by using the FileSystemWatcher's Created event. For some reason though when I go to write the file it stays empty. I know the list gets written to because I did a test copy/pasting files into the directory and running the program, but when I extract the files it does not work. I cannot figure out why its not since it works fine manually. What is weird though is if I create the file in my form's closing event its fine, but I need to make the file right after all the files are extracted.

Any suggestions about how to fix this? I am using C# with .NET 3.0.
User avatar
John Moore
 
Posts: 3294
Joined: Sun Jun 10, 2007 8:18 am

Post » Thu Sep 23, 2010 6:18 am

How do you guys archive your programs? I have one printed out in landscape mode with syntax highlighting and line numbers, though sometimes my comments get cut off and sent to a new line which looks bad. I also keep a folder on my PC full of .zip files of the Visual Studio solutions. Any other ways I should consider?

CVS/SVN/Git Repository? That is only if you are thinking of altering them though :shrug: Otherwise I would just email them to a gmail account.
User avatar
Bethany Watkin
 
Posts: 3445
Joined: Sun Jul 23, 2006 4:13 pm

Post » Thu Sep 23, 2010 6:41 am

Hello fellow problem solvers, I've got a problem I cant solve on my own here, language is c# ,and the posted code is all psuedo-code, but exactly what I need to do.

switch (MyInt){    case 1:            Names1.Mine Name  = new Names1.Mine();                break;    case 2:            Names2.Mine Name  = new Names2.Mine();                break;}Label1.Text = Name.First;

So, this gives me an error "A local variable named 'Name' is already defined in this scope". So I tried:

switch (MyInt){    case 1:            { Names1.Mine Name  = new Names1.Mine(); }                break;    case 2:            { Names2.Mine Name  = new Names2.Mine(); }                break;}Label1.Text = Name.First;

Notice the brackets, this way they arnt in the same scope, but now I get an error cause Name isnt defined at all in the scope! There has to be some way to be able to define the same variable as two different things depending on MyInt :unsure2: The variable 'Name' is used hundreds of times in my program, I dont see an alternative than doing what the compiler says cannot be done. :nope: Thanks in advance if anyone can think of a way.
User avatar
Adrian Powers
 
Posts: 3368
Joined: Fri Oct 26, 2007 4:44 pm

Post » Thu Sep 23, 2010 6:54 am

The problem is on the last line. Name is not defined in that context because you just limited visibility. Can you just copy that last line into both of those cases? The easiest solution I can think of. I don't know how "Names1.Mine" etc is defined but I guess it works as a type then.

I would have suggested moving Name out of the switch first, but then I noticed you have different types for the same variable name, so it probably wouldn't work unless you start defining it as a pointer and start doing casting of some sort.
User avatar
Amiee Kent
 
Posts: 3447
Joined: Thu Jun 15, 2006 2:25 pm

Post » Thu Sep 23, 2010 12:41 pm

The problem is on the last line. Name is not defined in that context because you just limited visibility. Can you just copy that last line into both of those cases? The easiest solution I can think of. I don't know how "Names1.Mine" etc is defined but I guess it works as a type then.

I would have suggested moving Name out of the switch first, but then I noticed you have different types for the same variable name, so it probably wouldn't work unless you start defining it as a pointer and start doing casting of some sort.

Nope, thats the thing, Name needs to be used out of the switch as its used so many times in my program all over the place. But i need the option to have Name be defined as a type of Names1 or Names2.

"defining it as a pointer and start doing casting of some sort." Hows this work? Both Names1 and Names2 have the same exact variables in them so for example

Names1.Mine.First == PaulNames2.Mine.First == Benjamin

User avatar
Lynette Wilson
 
Posts: 3424
Joined: Fri Jul 14, 2006 4:20 pm

Post » Thu Sep 23, 2010 4:46 am

C# is a statically typed language. A variable can hold values of one type only. If both Names1.Mine and Names2.Mine are subclasses of the same base, that can work by simply doing:
SomeSharedBaseClass Name;switch (MyInt) {    case 1:           Name  = new Names1.Mine();           break;    case 2:           Name  = new Names2.Mine();           break;    default:           Name  = new Names3.Mine();}Label1.Text = Name.First;


You could of course define Name as an Object, but then you won't be able to access First without casting, which makes it pretty much useless. However, if you're even encountering such problems, it's likely your overall design is not appropriate for the language.
User avatar
Flash
 
Posts: 3541
Joined: Fri Oct 13, 2006 3:24 pm

Post » Thu Sep 23, 2010 5:32 am

C# is a statically typed language. A variable can hold values of one type only. If both Names1.Mine and Names2.Mine are subclasses of the same base, that can work by simply doing:
SomeSharedBaseClass Name;switch (MyInt) {    case 1:           Name  = new Names1.Mine();           break;    case 2:           Name  = new Names2.Mine();           break;    default:           Name  = new Names3.Mine();}Label1.Text = Name.First;


You could of course define Name as an Object, but then you won't be able to access First without casting, which makes it pretty much useless. However, if you're even encountering such problems, it's likely your overall design is not appropriate for the language.

Nah its not that the design is not appropriate, on the contrary c# is perfect for it, this problem came up when I thought to combine two versions of my program. Both are the same but using different values under "Names". I think putting them both under the same base will work just fine like your example.

Thanks :celebrate: !!
User avatar
suzan
 
Posts: 3329
Joined: Mon Jul 17, 2006 5:32 pm

Post » Thu Sep 23, 2010 12:23 pm

Solved. :)
User avatar
Anthony Santillan
 
Posts: 3461
Joined: Sun Jul 01, 2007 6:42 am

Post » Thu Sep 23, 2010 2:24 pm

Functional programming anyone? F# i particular?
User avatar
Floor Punch
 
Posts: 3568
Joined: Tue May 29, 2007 7:18 am

Post » Thu Sep 23, 2010 8:56 am

Functional programming anyone? F# i particular?

I did a quick wiki on it, looks complicated :cryvaultboy:

Oh, btw the above switch didnt work exactly, I had to keep the same type but I can (prob, i havnt tested) update the variables from different functions in the same class depending on the value of MyInt. So that works maybe even better.
User avatar
Johanna Van Drunick
 
Posts: 3437
Joined: Tue Jun 20, 2006 11:40 am

Post » Thu Sep 23, 2010 6:27 pm

I did a quick wiki on it, looks complicated :cryvaultboy:

It can be confusing. I'm only starting to grasp how things work. I like the way I can handle lists.
User avatar
Syaza Ramali
 
Posts: 3466
Joined: Wed Jan 24, 2007 10:46 am

Post » Thu Sep 23, 2010 6:56 am

Oh, right. C# has an Object type. I'm still learning that. I've been playing with Variants and Objects in Basic. Especially when doing something over the COM interface. However, it's technically not Visual Basic. It's Sax Basic, which IS compatible with VB6, which also has some roots or design characteristics of the whole .Net thing, doesn't it..

Would you need to do dynamic/static casting in that kind of use case?

I don't know why you need to create an instance of that object, but I guess you need it. I'm still slightly confused by the C# typology as a C++/Basic coder.
User avatar
rae.x
 
Posts: 3326
Joined: Wed Jun 14, 2006 2:13 pm

Post » Thu Sep 23, 2010 1:31 pm

Oh, right. C# has an Object type. I'm still learning that. I've been playing with Variants and Objects in Basic. Especially when doing something over the COM interface. However, it's technically not Visual Basic. It's Sax Basic, which IS compatible with VB6, which also has some roots or design characteristics of the whole .Net thing, doesn't it..

Would you need to do dynamic/static casting in that kind of use case?

I don't know why you need to create an instance of that object, but I guess you need it. I'm still slightly confused by the C# typology as a C++/Basic coder.

Referring to the above? If I made Name of type object it would create a huge mess, Name holds all sorts of different types of variables and every time i used one i would need to cast it before using.
I suppose I dont _really_ need my class as a non-static, I never really thought about changing it tbh. almost all my functions in my program are static except this one, when i need it i just create a new instance of it, its no big deal. In some places I think i've created a static instance of it and used it in multiple functions in the class its being used.
User avatar
Charlotte Henderson
 
Posts: 3337
Joined: Wed Oct 11, 2006 12:37 pm

Post » Thu Sep 23, 2010 6:52 pm

I'm too much into C++ coding.. so I shouldn't touch C# topics.

In any case the static_cast operator in C++ can be used for converting a pointer to a base class to a pointer to a derived class. Then there is dynamic_cast. You may need to check for null pointer in case the conversion cannot be made. You may do upcasts and downcasts along the class hierarchy. I may not be able to give a perfect explanation of this so I'll just throw lines here to show what I meant.

base * pb = new base;
derived * pd = new derived;
derived * d = static_cast(pb);
base * b = dynamic_cast(pd);

Might be a bad practice anyways in C++, but still..
User avatar
Samantha Jane Adams
 
Posts: 3433
Joined: Mon Dec 04, 2006 4:00 pm

Post » Thu Sep 23, 2010 12:06 pm

I'm too much into C++ coding.. so I shouldn't touch C# topics.

In any case the static_cast operator in C++ can be used for converting a pointer to a base class to a pointer to a derived class. Then there is dynamic_cast. You may need to check for null pointer in case the conversion cannot be made. You may do upcasts and downcasts along the class hierarchy. I may not be able to give a perfect explanation of this so I'll just throw lines here to show what I meant.

base * pb = new base;
derived * pd = new derived;
derived * d = static_cast(pb);
base * b = dynamic_cast(pd);

Might be a bad practice anyways in C++, but still..


It's to be avoided where possible, but sometimes you just cannot do without casting.
User avatar
The Time Car
 
Posts: 3435
Joined: Sat Oct 27, 2007 7:13 pm

Post » Thu Sep 23, 2010 7:14 am

It's to be avoided where possible, but sometimes you just cannot do without casting.

My problem with c++ is converting between types, it makes it so difficult! From LWPSTR to LPSTR or string to int, char to char[], string to char[]. I can go on cause there are so many types, but c++ def does not make it easy. In c# I can do something as simple as Convert.To(MyVar);

Half the time in c++ when converting something to string i have to use MyVar.c_str() then mess around with it to get it to work with w/e function i need it for. So unnecessarily complicated!! :swear:

Maybe I just dont really understand something about it, who knows, I just know im never going back to c++ unless I absolutely need it for something.
User avatar
Roberto Gaeta
 
Posts: 3451
Joined: Tue Nov 06, 2007 2:23 am

Post » Thu Sep 23, 2010 10:01 am

Well, that's right, they improved a few things in C#, and then removed some features they deemed unnecessary, like polymorphism if I'm correct. Those LPwhatever types are mostly existent in Windows environments aren't they. There's LPCSTR, w_char, string, char *.. right. They all kinda come from numerous different libraries though. COM interface also intervenes in this brings a few string types, variant types, optional parameters and whatever. Have you seen how different adding a COM interface is in C++ and C#. For C++ you'll have write all those functions in a specific form with non-c++ looking definition, but they are actually macros of some kind and add a bunch of code into the middle of the code. In C#, all you practically need to do is set COM_VISIBLE = true, or something like that. Then your class definition is fully visible through COM and that's all there is to it.

You can use operators in conversions in C++ too. It's just that C# brings a quite a lot of that into every class with the framework by default. You don't need to separately define everything. It's kinda good. Less time spent in repetitive coding. Yes, C++ needs more work to do some things, definitely.
User avatar
rae.x
 
Posts: 3326
Joined: Wed Jun 14, 2006 2:13 pm

Post » Thu Sep 23, 2010 5:51 pm

My problem with c++ is converting between types, it makes it so difficult!

I will admit that was got me at first. Going from a weakly typed language to a fairly strongly typed language was a pain.

From LWPSTR to LPSTR

That looks like windows typedefs for wchar_t* and char*. You can't convert between them without data loss. (Unicode to ASCII). If you want to the last byte of each wchar is the ascii value, as long as all other bytes in the wchar are 0

string to int,

Look at stringstreams. It is really easy to write a toString/fromString function if needed. If you are using boost, look at boost::lexical_cast

char to char[]
There is no standard way to convert a type to an array of that type. Is it even a string? Should it be null terminated? It has to be application specific. Not every application wants a character array to be null terminated.

string to char[]

You just need to copy the result from string::c_str to a char*
std::string str("foo");char* c = new char[str.length+1]; //allocate new memory storage for the datastrcpy(c, str.c_str()); //copy the data from the string


If you only need it within the lifetime of the string you can use str.c_str();

Half the time in c++ when converting something to string i have to use MyVar.c_str() then mess around with it to get it to work with w/e function i need it for. So unnecessarily complicated!! :swear:

? Modifying the const char* result of c_str is undefined behaviour.
User avatar
Cat Haines
 
Posts: 3385
Joined: Fri Oct 27, 2006 9:27 am

Post » Thu Sep 23, 2010 7:48 pm

I will admit that was got me at first. Going from a weakly typed language to a fairly strongly typed language was a pain.


That looks like windows typedefs for wchar_t* and char*. You can't convert between them without data loss. (Unicode to ASCII). If you want to the last byte of each wchar is the ascii value, as long as all other bytes in the wchar are 0


Look at stringstreams. It is really easy to write a toString/fromString function if needed. If you are using boost, look at boost::lexical_cast

There is no standard way to convert a type to an array of that type. Is it even a string? Should it be null terminated? It has to be application specific. Not every application wants a character array to be null terminated.


You just need to copy the result from string::c_str to a char*
std::string str("foo");char* c = new char[str.length+1]; //allocate new memory storage for the datastrcpy(c, str.c_str()); //copy the data from the string


If you only need it within the lifetime of the string you can use str.c_str();


? Modifying the const char* result of c_str is undefined behaviour.

strcpy(c, str.c_str()); I would use it like that, not modify it directly. Still c++ has its uses, and its used on a lot of game development.
User avatar
Jessica Raven
 
Posts: 3409
Joined: Thu Dec 21, 2006 4:33 am

Post » Thu Sep 23, 2010 1:15 pm

Still c++ has its uses, and its used on a lot of game development.

To be honest I really hope http://en.wikipedia.org/wiki/D_(programming_language) takes off and supplants C++ as it is a far nicer systems programming language. I doubt it will though :shrug:
User avatar
Samantha Jane Adams
 
Posts: 3433
Joined: Mon Dec 04, 2006 4:00 pm

Post » Thu Sep 23, 2010 8:36 pm

http://stackapps.com/questions/1141/stacked-odds-finding-the-questions-that-you-can-answer what I have currently been working on... I like JavaScript, I hate dealing with Chromes permssions and sandboxing. I spent at least half the time I wrote it banging my had against the wall and making stupid mistakes. I also found that jQuery wasn't enough. While it is fine for small things as soon as you write something larger, you need features it doesn't provide. The most obvious thing is splitting code into modules and being able to have modules require other modules. I ended up using RequireJs for this.


If you can upvote the linked question and you think what I wrote is worth it, feel free (i.e. it would be helpful). You will need 200 rep on another site (Like StackOverflow) to do it though. It just needs 6 upvotes and I realised that I am terrible at marketing :shrug:
User avatar
MR.BIGG
 
Posts: 3373
Joined: Sat Sep 08, 2007 7:51 am

Post » Thu Sep 23, 2010 5:54 am

So, I've just spent the last hour or so cleaning up a few parts of my todo list manager - I quite like it, now. It stores todo entries, tags, completion/failure and addition times in an sqlite3 database. To list them, it generates a tree using each tag's parent-child relationship and then recursively prints them out, indenting them by recursion depth.
Written in python 2.6, requires nothing outside of the standard library, and works on at least windows and linux. http://dl.dropbox.com/u/2502059/Python%20Scripts/todo.py, or to give any advice :)
User avatar
Rusty Billiot
 
Posts: 3431
Joined: Sat Sep 22, 2007 10:22 pm

Post » Thu Sep 23, 2010 8:14 am

I currently have taken a small break from writing anything since I couldn't get threading to work in my hash validator. I also kind of hit a wall with ideas, thugh my old VB.NET book has tons of ideas in it as opposed to both my C# books. I may pick something back up soon though after I get through my A+ exam and back to school next month. Hopefully I can think up a project before then and get it done. I did try and build a similar program to OBMM for Unreal Tournament 3 maps but it failed horribly when for some reason I could add files added to a listbox but not a List collection. I have no idea why it works for the listbox but not the collection, the commands are the same to add to each and I put them in the same place in the code.

If anyone has any idea about this issue feel free to let me know. Though I did get too annoyed with it and scrapped the project I could probably write it again.
User avatar
NAkeshIa BENNETT
 
Posts: 3519
Joined: Fri Jun 16, 2006 12:23 pm

PreviousNext

Return to Othor Games