Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Excel > I'm unable to get the return value of func....


Reply
 
Thread Tools Display Modes
  #1  
Old 07-01-2012, 12:37 AM
ecasper ecasper is offline
Newcomer
 
Join Date: Jun 2012
Posts: 1
Default I'm unable to get the return value of func....


Hi, This is the my first post here as I'm started writing my first VBA code

Here is my code:

Module1:
Code:
Function AddABackSlash(tcPath As String) As String

tcPath = Trim(tcPath)

If tcPath = "" Then
    AddBackSlash = ""
End If

If Right(tcPath, 1) = "\" Then
    tcPath = tcPath
Else
    tcPath = tcPath & "\"
End If

AddBackSlash = tcPath

End Function
Module2:
Code:
Public Property Get cAppPath() As String

    cAppPath = Module1.AddABackSlash(ThisWorkbook.Path)

End Property
Now the question is I always get cAppPath property value as "". this must be very basic mistake. please help.. Thanks in advance!
Reply With Quote
  #2  
Old 07-01-2012, 07:46 AM
Gruff's Avatar
Gruff Gruff is offline
Bald Mountain Survivor

Super Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,882
Default

1) Tou are not using "option explicit" at the top of every form, module or class.
If you did vb would let you know you have an undefined variable. You left the "A" out of "Add A BackSlash" inside your function.

2) Properties are defined in classes not modules
3) You do not need a property. Your function can do it all.
4) Unless I am mistaken Excel's Workbook.Path Property always omits the trailing backslash
5) It is a good idea to always declare subs and functions as Public or Prinate. Default is Public but you should still declare it for clarity.
6) Public procedures do not need to be prefixed with the module name.

Code:
Option Explicit Public Function AddABackSlash(tcpath As String) As String tcpath = Trim(tcpath) If tcpath <> "" Then If Right(tcpath, 1) <> "\" Then tcpath = tcpath & "\" End If End If AddABackSlash = tcpath End Function

In use...

Code:
Private Sub Command1_Click() MsgBox AddABackSlash(ThisWorkbook.Path) End Sub

Or directly

Code:
Option Explicit Public Function WB_Path() As String Dim Path As String Path = ThisWorkbook.Path If Path <> "" Then If Right$(Path, 1) <> "\" Then Path = Path & "\" End If End If WB_Path = Path End Function
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 07-01-2012 at 08:22 AM.
Reply With Quote
  #3  
Old 07-02-2012, 12:14 PM
Bob Phillips's Avatar
Bob Phillips Bob Phillips is offline
Contributor
 
Join Date: Jul 2009
Posts: 506
Default

Quote:
Originally Posted by Gruff View Post
2) Properties are defined in classes not modules
Modules can define and use properties as well

Code:
Private myVar As Double Property Get GetVar() As Double GetVar = myVar End Property Property Let SetVar(inVal As Double) Select Case inVal Case Is > 100: myVar = inVal * 4.52 Case Is > 50: myVar = inVal * 3.25 Case Else: myVar = inVal * 2.2 End Select End Property Sub TestProperties() SetVar = 75 MsgBox GetVar End Sub
Reply With Quote
  #4  
Old 07-02-2012, 12:38 PM
Gruff's Avatar
Gruff Gruff is offline
Bald Mountain Survivor

Super Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,882
Default

IMHO Bad practice is bad practice Bob.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #5  
Old 07-03-2012, 09:16 AM
Bob Phillips's Avatar
Bob Phillips Bob Phillips is offline
Contributor
 
Join Date: Jul 2009
Posts: 506
Default

Whilst you will have to explain to me what is bad about that, saying that you think it is bad practice is not the same as saying '... Properties are defined in classes not modules'.
Reply With Quote
  #6  
Old 07-03-2012, 11:59 AM
Gruff's Avatar
Gruff Gruff is offline
Bald Mountain Survivor

Super Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA
Posts: 5,882
Default

Perhaps Bad Practice was too strong. I believe in usng the mainstream tools in all languages for maintainabillity and clarity.
The easiest way to shoot yourself in the foot is to use non explicit, little documented, or hidden features of a language.

If you want to discuss this sort of thing I suggest we take it to a new thread and not hijack this one.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->