473,848 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting "object" instance for calling methods

Ok guys, I'm really looking for someone to tell me how bad a hack this
is, and if I'm close to where I should be with it.

The basic situation is that I have a class which creates a basic
calendar control, the only difference is I stole the navigation scheme
from Vista (e.g., if you click on the year you zoom out to the months
list, then out to the decade, and back in when you click a year, then a
month).

I ran into some trouble setting the onclick events dynamically, then did
some reading on why I was getting a "too much recursion" error, which I
fixed. No problem. Now what I have is this:

function VistaCal() {
VistaCal.instan ce = this;
}

and then later on, when I am building the calendar controls:

mNameCell.oncli ck = function(){ updateCal("year ", dateString);};

Where mNameCell is the year header, you click it, it zooms you out to
the list of months.

The updateCal function is outside the prototyping, and uses
VistaCal.instan ce to call the member function I need:

function updateCal(type, date) {
VistaCal.instan ce.displayChang e(type, date);
}

I couldn't find any other way to get a reference to the current
VistaCal, and I looked. I fought with this thing for three hours last
night, and I got it working, but it feels like a hack.

Thoughts?

~A!
Dec 24 '07 #1
4 2185
Peter Michaux said:
I think it is worth programming these silly rollover widgets and then
a tabbed pane (less than 50 lines of code) every time I play with a
new architecture. The investment is small and the insight is worth it.

The second example (functional style) always seems to be shorter and
have more appeal to me but I've only programmed large widgets like the
first example (prototype OOP) as it has seemed easier to reuse code
and to extend the widget to make something like MyFancyRollOver Widget.

Peter
I think that was EXACTLY what I needed, the example of the event handler
in the class. I write big stuff, but I always brute-force my way through
it, trying to pick up from you guys a better way of doing things.

Seems to be working. Thanks, I really appreciate that.

~A!
Dec 24 '07 #2
My Pet Programmer said:
Peter Michaux said:
>I think it is worth programming these silly rollover widgets and then
a tabbed pane (less than 50 lines of code) every time I play with a
new architecture. The investment is small and the insight is worth it.

The second example (functional style) always seems to be shorter and
have more appeal to me but I've only programmed large widgets like the
first example (prototype OOP) as it has seemed easier to reuse code
and to extend the widget to make something like MyFancyRollOver Widget.

Peter

I think that was EXACTLY what I needed, the example of the event handler
in the class. I write big stuff, but I always brute-force my way through
it, trying to pick up from you guys a better way of doing things.

Seems to be working. Thanks, I really appreciate that.

~A!
Oh, if you want to take a look:

http://mypetprogrammer.com/vistacal.html

It's just a little prototype at the moment, I'm working on the
architecture as of the moment I read your post.

~A!
Dec 24 '07 #3
My Pet Programmer wrote:
Ok guys, I'm really looking for someone to tell me how bad
a hack this is, and if I'm close to where I should be with it.

The basic situation is that I have a class which creates a
basic calendar control,
By which the nearest you can mean is that you have an aggregation of
code that attempts to implement the concept of a 'class' in javascript,
as javascript has no 'classes' (or it has only one class; the native
ECMAScript object).
the only difference is I stole the navigation scheme from Vista (e.g.,
if you click on the year you zoom out
to the months list, then out to the decade, and back in
when you click a year, then a month).

I ran into some trouble setting the onclick events
dynamically, then did some reading on why I was
getting a "too much recursion" error, which I fixed.
No problem. Now what I have is this:

function VistaCal() {
VistaCal.instan ce = this;
}
If you assign the - this - value to a property of the constructor inside
the constructor then that means that there can only ever be one instance
of - VistaCal -, and if there can only ever be one instance it is
difficult to see how this can be a implementation of the concept of a
'class' in javascript (as 'classes' are things that are expected to have
multiple instances).
and then later on, when I am building the calendar controls:

mNameCell.oncli ck = function(){ updateCal("year ", dateString);};

Where mNameCell is the year header, you click it, it zooms you
out to the list of months.

The updateCal function is outside the prototyping, and uses
VistaCal.instan ce to call the member function I need:

function updateCal(type, date) {
VistaCal.instan ce.displayChang e(type, date); }

I couldn't find any other way to get a reference to the current
VistaCal, and I looked.
Closures are frequently employed in this context. See:-

<URL: http://jibbering.com/faq/faq_notes/closures.html >
I fought with this thing for three hours last night, and I got it
working, but it feels like a hack.
It would not be anybody's first choice to use a single global reference
to refer back to the object instance. But if there can only be one such
object and it is properly named (Your chosen name is obscure) then that
is not too big a deal. However, from the little code you posted it looks
like the creation of that single object could be much better handled,
and it would need much re-working if there were ever to be more than one
instance of that object.

Richard.

Dec 24 '07 #4
Richard Cornford said:
>
It would not be anybody's first choice to use a single global reference
to refer back to the object instance. But if there can only be one such
object and it is properly named (Your chosen name is obscure) then that
is not too big a deal. However, from the little code you posted it looks
like the creation of that single object could be much better handled,
and it would need much re-working if there were ever to be more than one
instance of that object.

Richard.
Thanks for the extensive feedback, Richard, much obliged. The reason I
posted it here in the first place was because the way I did it there
could only be one, and I knew it was messed up. Luckily a couple of you
guys came along this morning and helped me out, and I'll rework it.

It's actually not a lot of rework that it needs, all the events are
handled from the same spot, so however I fix them there will propagate,
and heck, if it doesn't, I get to learn a bunch more about the technology.

And you'll have to forgive me thinking about things as objects for a
bit, I'm coming from an oo background, and Javascript's more advanced
features are a very different concept.

I really appreciate the response, thank you.

~A!
Dec 24 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
22543
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object instance (in this case 'myDog'). Of course it would be better if I could somehow know from within write() that the name of the object instance was 'myDog' without having to pass it as a parameter. //////////////////////////////// function...
4
5036
by: Tim Johnson | last post by:
Hello All: I would like to expand upon the typeof operator. Using 'typeof' I can distinguish between string and array for instance, because typeof returns "object" for array. How can I programmatically distinguish between an Array and an Option, for instance? Thanks
6
19945
by: Lauchlan M | last post by:
Hi. Usin ASP.NET, getting an "Object reference not set to an instance of an object" error. In my login.aspx page I have: string arrUserRoles = new string {"UserRole"}; Context.Items.Add("UserRoles", arrUserRoles); Context.User = new
3
19727
by: Steve Lutz | last post by:
Hi All, I have a Windows Service that runs well. The service hosts a remote object. The purpose of the object is so that I can "peak" into the service to see what it's doing. I wrote a small Windows Application that connects to the remote object and prints out status information. Everything works fine when I first start the service. However, after some period, when I attempt to connect to the remote object, I get "Requested Service not...
4
5525
by: Piotr Perak | last post by:
Can someone explain the "object instance" term that is used i many of C# books? I have C++ background. In C++ instance and object meant the same. I could say that I have object of some class or instance of some class. There was no object instance used. How can I understand object instance? (I don't mean framweork class named object) -- Piotr (Peri, 3P) Perak
2
1883
by: Andrzej Kaczmarczyk | last post by:
Hi, I have following problem: (warning, quite long post with lots of code) I have an object of a known type like this. class MyClass { public int Property1; public string Property2; }
18
28764
by: Microsoft | last post by:
When I try this in my code I alwas get an errormessage: "Object reference not set to an instance of an object" Dim g As System.Drawing.Graphics g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold), Brushes.Black, 0, 0) Why is this? Marc
7
1314
by: Andy B | last post by:
I have an instance of an object that needs to be accessed by all members of a page like page_load, button_click events and so on. Where in the codebehind would I put the creation of the object instance?
3
2342
by: Sarah | last post by:
Hi - Please be gentle. I am quite new to visual basic, but I have been going through tutorials and reading up. I found a code snippet on the internet that I wanted to see if I could re-purpose for a project, but I keep getting the error: "Object reference not set to an instance of an object" for the 7th line of the code below which is: obj.ConvertPage(URL).Save("C:\screencaptest2.bmp", System.Drawing.Imaging.ImageFormat.Bmp)
0
9735
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10997
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10718
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9497
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7889
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5731
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5917
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4134
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3172
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.