Results 1 to 5 of 5

Thread: property from c# to vb.net

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405

    property from c# to vb.net

    I'm trying to port this property to vb.net.
    but i don't know what the
    Code:
    this[int index]
    bit is doing. please help.

    Code:
    		public virtual User this[int index] 
    		{
    			get 
    			{
    				if (!((User)this.List[index]).IsLoaded)
    				{
    					((User)this.List[index]).Load();
    				}
    				return (User) this.List[index];
    			}
    			set 
    			{
    				this.List[index] = value;
    			}
    		}

  2. #2
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Post Re: property from c# to vb.net

    I just put the C# code through the C# to VB.NET converter, and this is what it created:
    VB Code:
    1. Public Overridable User Property index)() As Me(int
    2.     Get
    3.                 If Not (CType(Me.List(index),User)).IsLoaded Then
    4.                     (CType(Me.List(index), User)).Load()
    5.                 End If
    6.                 ReturnCType(Me.List(index), User)
    7.     End Get
    8.     Set (ByVal Value As Me(int)
    9.                 Me.List(index) = value
    10.     End Set
    11. End Property
    ~Peter


  3. #3
    Fanatic Member
    Join Date
    May 2002
    Posts
    746

    Re: property from c# to vb.net

    'This' is synonymous with 'Me'. Brackets [] are used for arrays*, so the property looks to be an index to a collection class. That is, if i have a People class housing Person objects,
    Code:
     
    public Person this[int index]
    will return the Person object associated w/ the supplied index.

    * As an aside, this is one of the places where VB is just retarded. () for arrays/indices and functions/parameters makes no sense at all. They do different things - make them look different. You may now return to your scheduled programming.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: property from c# to vb.net

    It's equivalent to setting the Item property as Default in vb.net

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405

    Re: property from c# to vb.net

    Thanks to all for the replies.
    Sorry to appear dense, but what would be the equivalent in vb.net, can i use the output from the c# tovb.net convert program.

    i did some googling on this last nigth and it said something about the proeprty being an indexer, which don't exist in vb.net

    or do i just need to set the item property to default?

    i'd really like a code example if possible. if it makes it easier here is the class that the property belongs to:

    Code:
    using System;
    using System.Collections;
    
    namespace BVA.ActiveDirectory.Navigator.Objects
    {
    	/// <summary>
    	/// Collection of users.
    	/// </summary>
    	public class UserCollection : CollectionBase 
    	{
    		/// <summary>
    		/// This property indicates if the collection is fixed.
    		/// </summary>
    		public virtual bool IsFixed 
    		{
    			get 
    			{
    				return false;
    			}
    		}
        
    		/// <summary>
    		/// Indicates if the collection is read only.
    		/// </summary>
    		public virtual bool IsReadOnly 
    		{
    			get 
    			{
    				return false;
    			}
    		}
        
    		/// <summary>
    		/// Enumerator for this collection.
    		/// </summary>
    		public virtual User this[int index] 
    		{
    			get 
    			{
    				if (!((User)this.List[index]).IsLoaded)
    				{
    					((User)this.List[index]).Load();
    				}
    				return (User) this.List[index];
    			}
    			set 
    			{
    				this.List[index] = value;
    			}
    		}
    
    		/// <summary>
    		/// Enumerator for this collection. This enumerator gets an item through its name.
    		/// </summary>
    		public virtual User this[string Username] 
    		{
    			get 
    			{
    				int index = -1;
    				for (int i=0; i<this.List.Count; i++)
    				{
    					if (((User)this.List[i]).UserName==Username)
    					{
    						index = i;
    					}
    				}
    				if (index==-1){throw new NullReferenceException ("The User " + Username + " was not found.");}
    				if (!((User)this.List[index]).IsLoaded)
    				{
    					((User)this.List[index]).Load();
    				}
    				return (User) this.List[index];
    			}
    			set 
    			{
    				int index = -1;
    				for (int i=0; i<this.List.Count; i++)
    				{
    					if (((User)this.List[i]).UserName==Username)
    					{
    						index = i;
    					}
    				}
    				if (index==-1){throw new NullReferenceException ("The User " + Username + " was not found.");}
    				this.List[index] = value;
    			}
    		}
        
    		/// <summary>
    		/// Returns the index (position) of an item in the collection.
    		/// </summary>
    		/// <param name="item">Item to be searched.</param>
    		/// <returns>Returns the index(position) of the item.</returns>
    		public virtual int IndexOf(User item) 
    		{
    			return this.List.IndexOf(item);
    		}
        
    		/// <summary>
    		/// This method appends an item to the collection.
    		/// </summary>
    		/// <param name="item">Item to be appended to the collection.</param>
    		/// <returns>Returns an integer indicating if the item was added.</returns>
    		public virtual int Add(User item) 
    		{
    			return this.List.Add(item);
    		}
        
    		/// <summary>
    		/// Removes an item from the collection.
    		/// </summary>
    		/// <param name="item">Item to be removed from the collection.</param>
    		public virtual void Remove(User item) 
    		{
    			this.List.Remove(item);
    		}
        
    		/// <summary>
    		/// Copies an array to another.
    		/// </summary>
    		/// <param name="array">Array to be copied.</param>
    		/// <param name="index">Index to begin the copy.</param>
    		public virtual void CopyTo(Array array, int index) 
    		{
    			this.List.CopyTo(array, index);
    		}
        
    		/// <summary>
    		/// This methoid adds one collection to this collection.
    		/// </summary>
    		/// <param name="collection">Collection to be added.</param>
    		public virtual void AddRange(UserCollection collection) 
    		{
    			this.InnerList.AddRange(collection);
    		}
        
    		/// <summary>
    		/// This methoid adds one collection to this collection.
    		/// </summary>
    		/// <param name="collection">Collection to be added.</param>
    		public virtual void AddRange(User[] collection) 
    		{
    			this.InnerList.AddRange(collection);
    		}
        
    		/// <summary>
    		/// This method tells if the collection contains the item.
    		/// </summary>
    		/// <param name="item">Item to be tested.</param>
    		/// <returns>Returns true if the collection contains the item.</returns>
    		public virtual bool Contains(User item) 
    		{
    			return this.List.Contains(item);
    		}
        
    		/// <summary>
    		/// Inserts an item in the collection.
    		/// </summary>
    		/// <param name="index">Position to be inserted.</param>
    		/// <param name="item">Item to be inserted.</param>
    		public virtual void Insert(int index, User item) 
    		{
    			this.List.Insert(index, item);
    		}
    	}
    }

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