Author Topic: Ugh, Noob C# Question...  (Read 7935 times)

0 Members and 1 Guest are viewing this topic.

Offline abc

  • Hero Member
  • *****
  • Posts: 576
    • View Profile
Ugh, Noob C# Question...
« on: June 30, 2007, 08:57:17 pm »
I'm sure this is the most pathetic question in C#...But I have a GUI on my form frmMain (go figure  :P) and then I have another class, that I want to control the text of frmMain, so I in frmMain.Designer.cs I made my GUI controls publics so I could alter the text. So in one of my voids, I put
Code: [Select]
frmMain Main = new frmMain();
Main.TabPage1.Text = "Blah";

To append text, I also use my AddChat function, which is in my frmMain.cs and nothing is working, I compile, and get no errors, nothing happens though...

I hope this wasn't too confusing.. I have such a headache..

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: Ugh, Noob C# Question...
« Reply #1 on: June 30, 2007, 11:18:35 pm »
I've got a neat solution but I can't attach it for some reason. Anyhow, IM me and I'll send it.
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Ender

  • x86
  • Hero Member
  • *****
  • Posts: 2390
    • View Profile
Re: Ugh, Noob C# Question...
« Reply #2 on: July 01, 2007, 05:02:19 am »
Methods, not public fields :P aka data encapsulation. Granted, encapsulation can sometimes be a nuisance for things like setUsername() getUsername() etc., but in a situation like this, i.e. modifying the output on a GUI, I believe encapsulation is warranted.

I think learning some of the basics of OOP and software design would highly benefit.
« Last Edit: July 01, 2007, 05:06:47 am by Ender »

Offline Ender

  • x86
  • Hero Member
  • *****
  • Posts: 2390
    • View Profile
Re: Ugh, Noob C# Question...
« Reply #3 on: July 01, 2007, 05:04:51 am »
I'm sure this is the most pathetic question in C#...But I have a GUI on my form frmMain (go figure  :P) and then I have another class, that I want to control the text of frmMain, so I in frmMain.Designer.cs I made my GUI controls publics so I could alter the text. So in one of my voids, I put
Code: [Select]
frmMain Main = new frmMain();
Main.TabPage1.Text = "Blah";

To append text, I also use my AddChat function, which is in my frmMain.cs and nothing is working, I compile, and get no errors, nothing happens though...

I hope this wasn't too confusing.. I have such a headache..

Sounds like you're talking to the wrong object, i.e. you're instantiating a new object different from the one that displays your GUI, and telling that object to addChat, modifying variables that never come into play. You have to pass a reference around your program to the original GUI object (there really should be no more than one). There are many ways to do this, such as passing by constructors, passing by methods, Singletons, etc.

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: Ugh, Noob C# Question...
« Reply #4 on: July 01, 2007, 12:58:22 pm »
Methods, not public fields :P aka data encapsulation. Granted, encapsulation can sometimes be a nuisance for things like setUsername() getUsername() etc., but in a situation like this, i.e. modifying the output on a GUI, I believe encapsulation is warranted.

I think learning some of the basics of OOP and software design would highly benefit.

Code: [Select]
private string c_sUsername;
public string Username
{
    set { this.c_sUsername = value; }
    get { return this.c_sUsername; }
}
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: Ugh, Noob C# Question...
« Reply #5 on: July 01, 2007, 05:25:53 pm »
You need to learn object-oriented programming, as Ender said, before you try to tackle a bot or something similar in C#.
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline Hdx

  • The Hdx!
  • Full Member
  • ***
  • Posts: 311
  • <3 Java/Cpp/VB/QB
    • View Profile
Re: Ugh, Noob C# Question...
« Reply #6 on: July 03, 2007, 02:14:23 pm »
Code: [Select]
private string c_sUsername;
public string Username
{
    set { this.c_sUsername = value; }
    get { return this.c_sUsername; }
}
Sorry to hijack this a bit.
Does this method work in java? (I'm at the library, no place to test)
I've never seen it done in java so.. ?
If it does this will help emensly in memory consumption of things i'm writing.
~Hdx
http://img140.exs.cx/img140/6720/hdxnew6lb.gif
09/08/05 - Clan SBs @ USEast
 [19:59:04.000] <DeadHelp> We don't like customers.
 [19:59:05.922] <DeadHelp> They're assholes
 [19:59:08.094] <DeadHelp> And they're never right.

Offline Warrior

  • supreme mac daddy of trolls
  • Hero Member
  • *****
  • Posts: 7503
  • One for a Dime two for a Quarter!
    • View Profile
Re: Ugh, Noob C# Question...
« Reply #7 on: July 03, 2007, 02:26:42 pm »
Code: [Select]
private string c_sUsername;
public string Username
{
    set { this.c_sUsername = value; }
    get { return this.c_sUsername; }
}
Sorry to hijack this a bit.
Does this method work in java? (I'm at the library, no place to test)
I've never seen it done in java so.. ?
If it does this will help emensly in memory consumption of things i'm writing.
~Hdx

It should, or at least a variant of it. Most OO programming languages should have Property modifiers. (Hell, even VB has them)
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Offline Ender

  • x86
  • Hero Member
  • *****
  • Posts: 2390
    • View Profile
Re: Ugh, Noob C# Question...
« Reply #8 on: July 04, 2007, 12:35:12 am »
Java doesn't have property modifiers like that. I haven't tested it but I'm 99.9% sure :P

Offline Warrior

  • supreme mac daddy of trolls
  • Hero Member
  • *****
  • Posts: 7503
  • One for a Dime two for a Quarter!
    • View Profile
Re: Ugh, Noob C# Question...
« Reply #9 on: July 04, 2007, 01:06:25 am »
You're right, Java isn't as advanced as C#. =/
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: Ugh, Noob C# Question...
« Reply #10 on: July 07, 2007, 08:55:29 am »
We could reverse that on you and say that C# is more complicated than Java and a pain in the ass to use. Please leave your bias at the door.

@Hdx: No. If you just do set and get methods though it shouldn't be too intensive. Perform some sanity checks where needed if you want a little edge.
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Warrior

  • supreme mac daddy of trolls
  • Hero Member
  • *****
  • Posts: 7503
  • One for a Dime two for a Quarter!
    • View Profile
Re: Ugh, Noob C# Question...
« Reply #11 on: July 07, 2007, 10:48:25 am »
We could reverse that on you and say that C# is more complicated than Java and a pain in the ass to use. Please leave your bias at the door.

@Hdx: No. If you just do set and get methods though it shouldn't be too intensive. Perform some sanity checks where needed if you want a little edge.

I can reverse this and say you're an idiot. Try to leave that at the door.
Also, who's "we" you're one person.

Shut up.
« Last Edit: July 07, 2007, 10:56:28 am by Warrior[x86] »
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: Ugh, Noob C# Question...
« Reply #12 on: July 07, 2007, 05:46:36 pm »
We could reverse that on you and say that C# is more complicated than Java and a pain in the ass to use. Please leave your bias at the door.
Not really.  Here's why.

C# is a superset of Java.  Everything that you can do in Java, you can do in C# with minimal modification (such as type name changes).  A java class will be valid C#.

However, C# supports treating primitives like objects.  Java's type system does not.  In Java you have (as types that can be represented by literals):

Code: [Select]
byte
short
int
long
float
double
Object
|-> String

In C# you have:
Code: [Select]
Object
|-> byte
|-> sbyte
|-> short
|-> ushort
|-> int
|-> uint
|-> long
|-> ulong
|-> float
|-> double
|-> decimal
|-> string
(Technically, all of the numerics in C# inherit from System.ValueType, but that's an internal compiler detail since ValueType isn't really used in any instance ever).

Because in C# all primitives are actually Objects, we can do something like so:
Code: [Select]
ArrayList al = new ArrayList();
al.Add(1);
al.Add(4485);
al.Add("This is a string.");
In Java, you can't; you have to provide the class wrapper (except in the case of strings):
Code: [Select]
al.add(new Integer(1));
al.add(new Integer(4485));
al.add("This is a string.");
Also, because C# supports overloaded operator this[], ArrayList can be treated like an array, which makes its syntax cleaner.  Example:
Code: [Select]
int val = (int)al[0];
int val2 = (int)al[1];
string val3 = al[2] as string;
In Java you'd have to:
Code: [Select]
int val = ((Integer)al.get(0)).intValue();
int val2 = ((Integer)al.get(1)).intValue();
String val3 = (String)al.get(2);
Note also that the semantic difference between the C# and Java implementations of getting "val3" are important.  In C#, if the third ArrayList element is not a String-compatible type, val3 will be null.  In Java, because you're forced to do typecasting in this way, you run the risk of generating a ClassCastException.  If you cast that way in C# you can generate an InvalidCastException as well.  The point is that you don't need to cast reference types in a way that generates an unnecessary exception.

I haven't even begun to talk about all other ways that Java is a pain in the ass - such as the necessity of an additional class where function pointers would suffice (Runnable, for example, vs. a ThreadStart calback in C#).
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline Warrior

  • supreme mac daddy of trolls
  • Hero Member
  • *****
  • Posts: 7503
  • One for a Dime two for a Quarter!
    • View Profile
Re: Ugh, Noob C# Question...
« Reply #13 on: July 07, 2007, 05:52:31 pm »
I can tell you enjoyed that.
One must ask oneself: "do I will trolling to become a universal law?" And then when one realizes "yes, I do will it to be such," one feels completely justified.
-- from Groundwork for the Metaphysics of Trolling

Offline Hitmen

  • B&
  • x86
  • Hero Member
  • *****
  • Posts: 1913
    • View Profile
Re: Ugh, Noob C# Question...
« Reply #14 on: August 16, 2007, 11:14:15 am »
Because in C# all primitives are actually Objects, we can do something like so:
Code: [Select]
ArrayList al = new ArrayList();
al.Add(1);
al.Add(4485);
al.Add("This is a string.");
In Java, you can't; you have to provide the class wrapper (except in the case of strings):
Code: [Select]
al.add(new Integer(1));
al.add(new Integer(4485));
al.add("This is a string.");

I know this post is a bit old, but I just want to point out that this is entirely wrong. Clicky.

Though currently the code should look something like this:
Code: [Select]
ArrayList<Object> al = new ArrayList<Object>();
al.add(1);
al.add(4485);
al.add("This is a string.");
Quote
(22:15:39) Newby: it hurts to swallow