[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

cannot get attr_accessor to work

Tom Cloyd

1/21/2009 2:12:00 AM

I've been staring at this for a couple of hours, and I cannot crack the
nut. This SHOULD work, but I'm obviously screwing up. Just cannot see
where. I'm carefully following several examples, but...I cannot get
access to my instance variables.

Here's a stripped down version of the code -

# SetNet.rb

def main
# Set up logging
run_log = Manage_log.new( 'logfile.txt' ).open
log = run_log.log
logging_now = run_log.lgg
# @logging_now = false
# @log, @logging_now = manage_log( 'open' )
end

# Creates program log file, setting default logging level of INFO.
Current logging is appended to existing log content. Closes log when
requested by user.
#
# * _op_ - Requested operation: _open_ or _close
#
class Manage_log
attr_accessor :log, :lgg
def initialize( logFileName )
@logFileName = logFileName
end
def open
#create log file
log_main = File.open( @logFileName, File::WRONLY | File::APPEND )
@log = Logger.new( log_main )
@log.datetime_format = "%Y-%m-%d %H:%M:%S"
@lgg = true

# set logging level
@log.level = Logger::INFO # I.e., only INFO level and above will be
logged
@log.info( '===== START logging' ) # just a program place indicator
puts '> logging started, at INFO level'
end

end

%w(rubygems ruby-debug readline strscan logger fileutils).each{ |lib|
require lib }
Debugger.start
debugger # call to ruby-debug

main

====

Demonstration of the lack of access -

> $ ruby setnet-x.rb
> setnet-x.rb:40
> main
> (rdb:1) b 4
> Breakpoint 1 file setnet-x.rb, line 4
> (rdb:1) c
> Breakpoint 1 at setnet-x.rb:4
> setnet-x.rb:4
> run_log = Manage_log.new( 'logfile.txt' ).open
> (rdb:1) n
> > logging started, at INFO level
> setnet-x.rb:5
> log = run_log.log
> (rdb:1) p run_log.log
> NoMethodError Exception: undefined method `log' for nil:NilClass
> (rdb:1)
I would truly appreciate someone's pointing out the problem here.

Thanks!

Tom

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


14 Answers

David A. Black

1/21/2009 2:18:00 AM

0

Hi --

On Wed, 21 Jan 2009, Tom Cloyd wrote:

> I've been staring at this for a couple of hours, and I cannot crack the nut.
> This SHOULD work, but I'm obviously screwing up. Just cannot see where. I'm
> carefully following several examples, but...I cannot get access to my
> instance variables.
>
> Here's a stripped down version of the code -
>
> # SetNet.rb
>
> def main
> # Set up logging
> run_log = Manage_log.new( 'logfile.txt' ).open
> log = run_log.log
> logging_now = run_log.lgg # @logging_now = false

I haven't read the rest of your code but I'll bet the problem is right
there. Ruby is interpreting log and logging_now as local variables. If
you want to call the methods of those names, you have to do:

self.log = run_log.log
self.logging_now = run_log.lgg

Basically, given any expression that looks like this:

var = value

the parser interprets var as a local variable name. So you have to add
the explicit receiver to achieve the method call.


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

http://www.wis... => Independent, social wishlist management!

Justin Collins

1/21/2009 2:21:00 AM

0

Tom Cloyd wrote:
> I've been staring at this for a couple of hours, and I cannot crack
> the nut. This SHOULD work, but I'm obviously screwing up. Just cannot
> see where. I'm carefully following several examples, but...I cannot
> get access to my instance variables.
>
> Here's a stripped down version of the code -
>
> # SetNet.rb
>
> def main
> # Set up logging
> run_log = Manage_log.new( 'logfile.txt' ).open
> log = run_log.log
> logging_now = run_log.lgg # @logging_now = false
> # @log, @logging_now = manage_log( 'open' )
> end
>
> # Creates program log file, setting default logging level of INFO.
> Current logging is appended to existing log content. Closes log when
> requested by user.
> #
> # * _op_ - Requested operation: _open_ or _close
> #
> class Manage_log attr_accessor :log, :lgg
> def initialize( logFileName )
> @logFileName = logFileName
> end
> def open #create log file
> log_main = File.open( @logFileName, File::WRONLY | File::APPEND )
> @log = Logger.new( log_main )
> @log.datetime_format = "%Y-%m-%d %H:%M:%S"
> @lgg = true
>
> # set logging level
> @log.level = Logger::INFO # I.e., only INFO level and above will be
> logged
> @log.info( '===== START logging' ) # just a program place indicator
> puts '> logging started, at INFO level'
> end
>
> end
>
> %w(rubygems ruby-debug readline strscan logger fileutils).each{ |lib|
> require lib }
> Debugger.start
> debugger # call to ruby-debug
>
> main
>
> ====
>
> Demonstration of the lack of access -
>
>> $ ruby setnet-x.rb
>> setnet-x.rb:40
>> main
>> (rdb:1) b 4
>> Breakpoint 1 file setnet-x.rb, line 4
>> (rdb:1) c
>> Breakpoint 1 at setnet-x.rb:4
>> setnet-x.rb:4
>> run_log = Manage_log.new( 'logfile.txt' ).open
>> (rdb:1) n
>> > logging started, at INFO level
>> setnet-x.rb:5
>> log = run_log.log
>> (rdb:1) p run_log.log
>> NoMethodError Exception: undefined method `log' for nil:NilClass
>> (rdb:1)
> I would truly appreciate someone's pointing out the problem here.
>
> Thanks!
>
> Tom
>


Take a look at ManageLog#open. The last expression is a call to puts,
which returns nil. You are setting the variable "run_log" to the result
of the call to open in the debugger, but that result is nil. Then you
try to call "log" on nil, thus the error.

-Justin

Tom Cloyd

1/21/2009 2:27:00 AM

0

David A. Black wrote:
> Hi --
>
> On Wed, 21 Jan 2009, Tom Cloyd wrote:
>
>> I've been staring at this for a couple of hours, and I cannot crack
>> the nut. This SHOULD work, but I'm obviously screwing up. Just cannot
>> see where. I'm carefully following several examples, but...I cannot
>> get access to my instance variables.
>>
>> Here's a stripped down version of the code -
>>
>> # SetNet.rb
>>
>> def main
>> # Set up logging
>> run_log = Manage_log.new( 'logfile.txt' ).open
>> log = run_log.log
>> logging_now = run_log.lgg # @logging_now = false
>
> I haven't read the rest of your code but I'll bet the problem is right
> there. Ruby is interpreting log and logging_now as local variables. If
> you want to call the methods of those names, you have to do:
>
> self.log = run_log.log
> self.logging_now = run_log.lgg
>
> Basically, given any expression that looks like this:
>
> var = value
>
> the parser interprets var as a local variable name. So you have to add
> the explicit receiver to achieve the method call.
>
>
> David
>
David,

Thank for your reply, but I'm puzzled by it. Doesn't the code in my
previous post make it clear that I'm dealing with instance variables?
The class instance initiates them, e.g., @log, and I'm trying to get
that with the run_log.log call. This is NOT an attempt to call a method.
I very carefully copied (I thought) the pattern I saw in several
authoritative sources, but it doesn't work for me, which is nuts.

I hope this makes sense.

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Tom Cloyd

1/21/2009 2:34:00 AM

0

Justin Collins wrote:
> Tom Cloyd wrote:
>> I've been staring at this for a couple of hours, and I cannot crack
>> the nut. This SHOULD work, but I'm obviously screwing up. Just cannot
>> see where. I'm carefully following several examples, but...I cannot
>> get access to my instance variables.
>>
>> Here's a stripped down version of the code -
>>
>> # SetNet.rb
>>
>> def main
>> # Set up logging
>> run_log = Manage_log.new( 'logfile.txt' ).open
>> log = run_log.log
>> logging_now = run_log.lgg # @logging_now = false
>> # @log, @logging_now = manage_log( 'open' )
>> end
>>
>> # Creates program log file, setting default logging level of INFO.
>> Current logging is appended to existing log content. Closes log when
>> requested by user.
>> #
>> # * _op_ - Requested operation: _open_ or _close
>> #
>> class Manage_log attr_accessor :log, :lgg
>> def initialize( logFileName )
>> @logFileName = logFileName
>> end
>> def open #create log file
>> log_main = File.open( @logFileName, File::WRONLY | File::APPEND )
>> @log = Logger.new( log_main )
>> @log.datetime_format = "%Y-%m-%d %H:%M:%S"
>> @lgg = true
>>
>> # set logging level
>> @log.level = Logger::INFO # I.e., only INFO level and above will
>> be logged
>> @log.info( '===== START logging' ) # just a program place indicator
>> puts '> logging started, at INFO level'
>> end
>>
>> end
>>
>> %w(rubygems ruby-debug readline strscan logger fileutils).each{ |lib|
>> require lib }
>> Debugger.start
>> debugger # call to ruby-debug
>>
>> main
>>
>> ====
>>
>> Demonstration of the lack of access -
>>
>>> $ ruby setnet-x.rb
>>> setnet-x.rb:40
>>> main
>>> (rdb:1) b 4
>>> Breakpoint 1 file setnet-x.rb, line 4
>>> (rdb:1) c
>>> Breakpoint 1 at setnet-x.rb:4
>>> setnet-x.rb:4
>>> run_log = Manage_log.new( 'logfile.txt' ).open
>>> (rdb:1) n
>>> > logging started, at INFO level
>>> setnet-x.rb:5
>>> log = run_log.log
>>> (rdb:1) p run_log.log
>>> NoMethodError Exception: undefined method `log' for nil:NilClass
>>> (rdb:1)
>> I would truly appreciate someone's pointing out the problem here.
>>
>> Thanks!
>>
>> Tom
>>
>
>
> Take a look at ManageLog#open. The last expression is a call to puts,
> which returns nil. You are setting the variable "run_log" to the
> result of the call to open in the debugger, but that result is nil.
> Then you try to call "log" on nil, thus the error.
>
> -Justin
>
>
Well, nuts. That's crystal clear. Man, it's tough being a Ruby amateur,
when it 's not great fun, which it hasn't been for a couple of hours.

Thanks Justin. Much appreciated.

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Justin Collins

1/21/2009 2:43:00 AM

0

Tom Cloyd wrote:
> Justin Collins wrote:
>> Tom Cloyd wrote:
>>> I've been staring at this for a couple of hours, and I cannot crack
>>> the nut. This SHOULD work, but I'm obviously screwing up. Just
>>> cannot see where. I'm carefully following several examples, but...I
>>> cannot get access to my instance variables.
>>>
>>> Here's a stripped down version of the code -
>>>
>>>
<snip>
>>>> (rdb:1) p run_log.log
>>>> NoMethodError Exception: undefined method `log' for nil:NilClass
>>>> (rdb:1)
>>> I would truly appreciate someone's pointing out the problem here.
>>>
>>> Thanks!
>>>
>>> Tom
>>>
>>
>>
>> Take a look at ManageLog#open. The last expression is a call to puts,
>> which returns nil. You are setting the variable "run_log" to the
>> result of the call to open in the debugger, but that result is nil.
>> Then you try to call "log" on nil, thus the error.
>>
>> -Justin
>>
>>
> Well, nuts. That's crystal clear. Man, it's tough being a Ruby
> amateur, when it 's not great fun, which it hasn't been for a couple
> of hours.
>
> Thanks Justin. Much appreciated.
>
> t.


Just remember that whenever you see

NoMethodError Exception: undefined method `..." for nil:NilClass

look at what variable you are calling the method on. Then figure out why
it is nil.

-Justin

David A. Black

1/21/2009 2:48:00 AM

0

Hi --

On Wed, 21 Jan 2009, Tom Cloyd wrote:

> David A. Black wrote:
>> Hi --
>>
>> On Wed, 21 Jan 2009, Tom Cloyd wrote:
>>
>>> I've been staring at this for a couple of hours, and I cannot crack the
>>> nut. This SHOULD work, but I'm obviously screwing up. Just cannot see
>>> where. I'm carefully following several examples, but...I cannot get access
>>> to my instance variables.
>>>
>>> Here's a stripped down version of the code -
>>>
>>> # SetNet.rb
>>>
>>> def main
>>> # Set up logging
>>> run_log = Manage_log.new( 'logfile.txt' ).open
>>> log = run_log.log
>>> logging_now = run_log.lgg # @logging_now = false
>>
>> I haven't read the rest of your code but I'll bet the problem is right
>> there. Ruby is interpreting log and logging_now as local variables. If
>> you want to call the methods of those names, you have to do:
>>
>> self.log = run_log.log
>> self.logging_now = run_log.lgg
>>
>> Basically, given any expression that looks like this:
>>
>> var = value
>>
>> the parser interprets var as a local variable name. So you have to add
>> the explicit receiver to achieve the method call.
>>
>>
>> David
>>
> David,
>
> Thank for your reply, but I'm puzzled by it. Doesn't the code in my previous
> post make it clear that I'm dealing with instance variables? The class
> instance initiates them, e.g., @log, and I'm trying to get that with the
> run_log.log call. This is NOT an attempt to call a method. I very carefully
> copied (I thought) the pattern I saw in several authoritative sources, but it
> doesn't work for me, which is nuts.
>
> I hope this makes sense.

The code I saw was:

>>> run_log = Manage_log.new( 'logfile.txt' ).open
>>> log = run_log.log
>>> logging_now = run_log.lgg # @logging_now = false

which are local variable assignments, and you'd mentioned
attr_accessor, so I assumed you had attr_accessor somewhere in a part
of the code you hadn't posted. It's a common error to do:

class C
attr_accessor :name
def initialize(name)
name = name # should be @name or self.name
end
end

so I surmised that that was what was going on. Oh well -- can't hurt
to see that particular potential problem anyway :-)

I'm still not sure where attr_accessor fits in.


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

http://www.wis... => Independent, social wishlist management!

Tom Cloyd

1/21/2009 3:02:00 AM

0

David A. Black wrote:
> Hi --
>
> On Wed, 21 Jan 2009, Tom Cloyd wrote:
>
>> David A. Black wrote:
>>> Hi --
>>>
>>> On Wed, 21 Jan 2009, Tom Cloyd wrote:
>>>
>>>> I've been staring at this for a couple of hours, and I cannot crack
>>>> the nut. This SHOULD work, but I'm obviously screwing up. Just
>>>> cannot see where. I'm carefully following several examples, but...I
>>>> cannot get access to my instance variables.
>>>>
>>>> Here's a stripped down version of the code -
>>>>
>>>> # SetNet.rb
>>>>
>>>> def main
>>>> # Set up logging
>>>> run_log = Manage_log.new( 'logfile.txt' ).open
>>>> log = run_log.log
>>>> logging_now = run_log.lgg # @logging_now = false
>>>
>>> I haven't read the rest of your code but I'll bet the problem is right
>>> there. Ruby is interpreting log and logging_now as local variables. If
>>> you want to call the methods of those names, you have to do:
>>>
>>> self.log = run_log.log
>>> self.logging_now = run_log.lgg
>>>
>>> Basically, given any expression that looks like this:
>>>
>>> var = value
>>>
>>> the parser interprets var as a local variable name. So you have to add
>>> the explicit receiver to achieve the method call.
>>>
>>>
>>> David
>>>
>> David,
>>
>> Thank for your reply, but I'm puzzled by it. Doesn't the code in my
>> previous post make it clear that I'm dealing with instance variables?
>> The class instance initiates them, e.g., @log, and I'm trying to get
>> that with the run_log.log call. This is NOT an attempt to call a
>> method. I very carefully copied (I thought) the pattern I saw in
>> several authoritative sources, but it doesn't work for me, which is
>> nuts.
>>
>> I hope this makes sense.
>
> The code I saw was:
>
>>>> run_log = Manage_log.new( 'logfile.txt' ).open
>>>> log = run_log.log
>>>> logging_now = run_log.lgg # @logging_now = false
>
> which are local variable assignments, and you'd mentioned
> attr_accessor, so I assumed you had attr_accessor somewhere in a part
> of the code you hadn't posted. It's a common error to do:
>
> class C
> attr_accessor :name
> def initialize(name)
> name = name # should be @name or self.name
> end
> end
>
> so I surmised that that was what was going on. Oh well -- can't hurt
> to see that particular potential problem anyway :-)
>
> I'm still not sure where attr_accessor fits in.
>
>
> David
>
Sorry you got a partial copy of the code I sent. It rather sounded like
that was what happened. I DO appreciate that you responded so quickly.
You've certainly been very helpful to me on a number of occasions. (And
I require that help, at times, if I'm get anything much accomplished in
Ruby, in the time I have!).

Thanks.

Tom

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


David A. Black

1/21/2009 3:08:00 AM

0

Hi --

On Wed, 21 Jan 2009, Tom Cloyd wrote:

> David A. Black wrote:
>> Hi --
>>
>> On Wed, 21 Jan 2009, Tom Cloyd wrote:
>>
>>> David A. Black wrote:
>>>> Hi --
>>>>
>>>> On Wed, 21 Jan 2009, Tom Cloyd wrote:
>>>>
>>>>> I've been staring at this for a couple of hours, and I cannot crack the
>>>>> nut. This SHOULD work, but I'm obviously screwing up. Just cannot see
>>>>> where. I'm carefully following several examples, but...I cannot get
>>>>> access to my instance variables.
>>>>>
>>>>> Here's a stripped down version of the code -
>>>>>
>>>>> # SetNet.rb
>>>>>
>>>>> def main
>>>>> # Set up logging
>>>>> run_log = Manage_log.new( 'logfile.txt' ).open
>>>>> log = run_log.log
>>>>> logging_now = run_log.lgg # @logging_now = false
>>>>
>>>> I haven't read the rest of your code but I'll bet the problem is right
>>>> there. Ruby is interpreting log and logging_now as local variables. If
>>>> you want to call the methods of those names, you have to do:
>>>>
>>>> self.log = run_log.log
>>>> self.logging_now = run_log.lgg
>>>>
>>>> Basically, given any expression that looks like this:
>>>>
>>>> var = value
>>>>
>>>> the parser interprets var as a local variable name. So you have to add
>>>> the explicit receiver to achieve the method call.
>>>>
>>>>
>>>> David
>>>>
>>> David,
>>>
>>> Thank for your reply, but I'm puzzled by it. Doesn't the code in my
>>> previous post make it clear that I'm dealing with instance variables? The
>>> class instance initiates them, e.g., @log, and I'm trying to get that with
>>> the run_log.log call. This is NOT an attempt to call a method. I very
>>> carefully copied (I thought) the pattern I saw in several authoritative
>>> sources, but it doesn't work for me, which is nuts.
>>>
>>> I hope this makes sense.
>>
>> The code I saw was:
>>
>>>>> run_log = Manage_log.new( 'logfile.txt' ).open
>>>>> log = run_log.log
>>>>> logging_now = run_log.lgg # @logging_now = false
>>
>> which are local variable assignments, and you'd mentioned
>> attr_accessor, so I assumed you had attr_accessor somewhere in a part
>> of the code you hadn't posted. It's a common error to do:
>>
>> class C
>> attr_accessor :name
>> def initialize(name)
>> name = name # should be @name or self.name
>> end
>> end
>>
>> so I surmised that that was what was going on. Oh well -- can't hurt
>> to see that particular potential problem anyway :-)
>>
>> I'm still not sure where attr_accessor fits in.
>>
>>
>> David
>>
> Sorry you got a partial copy of the code I sent. It rather sounded like that
> was what happened.

I think I got it all (the bit I quoted was just a few lines of it),
but I surmised too quickly what your problem was. I'm STILL not sure
where attr_accessor fits in :-) (You're not calling it anywhere, are
you?) But it sounds like you got the problem resolved.

> I DO appreciate that you responded so quickly. You've certainly
> been very helpful to me on a number of occasions. (And I require
> that help, at times, if I'm get anything much accomplished in Ruby,
> in the time I have!).

Glad to be of help!


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

http://www.wis... => Independent, social wishlist management!

Tom Cloyd

1/22/2009 6:37:00 AM

0

Justin Collins wrote:
> Tom Cloyd wrote:
>> Justin Collins wrote:
>>> Tom Cloyd wrote:
>>>> I've been staring at this for a couple of hours, and I cannot crack
>>>> the nut. This SHOULD work, but I'm obviously screwing up. Just
>>>> cannot see where. I'm carefully following several examples, but...I
>>>> cannot get access to my instance variables.
>>>>
>>>> Here's a stripped down version of the code -
>>>>
>>>>
> <snip>
>>>>> (rdb:1) p run_log.log
>>>>> NoMethodError Exception: undefined method `log' for nil:NilClass
>>>>> (rdb:1)
>>>> I would truly appreciate someone's pointing out the problem here.
>>>>
>>>> Thanks!
>>>>
>>>> Tom
>>>>
>>>
>>>
>>> Take a look at ManageLog#open. The last expression is a call to
>>> puts, which returns nil. You are setting the variable "run_log" to
>>> the result of the call to open in the debugger, but that result is
>>> nil. Then you try to call "log" on nil, thus the error.
>>>
>>> -Justin
>>>
>>>
>> Well, nuts. That's crystal clear. Man, it's tough being a Ruby
>> amateur, when it 's not great fun, which it hasn't been for a couple
>> of hours.
>>
>> Thanks Justin. Much appreciated.
>>
>> t.
>
>
> Just remember that whenever you see
>
> NoMethodError Exception: undefined method `..." for nil:NilClass
>
> look at what variable you are calling the method on. Then figure out
> why it is nil.
>
> -Justin
>
>
Got it! Makes total sense (in retrospect).

Tks,

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Peter T

12/16/2009 8:39:00 PM

0

Maybe something like

n = Val(Replace("ws.CodeName", "Sheet", ""))
or
n = val(Mid$("ws.CodeName",6,5)

If n = 0 then
?
elseif n <= 25 then
Set pWS = Sheet51
elseif n <= 50 then
Set pWS = Sheet52
else
?
end if

Regards,
Peter T


"Robert Crandal" <nobody@gmail.com> wrote in message
news:q6bWm.2764$2A7.866@newsfe07.iad...
> Sure, I'll try to elaborate on my "Do stuff" code.
>
> Basically, if the "ws.CodeName" is between Sheet1 and Sheet25,
> then the "Do stuff" is as follows:
>
> Set pWS = Sheet51
>
> Then, if "ws.CodeName" is between Sheet26 and Sheet50, then
> the "Do stuff" is:
>
> Set pWS = Sheet52
>
> It would be nice if I could use the "Select Case" statement as
> follows (but I don't think it will work):
>
> Select Case ws.CodeName
>
> Case "Sheet1" to "Sheet25":
>
> Set pWS = Sheet51
>
> Case "Sheet26" to "Sheet50":
>
> Set pWS = Sheet52
>
> End Select
>
>
>
> "Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message
> news:%23$7IAhofKHA.5036@TK2MSFTNGP04.phx.gbl...
>> Whether you can shorten that up or not is highly dependent on what each
>> of the "Do stuff" are. If the code for them is all the same except for
>> the CodeName reference, then you can shorten this up dramatically... if
>> they are differences other than in the CodeName, then it depends on what
>> those differences are as to whether you can simplify the code or not...
>> if there is some serial regularity about numbers that appear in them,
>> then maybe a mathematical expression can be used to simplify the loops...
>> but if the "Do stuff" is wildly different from each other, then you have
>> to specify each one separately. We would have to see the "Do stuff" to
>> decide.
>>
>> --
>> Rick (MVP - Excel)
>>
>