Page 1 of 2 12 LastLast
Results 1 to 40 of 49

Thread: Need help with craeting a class with a single function in it.

  1. #1

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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:
    1. Public Class Users()
    2.  
    3.    Public Function Msg() As String
    4.  
    5.       Return "Woof"
    6.  
    7.    End Function
    8.  
    9.  
    10. End Class
    Then in UI:
    VB Code:
    1. Dim MyObj As New Users
    2.  
    3.    Console.WriteLine(MyObj.Msg)
    Simple and to the point.

    The mess I have in c# is:
    VB Code:
    1. public class Users
    2.     {
    3.         public Users()
    4.         {
    5.             ///What on earth is this for?! Obvious isn't it...BAH!
    6.             ///Pointless...???
    7.         }
    8.  
    9.         public string Msg()
    10.         {
    11.             return "Woof";
    12.         }
    13.  
    14.        
    15.     }
    Then in UI:
    VB Code:
    1. Users MyObject = new Users();
    2.             System.Console.WriteLine(MyObject.Msg());

    What actually happens in the .Msg in the UI gets a squiggley line and the error is:
    Method 'Users.Msg()' referenced without parentheses
    Wot on earth does that mean?!

    I am tired and stressed

    Woka

  2. #2
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    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

    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?
    Last edited by Kasracer; Feb 1st, 2006 at 04:41 PM.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Need help with craeting a class with a single function in it.

    It was probably the name "Users".
    Attached Files Attached Files
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690

    Re: Need help with craeting a class with a single function in it.

    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

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Re: Need help with craeting a class with a single function in it.

    thanks all. Woohoooo I have written a parent child class
    Wossy helped loads with this too, even though he thinks c# rocks
    VB Code:
    1. using System;
    2. using System.Collections;
    3.  
    4. namespace BusinessObjects
    5. {
    6.     public class Users
    7.     {
    8.         private ArrayList items = new ArrayList();
    9.  
    10.         public Users()
    11.         {
    12.             ///???
    13.         }
    14.  
    15.         public User Item(int Index)
    16.         {
    17.             return (User)items[Index];
    18.         }
    19.        
    20.         public int Count
    21.         {
    22.             get
    23.             {
    24.                 return items.Count;
    25.             }
    26.         }
    27.  
    28.         public void AddUser(User NewUser)
    29.         {  
    30.             items.Add(NewUser);
    31.         }
    32.     }
    33.  
    34.     public class User
    35.     {
    36.         private string username = string.Empty;
    37.         private string password = string.Empty;
    38.        
    39.         public User()
    40.         {
    41.             //create blank user class
    42.         }
    43.  
    44.         public User(string Username, string Password)
    45.         {
    46.             username = Username;
    47.             password = Password;
    48.         }
    49.  
    50.         public string Username
    51.         {
    52.             get
    53.             {
    54.                 return username;           
    55.             }
    56.  
    57.             set
    58.             {
    59.                 username = value;
    60.             }
    61.         }
    62.  
    63.         public string Password
    64.         {
    65.             get
    66.             {
    67.                 return password;           
    68.             }
    69.  
    70.             set
    71.             {
    72.                 password = value;
    73.             }
    74.         }
    75.     }
    76. }
    then to use:
    VB Code:
    1. Users MyObjects = new Users();
    2.  
    3. User MyObject = new User("Woka", "Password");
    4. MyObjects.AddUser(MyObject);
    5.  
    6. MyObject = new User("Wossy","HatesVB");
    7. MyObjects.AddUser(MyObject);
    8.  
    9.            
    10. MyObject = new User("RobDog","Moose");
    11. MyObjects.AddUser(MyObject);
    12.  
    13. System.Console.WriteLine(MyObjects.Item(2).Username);
    Am a happy badger

    Woof

  7. #7

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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

  8. #8
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.
    I don't live here any more.

  9. #9
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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
    I don't live here any more.

  10. #10

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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???!
    VB Code:
    1. cboUsers.???
    and
    VB Code:
    1. this.cboUsers.???
    Both do not work and I get 0 intellisense



    Woka

  11. #11

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. using System;
    2.  
    3. namespace Test_CS
    4. {
    5.     public class User
    6.     {
    7.         public User()
    8.         {
    9.             //
    10.             // TODO: Add constructor logic here
    11.             //
    12.         }
    13.         public string Msg()
    14.         {
    15.             return "Woof";
    16.         }
    17.         public string Msg(string str)
    18.         {
    19.             return str;
    20.         }
    21.  
    22.     }
    23. }
    Then to invoke it ...
    VB Code:
    1. private void Form1_Load(object sender, System.EventArgs e)
    2.         {
    3.             User MyObj = new User();
    4.             ///System.Console.WriteLine(MyObj.Msg());
    5.             MessageBox.Show(MyObj.Msg("Ruff!")); ///Ruff!
    6.             MessageBox.Show(MyObj.Msg()); ///Woof!
    7.         }
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    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.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.

    Yes, it should be Overloads
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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

  17. #17
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Need help with craeting a class with a single function in it.

    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".
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  18. #18
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  19. #19
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    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???!
    VB Code:
    1. cboUsers.???
    and
    VB Code:
    1. this.cboUsers.???
    Both do not work and I get 0 intellisense



    Woka

    One thing to remember here. C# is case sensitive.

  20. #20
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    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.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  21. #21

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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:
    1. public class MyCollection: CollectionBase
    2.     {
    3.         public MyCollection()
    4.         {
    5.             //
    6.             // TODO: Add constructor logic here
    7.             //
    8.         }
    9.  
    10.         public void Add(Object Value)
    11.         {
    12.             this.List.Add(Value);
    13.         }
    14.     }
    How on earth does this benefit me?

    It doesn't as it stands

    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

  22. #22

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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

  23. #23
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  24. #24
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  25. #25
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  26. #26
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  27. #27

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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
    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

  28. #28
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  29. #29
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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?

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  30. #30
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  31. #31
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  32. #32
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  33. #33
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Need help with craeting a class with a single function in it.

    Ah, makes sense now.

    Thanks
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  34. #34
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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? Or perhaps because I am looking at the Standardized .Net language? I guess it is!
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  35. #35

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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
    The Array table stores the KEY against the index

    Woka

  36. #36

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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

  37. #37
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Need help with craeting a class with a single function in it.

    "hunky dorey"

    See if I didnt mention it to John in post #31 you would have ended up with a hunky dorey badger messenger C#

    Meow!
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  38. #38
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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?

  39. #39

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    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

  40. #40
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width