-
Need help with craeting a class with a single function in it.
[Rant]
This is stupid. I wanna rant 1st. c# is overly complicated and pointless compared to VB.NET in my mind, alas, I am being forced to learn this akward language that give me no extra benefit when coding reletively simple web apps :(
[/Rant]
Ok...Vb.NET:
VB Code:
Public Class Users()
Public Function Msg() As String
Return "Woof"
End Function
End Class
Then in UI:
VB Code:
Dim MyObj As New Users
Console.WriteLine(MyObj.Msg)
Simple and to the point.
The mess I have in c# is:
VB Code:
public class Users
{
public Users()
{
///What on earth is this for?! Obvious isn't it...BAH!
///Pointless...???
}
public string Msg()
{
return "Woof";
}
}
Then in UI:
VB Code:
Users MyObject = new Users();
System.Console.WriteLine(MyObject.Msg());
What actually happens in the .Msg in the UI gets a squiggley line and the error is:
Quote:
Method 'Users.Msg()' referenced without parentheses
Wot on earth does that mean?!
I am tired and stressed :(
Woka
-
Re: Need help with craeting a class with a single function in it.
Quote:
Originally Posted by Wokawidget
[Rant]
This is stupid. I wanna rant 1st. c# is overly complicated and pointless compared to VB.NET in my mind, alas, I am being forced to learn this akward language that give me no extra benefit when coding reletively simple web apps :(
[/Rant]
I would like to say two things to this.
1. C# is not overly complicated. In fact, it has almost as many keywords and syntax characteristics as VB.Net.
2. It's not pointless. The syntax is similiar to many other languages (C++ and Java) allowing an easy transition into other languages. It's also faster than VB.Net in many areas. Because of VB.Net's more natural syntax, it's harder to translate into MSIL, thus not giving it quite the performance that C# has. Plus, C# has access to unmanaged syntax like pointers to gain even more speed.
I know those are your opinion but I'm the kind of person who can't let it go :). Sorry
As for your code... It works fine for me :confused:
Code:
namespace ConsoleTest
{
public class Users
{
public Users()
{
//This is the constructor of the Users' class. VB Also constructors ;)
//This is for initializations and such. This method is GUARENTEED
//to run before anything else in the class
}
public string Msg()
{
return "Woof";
}
}
class Entry
{
[STAThread]
static void Main(string[] args)
{
Users MyObject = new Users();
Console.WriteLine(MyObject.Msg());
}
}
}
Like mentioned in my souce code, Users() is the constructor of the class. The constructor always has the same name as the class and I thought it was very obviously but then again I came from C++. The constructor is run when the object is created and your favorite language (VB) has them as well.
Not really sure what you did wrong. Did you post the exact source code?
-
1 Attachment(s)
Re: Need help with craeting a class with a single function in it.
It was probably the name "Users".
-
Re: Need help with craeting a class with a single function in it.
Quote:
public Users()
{
///What on earth is this for?! Obvious isn't it...BAH!
///Pointless...???
}
That's the default no arguments constructor. I know more about C# than VB.NET, but I believe VB hides this from you. You know what would be really insteresting (assuming you have both simple classes working), would be to check out the MSIL of both versions and see how different things are.
Mike
-
Re: Need help with craeting a class with a single function in it.
The simple fact is that VB.NET does a lot of things for you where C# requires you to be explicit. Many people like the fact that VB.NET does these things for you because it means that you don't have to think about them yourself. Many others hate the fact that the language does things behind your back without you having told it to, r the fact that the language is doing things that many less experienced developers don't even know about and thus don't know if they are correct or not. For instance, C# requires at least one explicit constructor while VB.NET will provide a default constructor for you. This is cool if you don't want to do anything in a constructor, but the fact that a lot of VB.NET programmers don't know what a constructor is might indicate that it is not always the best thing. This is an example of a strength also being a weakness. Also, C# requires you to follow a method call with parentheses. Only properties and fields can be accessed without. Personally, I always add parentheses to my method calls in VB.NET too so that they are distinguished as methods and not properties. So many things about VB.NET and C# are not inherently good or bad. They are just different and could be considered a strength and/or a weakness dependng on the situation. The problem as I see it is that VB is spposed to be the easier to learn, partly because of the syntax and partly because it does do various things for you. The fact that it does those things for actually hinders your learning once you start to gain some experience though, because you aren't aware of what's happeneing in many cases. For instance, how many VB.NET developers think they have a handle on things and then fall apart as soon as you tell them to turn Option Strict On.
-
Re: Need help with craeting a class with a single function in it.
thanks all. Woohoooo I have written a parent child class :D
Wossy helped loads with this too, even though he thinks c# rocks ;)
VB Code:
using System;
using System.Collections;
namespace BusinessObjects
{
public class Users
{
private ArrayList items = new ArrayList();
public Users()
{
///???
}
public User Item(int Index)
{
return (User)items[Index];
}
public int Count
{
get
{
return items.Count;
}
}
public void AddUser(User NewUser)
{
items.Add(NewUser);
}
}
public class User
{
private string username = string.Empty;
private string password = string.Empty;
public User()
{
//create blank user class
}
public User(string Username, string Password)
{
username = Username;
password = Password;
}
public string Username
{
get
{
return username;
}
set
{
username = value;
}
}
public string Password
{
get
{
return password;
}
set
{
password = value;
}
}
}
}
then to use:
VB Code:
Users MyObjects = new Users();
User MyObject = new User("Woka", "Password");
MyObjects.AddUser(MyObject);
MyObject = new User("Wossy","HatesVB");
MyObjects.AddUser(MyObject);
MyObject = new User("RobDog","Moose");
MyObjects.AddUser(MyObject);
System.Console.WriteLine(MyObjects.Item(2).Username);
Am a happy badger :D
Woof
-
Re: Need help with craeting a class with a single function in it.
Ok, now puyll my code apart and tell me what c# design things I have done wrong.\
Does c# have OverLoads keyword like VB.NET does?
Woof
-
Re: Need help with craeting a class with a single function in it.
C# is the Lotus, extinguish the flame of doubt with the power of your mind, impetuous student.
-
Re: Need help with craeting a class with a single function in it.
Quote:
Originally Posted by Wokawidget
Ok, now puyll my code apart and tell me what c# design things I have done wrong.\
Does c# have OverLoads keyword like VB.NET does?
Woof
you'll have to look up th override syntax. its been ages sinne i used Vb for anything like this, due to Vb's restrictive and arbitrary syntax
-
Re: Need help with craeting a class with a single function in it.
ok. I have droped a combo on my form. I have named this cboUsers.
In Form_Load how on earth do I access this???!
and
Both do not work and I get 0 intellisense :(
:mad:
Woka
-
Re: Need help with craeting a class with a single function in it.
Wait...err...I have to BUILD the damn app 1st!!!
b4 code can see it. WOE?! :(
*cries again*
Woka
-
Re: Need help with craeting a class with a single function in it.
Looks like it does it without the overrrides keyword. Adding on to my example.
VB Code:
using System;
namespace Test_CS
{
public class User
{
public User()
{
//
// TODO: Add constructor logic here
//
}
public string Msg()
{
return "Woof";
}
public string Msg(string str)
{
return str;
}
}
}
Then to invoke it ...
VB Code:
private void Form1_Load(object sender, System.EventArgs e)
{
User MyObj = new User();
///System.Console.WriteLine(MyObj.Msg());
MessageBox.Show(MyObj.Msg("Ruff!")); ///Ruff!
MessageBox.Show(MyObj.Msg()); ///Woof!
}
-
Re: Need help with craeting a class with a single function in it.
Quote:
Originally Posted by RobDog888
Looks like it does it without the overrrides keyword. Adding on to my example.
You don't need to override anything in your example. I think you have overloading methods confused.
Overloading allows the same function name to receive different amounts of arguements. This works the same in both C# and VB. You just declare the functions and add the arguements.
Override allows you to create your own method to replace a method that your class derives from. This allows you to make your own ToString() method because you override the one your class' base has.
-
Re: Need help with craeting a class with a single function in it.
My bad. I got it stuck in my head from post #9. :D
Yes, it should be Overloads
-
Re: Need help with craeting a class with a single function in it.
It looks to me like you are trying to create a strongly-typed collection. Shouldn't your Users class be inheriting CollectionBase? That's the recommended way to create a strongly-typed collection, although I guess it's not essential.
-
Re: Need help with craeting a class with a single function in it.
errr...I am not sure inheritance is the right thing to use here :(
I am of the opinion that inheritance is used too much, which is a very bad thing.
I can see what you're getting at though, and I have thought about this in the past with VB6...however, I do not want the UI developer to see ANY properties/functions that i do not want them to use.
I am a firm believer of black boxing...but this is a religeous type of discussion...no one answer is right, and no one answer is wrong.
Rob, Yea, it's overloads. In vb.net u do not need to add the opverloads keyword, but if u do it makes reading the code much nicer.
I find VB.NET code to be 110% much easier to read compared with c#
WOka
-
Re: Need help with craeting a class with a single function in it.
Quote:
I can see what you're getting at though, and I have thought about this in the past with VB6...however, I do not want the UI developer to see ANY properties/functions that i do not want them to use.
I wish I new this when I was learning vb.net and was writting that XP Panel user control. I had a hard time hiding the properties and methods I didnt want the developers to "see". :(
-
Re: Need help with craeting a class with a single function in it.
Quote:
Originally Posted by Wokawidget
I find VB.NET code to be 110% much easier to read compared with c#
That's coming from someone with a lot of VB experience so of course it's so. Your average C++ or Java developer would say exactly the opposite. It's like us trying to read Chinese and a Chinese person reading English. Ease comes from experience and familiarity. You may never be as much at ease with the language that you learn last, but that doesn't mean that someone who learned the two the other way around wouldn't feel exactly the opposite.
I still think you should look at the CollectionBase class. By inheriting it you get all the members of the ArrayList that would not require you to specify the type of an item, like RemoveAt, Clear, etc., while those members that would require a type, like Add, Remove and Item, must be implemented by yourself. Perhaps it's not right for your situation, but it has been created specifically to be a base class for strongly-typed collections so it is appropriate in most cases.
-
Re: Need help with craeting a class with a single function in it.
Quote:
Originally Posted by Wokawidget
ok. I have droped a combo on my form. I have named this cboUsers.
In Form_Load how on earth do I access this???!
and
Both do not work and I get 0 intellisense :(
:mad:
Woka
One thing to remember here. C# is case sensitive.
-
Re: Need help with craeting a class with a single function in it.
Quote:
Originally Posted by jmcilhinney
That's coming from someone with a lot of VB experience so of course it's so. Your average C++ or Java developer would say exactly the opposite. It's like us trying to read Chinese and a Chinese person reading English. Ease comes from experience and familiarity. You may never be as much at ease with the language that you learn last, but that doesn't mean that someone who learned the two the other way around wouldn't feel exactly the opposite.
You are 100% correct on this.
I started with C++ so, for me, looking at syntax from C++ or C# looks natural where as VB seems awkward.
-
Re: Need help with craeting a class with a single function in it.
OK, I have decided to create a collection that inherits CollectionBase, however this is not for dev purposes, but just for my own interest.
I have:
VB Code:
public class MyCollection: CollectionBase
{
public MyCollection()
{
//
// TODO: Add constructor logic here
//
}
public void Add(Object Value)
{
this.List.Add(Value);
}
}
How on earth does this benefit me?
It doesn't as it stands :D
However, I would liketo replicate the good old VB6 collection, and this need to get items by key, or index.
This is my task for thsi morning.
Woof
-
Re: Need help with craeting a class with a single function in it.
Quote:
Originally Posted by mar_zim
One thing to remember here. C# is case sensitive.
Yup, I know...and it's annoying as hell :(
I deletded the control from the form, re-added it...and boooom it worked. Stupid IDE.
Woof
-
Re: Need help with craeting a class with a single function in it.
Like I said, CollectionBase provides to derived classes the members that don't require you to specify the type of the objects in the collection, including RemoveAt, Clear, etc. Members like Add, AddRange, Remove, etc. that do require you to specify the type of the objects are left to you. If you were going to create a collection strongly-typed to your User type from earlier you would do something like this:
Code:
class UserCollection : System.Collections.CollectionBase
{
public int Add(User value)
{
return this.InnerList.Add(value);
}
public void AddRange(User[] c)
{
this.InnerList.AddRange(c);
}
public void Remove(User obj)
{
this.InnerList.Remove(obj);
}
// This is the collection indexer, equivalent to the default property Item.
public User this[int index]
{
get { return this.InnerList[index]; }
set { this.InnerList[index] = value; }
}
}
Note that there are some differences between the List and InnerList properties of the CollectionBase class. I believe List allows you to employ more stringent type checking or something like that. To be honest, I don't know the details. I've always just used InnerList myself.
-
Re: Need help with craeting a class with a single function in it.
I have always used List and am not sure what the differences are either. I use it just like you show but with List.
-
Re: Need help with craeting a class with a single function in it.
Quote:
Originally Posted by RobDog888
I have always used List and am not sure what the differences are either. I use it just like you show but with List.
One reson I don't use List is that it has no AddRange method. The InnerList is an ArrayList reference, while the List is an IList. ArrayList implements IList but adds some of its own functionality, like AddRange. When you use IList the protected methods named "On<something>" are implicitly called, so you can add code there to perform additional actions if required. These methods are not invoked when using InnerList. I guess that means that InnerList is probably preferable unless you want to do something in OnInsert, OnSet, etc.
-
Re: Need help with craeting a class with a single function in it.
That makes sense as I was also using the On events to do validation and a few other things when an item was added to my collection. I had to do a workaround for Sorting the List. Wasnt fun. If I would have used Innerlist I would have had the Sort method. but would have lost the On events. Guess it is a catch 22. :(
-
Re: Need help with craeting a class with a single function in it.
I know about collectionbase, and do use it in VB.NET, I was just trying to keep things simple.
Anyways, here's my collection class. Get item by index or key :D
Code:
using System;
using System.Collections;
namespace BusinessObjects
{
/// <summary>
/// This is my custom collection that inherits CollectionBase
/// </summary>
public class MyCollection: CollectionBase
{
private Hashtable itemsByKey;
private ArrayList keyIndexList;
public MyCollection()
{
itemsByKey = new Hashtable();
keyIndexList = new ArrayList();
}
public Object Item(int index)
{
return(base.List[index]);
}
public Object Item(string key)
{
return itemsByKey[key];
}
public void Add(object newItem)
{
base.List.Add(newItem);
keyIndexList.Add(string.Empty);
}
public void Add(Object newItem, String key)
{
this.List.Add(newItem);
itemsByKey.Add(key,newItem);
keyIndexList.Add(key);
}
public void Remove(int index)
{
object item = base.List[index];
string itemKey = (string)keyIndexList[index];
base.RemoveAt(index);
if (itemKey != string.Empty)
{
itemsByKey.Remove(itemKey);
}
}
public void Remove(string key)
{
int itemIndex = keyIndexList.IndexOf(key);
keyIndexList.Remove(key);
itemsByKey.Remove(key);
base.RemoveAt(itemIndex);
}
}
}
How do u do color codoing?
Wopka
-
Re: Need help with craeting a class with a single function in it.
OK, you didn't mention anything about using keys. Just as CollectionBase exists for creating strongly-typed ArrayLists, so too DictionaryBase exists for creating strongly-typed Hashtables. Further still, Specialized.NameObjectCollectionBase exists for creating strongly-typed collections of objects that can be accessed either by a String key or by index. That sounds a lot like what you're doing, so that should be your base class.
-
Re: Need help with craeting a class with a single function in it.
You should know that one ;)
Code:
public string Msg()
{
return "Woof";
}
public string Msg(string str)
{
return str;
}
Why use an ArrayList and HashTable? Your inheriting from the CollectionBase so could you do without the ArrayList?
-
Re: Need help with craeting a class with a single function in it.
Quote:
Originally Posted by Wokawidget
How do u do color codoing?
I use the VBForums Extension for Firefox. It let's you paste code using syntax highlighting for a variety of languages. I don't approve of all the colour choices but it is more readable.
-
Re: Need help with craeting a class with a single function in it.
Hey John, does woka need both the hashtable and arraylist when inheriting a collectionbase?
Ps, I prefer to color manually.
-
Re: Need help with craeting a class with a single function in it.
Quote:
Originally Posted by RobDog888
Hey John, does woka need both the hashtable and arraylist when inheriting a collectionbase?
As I said, a class derived from CollectionBase is supposed to behave as a strongly-typed ArrayList. There are not supposed to be any keys and the values are supposed to be stored in the internal ArrayList that already exists. There should be no need to declare any additional internal collections. If you want a keyed collection then you should be inheriting a different base class, either DictionaryBase or NameObjectCollectionBase.
-
Re: Need help with craeting a class with a single function in it.
Ah, makes sense now. ;)
Thanks :thumb:
-
Re: Need help with craeting a class with a single function in it.
Quote:
Originally Posted by jmcilhinney
That's coming from someone with a lot of VB experience so of course it's so. Your average C++ or Java developer would say exactly the opposite. It's like us trying to read Chinese and a Chinese person reading English. Ease comes from experience and familiarity. You may never be as much at ease with the language that you learn last, but that doesn't mean that someone who learned the two the other way around wouldn't feel exactly the opposite.
I am a VB6.0 programmer but I love C#, I have no problem with those curly braces, maybe because C was our 'Introductory' subject in College days? :D Or perhaps because I am looking at the Standardized .Net language? I guess it is! :D
-
Re: Need help with craeting a class with a single function in it.
Quote:
Originally Posted by RobDog888
Hey John, does woka need both the hashtable and arraylist when inheriting a collectionbase?
Ps, I prefer to color manually.
Yes it does :D
The Array table stores the KEY against the index :D
Woka
-
Re: Need help with craeting a class with a single function in it.
Quote:
Originally Posted by jmcilhinney
OK, you didn't mention anything about using keys. Just as CollectionBase exists for creating strongly-typed ArrayLists, so too DictionaryBase exists for creating strongly-typed Hashtables. Further still, Specialized.NameObjectCollectionBase exists for creating strongly-typed collections of objects that can be accessed either by a String key or by index. That sounds a lot like what you're doing, so that should be your base class.
Specialized.NameObjectCollectionBase, now you tell me. DOH!
This is what I want.
Right gonna change me code now.
But my collection class above works just hunky dorey ;)
Woof
-
Re: Need help with craeting a class with a single function in it.
"hunky dorey" :lol:
See if I didnt mention it to John in post #31 you would have ended up with a hunky dorey badger messenger C# :D
Meow!
-
Re: Need help with craeting a class with a single function in it.
I'm not really following terribly well but did you consider using one of the generic collections instead?
-
Re: Need help with craeting a class with a single function in it.
generic collections?
I used collectionBase, and inheritted from it. But you can only get an item out by it's index, so I added other array types to handle the keys. it pretty much replicated VB6's collection. However jmc pointed me towards a more advanced collection which I need to look at.
Woof
Woka
-
Re: Need help with craeting a class with a single function in it.
Generics were introduced in VS 2005. I just read in another post that you're using 2003 so it's no use. But essentially they are a means of generating a strongly-typed class at compile-time. all the logic work is handled for you, you just fill in the type name when you instantiate it.
Code:
using System.Collections.Generic;
// ......
List<myClass> myList = new List<myClass>(); // generates a new strongly-typed collection
But yeah ignore that unless you're planning to upgrade to '05 :)
-
Re: Need help with craeting a class with a single function in it.
Arent generics only in 2005? Woof is running 2003, right?
-
Re: Need help with craeting a class with a single function in it.
ahhhh generics, yes I know what they are. But yes, I am using 2003 at the mo on this project. I am using 2005 at home for all furture dev work.
Personally, at this moment in time I prefer creating a collection and sitting it inside a strongly typed class. Black boxing is the way forward ;)
Maybe I am wrong.
Woof
-
Re: Need help with craeting a class with a single function in it.
Quote:
Originally Posted by RobDog888
Arent generics only in 2005? Woof is running 2003, right?
Yes, he already stated they were only in 2005 (C# 2.0)
Quote:
Originally Posted by Wokawidget
Personally, at this moment in time I prefer creating a collection and sitting it inside a strongly typed class. Black boxing is the way forward ;)
Maybe I am wrong.
The problem with that is lots of casting is needed and it will slow performance. It not be that noticable but generics would be much faster.
Unfortuantely they are not quite as advanced as C++ templates. Hopefully that'll change in C# 3.0
-
1 Attachment(s)
Re: Need help with craeting a class with a single function in it.
I don't see any problem in your code. Maybe you should add the ToString() method in the Console.WriteLine(miObjeto.Msg()); line.
It should be like this: Console.WriteLine(miObjeto.Msg().ToString());
If the problem persist, you should comment the line // to check if the error happens there, and if is not, try to comment the code in other place until the error dissapear. If that happen, you should check in that point.
Regards,
Tribo.
-
Re: Need help with craeting a class with a single function in it.
errr. I don't have an erro :confused:
If Msg is a string, then there is no poinbt what so ever in doing .ToString on it...:confused:
Woka
-
Re: Need help with craeting a class with a single function in it.
But if you can't run the program you've got a big problem. Try to download my solution and if still don't run try to install your VS again (because my solution works in my PC).
If it runs, try to look the difference between your code and my code...
Regards,
Tribo.
-
Re: Need help with craeting a class with a single function in it.
Eh? Are you posting in the right thread???
WOka
-
Re: Need help with craeting a class with a single function in it.
Quote:
Originally Posted by Tribo
But if you can't run the program you've got a big problem. Try to download my solution and if still don't run try to install your VS again (because my solution works in my PC).
If it runs, try to look the difference between your code and my code...
Regards,
Tribo.
He doesn't have a problem... lol
-
Re: Need help with craeting a class with a single function in it.
Quote:
Originally Posted by kasracer
He doesn't have a problem... lol
Perhaps not in the code department but... ;)