|
-
Jan 27th, 2004, 12:11 PM
#1
Thread Starter
Fanatic Member
Call a class from Main form?
I sure this is basic stuff and I just did it a couple of semesters ago, but I can't remember how to do it.
I have a main form and I want to call subs from a class.vb that I copied from something else. Right now there is a sub called:
Private sub mnuClickme(..........stuff)
How do I call this from the main form?
Thanks!
-
Jan 27th, 2004, 03:05 PM
#2
Frenzied Member
I think there's a couple things going on. Normally, you create an instance of your class, then you can call it's methods. If the method is shared then you don't need an instance.
But since you method is private, even if you create an instance, it won't be able to see the method.
-
Jan 27th, 2004, 05:23 PM
#3
you can set private methods in another Class / Form , here's a quick example i knocked up for you ...
VB Code:
'/// the Class ....
Public Class Class1
'/// a private sub ( not accessable from outside the Class ? nope ;)
Private Sub tester()
MessageBox.Show("testing 123")
End Sub
End Class
'///
'/// to invoke the Private Sub from a form ...
Dim cls As New Class1()
cls.GetType.InvokeMember("tester", Reflection.BindingFlags.InvokeMethod Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic, Nothing, cls, Nothing)
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Jan 27th, 2004, 05:29 PM
#4
Frenzied Member
Interesting - didn't know that. What good does private do then? Just makes it more difficult to call?
-
Jan 27th, 2004, 05:38 PM
#5
yup , but where there's a will there's a way
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Jan 27th, 2004, 05:46 PM
#6
Accessing Private members via reflection is how Binary Serialization works. Did you ever notice it saves your private parts as well? Although I think his main issue will be the Instance bit.
-
Jan 27th, 2004, 05:59 PM
#7
PowerPoster
GOSH!!!!!!!!
Did you also know that the third hair from the left of a Rottweiller's right eybrow is colored brown?
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jan 27th, 2004, 06:06 PM
#8
Originally posted by taxes
GOSH!!!!!!!!
Did you also know that the third hair from the left of a Rottweiller's right eybrow is colored brown?
Brown I thought it was black. This changes everything!
-
Jan 27th, 2004, 11:41 PM
#9
Thread Starter
Fanatic Member
Ok, I guess I don't get it. I basically have a form called frmMain. I added a class file to the project called Effects.vb. Right now it contains this:
Public Class Effects
Dim frmMain As New frmMain
Public Sub FlipVertically()
picOriginal.Image.RotateFlip(RotateFlipType.RotateNoneFlipY)
picOriginal.Invalidate()
End Sub
End Class
The picOriginal is a picture box on frmMain and I will (hopefully) be calling the sub routine with a button click. Can you give me a simplistic example using the above? I'm sure what I have above is not completely correct. I am afraid the instructor we had was an adjunct and believe it or not has NO experience with .NET and from what I could tell...an extremely limited knowledge of VB despite this being an Advanced class. What a rip off.
Thanks!
-
Jan 28th, 2004, 04:39 AM
#10
PowerPoster
Hi birthjay,
This subject has been fully covered in the thread
http://www.vbforums.com/showthread.p...hreadid=276165
However, your code:
"Public Class Effects
Dim frmMain As New frmMain
T
Public Sub FlipVertically()
picOriginal.Image.RotateFlip(RotateFlipType.RotateNoneFlipY)
picOriginal.Invalidate()
End Sub
End Class
he picOriginal is a picture box on frmMain and I will (hopefully) be calling the sub routine with a button click."
is not, in my humble opinion, not suitable. E.G. If your frmMain was instanced (opened) before Effects, you would simply be creating another entirely separate frmMain with the same name! - so remove the declaration from Effects. I would have thought you should work from frmMain from a button routine as follows:
dim effects1 as new Effects
effects1.FlipVertically
either Either way, you should simply amend the sub as follows:
Public Sub FlipVertically()
frmMain.picOriginal.Image.RotateFlipRotateFlipType.RotateNoneFlipY)picOriginal.Invalidate()
End Sub
If you do not want to create an instance of Efffects, then make the sub Shared by;
Public Shared Sub FlipVertically()
picOriginal.Image.RotateFlip(RotateFlipType.RotateNoneFlipY)
picOriginal.Invalidate()
End Sub
and call it from frmMain with
Effects.FlipVertically
Last edited by taxes; Jan 28th, 2004 at 04:52 AM.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jan 28th, 2004, 09:54 AM
#11
Thread Starter
Fanatic Member
Thanks
Thank you. I had already read the other post and used it's examples and they worked. However, When I tried to apply it to what I was doing, it did not work.
I have this in frmMain
Private Sub mnuFlipVertically_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles mnuFlipVertically.Click
Effects.FlipVertically()
End Sub
and this in Effects.vb
Public Shared Sub FlipVertically()
picOriginal.Image.RotateFlip(RotateFlipType.RotateNoneFlipY)
picOriginal.Invalidate()
End Sub
As it is right now, picOriginal has the blue lines under it saying that it is not declared. I changed the Effects.vb code to read:
Public Shared Sub FlipVertically()
Dim frmMain As frmMain
frmMain.picOriginal.Image.RotateFlip(RotateFlipType.RotateNoneFlipY)
frmMain.picOriginal.Invalidate()
End Sub
This allow the program to run, but I got the below error:
Object reference not set to an instance of an object
-
Jan 28th, 2004, 10:11 AM
#12
PowerPoster
Hi birthjay,
" Public Shared Sub FlipVertically()
Dim frmMain As frmMain
frmMain.picOriginal.Image.RotateFlip(RotateFlipType.RotateNoneFlipY)
frmMain.picOriginal.Invalidate()
End Sub"
What you have done here is to open a new instance of frmMain, so you have two open at the same time. Try removing
Dim frmMain as frmMain
from your sub.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jan 28th, 2004, 11:00 AM
#13
Shared means it can be called without an instance but that also means that it can't have instanced resources inside its call. Since you are wanting to access controls on a specific instance then you'll need to pass the instance you want into the call.
VB Code:
Public Shared Sub FlipVertically(ByVal inst As frmMain)
inst.picOriginal.Image.RotateFlip(RotateFlipType.RotateNoneFlipY)
inst.picOriginal.Invalidate()
End Sub
'syntax to call from within a frmMain instance
FlipVertically(Me)
If you are not trying to call this method from within an instance of frmMain then you will still have the original problem of getting the instance of frmMain that you want. The solution to that depends on more things about your project but could be found via searching this forum.
-
Jan 28th, 2004, 12:34 PM
#14
PowerPoster
Hi birthjay,
Without disputing what edneeis has posted, all you have to do is remove the "Dim frmMain as frmMain" from your code
"Public Shared Sub FlipVertically()
Dim frmMain As frmMain
frmMain.picOriginal.Image.RotateFlip(RotateFlipType.RotateNoneFlipY)
frmMain.picOriginal.Invalidate()
End Sub"
I have tried it and replicated your error message by leaving it in. It works if you leave it out.
I have not tried out edneeis' suggestion but I suspect that all he is doing is repeating the double instancing of frmMain. If so, it will not solve your problem.
Best of luck.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jan 28th, 2004, 04:28 PM
#15
PowerPoster
Hi birthjay
I've just tried out Edneeis's code and it also works. I guess that what he has done is declared inst as a container of form1 and not as a new instance, so that there is only one form1 open.
You can even use Edneeis's code to call the public shared sub from another form entirely, e.g. form3!
As the saying goes, there are many ways to crack an egg!
Last edited by taxes; Jan 28th, 2004 at 04:35 PM.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jan 28th, 2004, 04:34 PM
#16
i would never have doubted that edd's code wouldn't work , i think he knows what he's talking about
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Jan 28th, 2004, 04:41 PM
#17
PowerPoster
Hi edneeis,
"If you are not trying to call this method from within an instance of frmMain then you will still have the original problem of getting the instance of frmMain that you want. The solution to that depends on more things about your project but could be found via searching this forum."
I tried calling the method from a form3 and it worked perfectly, provided that the calling event code was
form2.FlipVertically(form1)
Last edited by taxes; Jan 29th, 2004 at 05:34 AM.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jan 28th, 2004, 04:48 PM
#18
PowerPoster
Hi dynamic_sysop
"i would never have doubted that edd's code wouldn't work , i think he knows what he's talking about "
I am not doubting him!! I am just trying to make sure I fully understand where he is coming from and to do that I have to experiment with the suggestions. I am just a beginner in VB.NET but in life generally it is more important to know what is wrong than what is right. Anyway, I learn from my mistakes and, so far, I've made every mistake possible!
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jan 28th, 2004, 05:42 PM
#19
Thread Starter
Fanatic Member
Thanks!
Thank you all! I appreciate all the discussion and feedback!
Edneeis, your code works except that instead of using:
FlipVertically(Me) -------- I used Effects.FlipVertically(Me)
I really do appreciate ALL the posts!
-
Jan 28th, 2004, 05:46 PM
#20
hey taxes dont take offence , i was only stating that edd knows his stuff
for someone who's only just started out in .NET , you're doing ok by the look of this thread
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Jan 28th, 2004, 05:55 PM
#21
PowerPoster
HI dynamic_sysop,
Hey I'm not taking offence. Quite the contrary. Do'nt forget, my sense of humour is slightly different from that of most of you guys - I have the same probem when visiting my USA son-in-law but I am educating him slowly ( or he is educating me). I am amazed by the knowledge and persistence of guys like ed.... and when guys like birthjay bring up these points, it sure makes you think things through. I am very new to forums and my learning curve has improved greatly since I joined this one. Many thanks to all, contributors and administrators. Keep up the good work.
Regards,
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jan 28th, 2004, 06:13 PM
#22
if you are in Bristol down the M5 motorway , we must share the same UK humour
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Jan 28th, 2004, 06:51 PM
#23
Hey thanks for the vote of confidence dynamic_sysop!
EDITED...I had a longer explanation of the code but I think it made things more confusing instead of less so I deleted it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|